图片压缩处理动图异常

文章讲述了在处理图片压缩任务时遇到动态图片失败的问题,作者通过截取第一帧图像解决了这个问题,并分享了使用Pillow和OpenCV进行处理的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前处理一批图片压缩任务,发现有一些是压缩失败的,排查一下发现是动态图片任务造成的,
所以针对这种情况,我这里采用截取第一帧图像进行压缩

# -*- coding: UTF-8 -*-
import requests
from PIL import Image, ImageSequence
import io
import numpy as np
import cv2


def zip_img(image_bytes):
    # 尝试以Pillow来打开图片
    image = Image.open(io.BytesIO(image_bytes))
    # 检查图片是否有多帧(即是否为动态的)
    if hasattr(image, 'is_animated') and image.is_animated:
        frames = [frame.copy() for frame in ImageSequence.Iterator(image)]
        image = frames[0]  # 只取第一帧
        # 将Pillow图片对象转换为OpenCV的格式
        image_np = np.array(image)
        image = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
    else:
        # 不是动态图片,直接解码为OpenCV的图片对象
        image_np = np.frombuffer(image_bytes, np.uint8)
        image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)

    # 以下为原函数的压缩逻辑
    h, w, c = np.shape(image)
    max_size = 600
    ratio = min(1, min(max_size / h, max_size / w))
    compressed_image = cv2.resize(image, [int(w * ratio), int(h * ratio)])
    success, encoded_image = cv2.imencode('.webp', compressed_image, [int(cv2.IMWRITE_WEBP_QUALITY), 80])
    if success:
        compressed_bytes = encoded_image.tobytes()
        return compressed_bytes
    else:
        print('编码失败')
        return False



# 下面是测试动图压缩效果 2.webp
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}

# 不用这个头压缩后的图片会变黑?
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "DNT": "1",  # Do Not Track Request Header
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1"
}

url = "https://images.dsw.com/is/image/DSWShoes/522080_660_ss_01"

response = requests.get(url, headers=headers)
# response = requests.get(url)

if response.status_code == 200:
    with open('1.webp', 'wb') as f:
        f.write(response.content)
	# 压缩图片
    zip_content = zip_img(response.content)
    if zip_content:
        with open('2.webp', 'wb') as f:
            f.write(zip_content)
else:
    print(f"Error fetching image: HTTP {response.status_code}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰履踏青云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值