怎么给git动图扣除背景?

环境:

Wn10 专业版

python

问题描述:

怎么给git动图扣除背景?

解决方案:

要将一个 GIF 动图的尺寸改为 50x50 并且把黑色背景改成透明,您可以使用 Python 的 Pillow 库。Pillow 支持处理静态图像和动画 GIF。下面是具体的步骤和代码示例:

  1. 安装 Pillow :如果您还没有安装 Pillow,可以通过 pip 安装:

    bash 复制代码
    pip install pillow
  1. 编写脚本:接下来是 Python 脚本,它会加载 GIF 文件,调整其大小到 50x50,并将所有黑色像素(或接近黑色的像素)转换为透明。
python 复制代码
from PIL import Image, ImageSequence
import math

def color_distance(color1, color2):
    """Calculate the Euclidean distance between two colors in RGB space."""
    r1, g1, b1, _ = color1
    r2, g2, b2, _ = color2
    return math.sqrt((r2 - r1) ** 2 + (g2 - g1) ** 2 + (b2 - b1) ** 2)

def make_transparent(image, target_color=(0, 33, 63), threshold=30):
    """Make pixels close to the target color transparent based on color distance."""
    image = image.convert("RGBA")
    datas = image.getdata()

    newData = []
    for item in datas:
        # If a pixel is close to the target color (based on color distance), make it transparent
        if color_distance(item, target_color + (255,)) <= threshold:
            newData.append((255, 255, 255, 0))  # Transparent
        else:
            newData.append(item)

    image.putdata(newData)
    return image

def process_gif(input_path, output_path, size=(50, 50)):
    with Image.open(input_path) as im:
        frames = [frame.copy() for frame in ImageSequence.Iterator(im)]
        
        # Process each frame
        processed_frames = []
        for frame in frames:
            resized_frame = frame.resize(size, Image.LANCZOS)  # Resize frame with LANCZOS filter
            transparent_frame = make_transparent(resized_frame)  # Make black background transparent
            processed_frames.append(transparent_frame)

        # Save the processed frames as a new gif
        processed_frames[0].save(output_path,
                                save_all=True,
                                append_images=processed_frames[1:],
                                duration=im.info.get('duration', 50),  # Use original duration or default to 50ms
                                loop=im.info.get('loop', 0))  # Use original loop count or default to forever

# Call the function with your input and output file paths
process_gif('C:\\Users\\Administrator\\Pictures\\123.gif', 'C:\\Users\\Administrator\\Pictures\\output.gif')

注意事项

  • 阈值设置make_transparent 函数中的 threshold 参数决定了什么样的颜色会被认为是"黑色"。您可能需要根据您的具体 GIF 来调整这个值。
  • 抗锯齿 :在调整大小时,我们使用了 Image.ANTIALIAS 滤镜来保证更好的图像质量。不过要注意的是,在较新的 Pillow 版本中,ANTIALIAS 已经被弃用,取而代之的是 Image.Resampling.LANCZOS
  • 帧延迟duration 参数定义了每一帧之间的时间间隔,单位是毫秒。如果原始 GIF 的帧速率不同,您可能需要调整这个值。
  • 循环次数loop=0 表示 GIF 将无限循环播放。如果您想要不同的行为,可以更改这个值。

请确保替换 'path/to/your/input.gif''path/to/your/output.gif' 为实际文件路径。运行此脚本后,您应该会在指定的位置得到一个调整过大小并且黑色背景已被设为透明的新 GIF 动图。

  1. 运行脚本

最后结果不咋地

改颜色值位置

def make_transparent(image, target_color=(0, 33, 63), threshold=30):

相关推荐
小龙10 小时前
【Git 报错解决】本地无有效提交无法推送(`src refspec main does not match any`)
git·github·报错
小扶苏11 小时前
删除git全局账号信息并设置成新的账号密码命令
git
Greg_Zhong13 小时前
Git创建任务分支进行开发,最后合并主分支master【纯git命令执行过程】阐述
git
眯眼因为很困啦14 小时前
GitHub Fork 协作完整流程
前端·git·前端工程化
AlexDeng16 小时前
Git 中模糊搜索分支名称并创建本地跟踪分支
git
jxm_csdn20 小时前
递归工程工厂:Claude Code + Git Worktrees + Tilix/Tmux 的“AI分身”编码团队
人工智能·git
码咔吧咔20 小时前
Git 中 pull.rebase = true 的作用与设置方法详解
git
郑州光合科技余经理1 天前
开发实战:海外版同城o2o生活服务平台核心模块设计
开发语言·git·python·架构·uni-app·生活·智慧城市
我是海飞1 天前
Git 本地仓库推送到远程空仓库指南
大数据·git·elasticsearch
记得开心一点嘛1 天前
如何去使用git进行协同开发?
git