怎么给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):

相关推荐
maply1 小时前
VSCode 中的 Git Graph扩展使用详解
ide·git·vscode·编辑器·扩展
行十万里人生5 小时前
Qt事件处理:理解处理器、过滤器与事件系统
开发语言·git·qt·华为od·华为·华为云·harmonyos
会敲代码的Steve8 小时前
git笔记-简单入门
笔记·git
画船听雨眠aa10 小时前
gitlab云服务器配置
服务器·git·elasticsearch·gitlab
Tangcan-1 天前
Linux中基础开发工具(yum,vim,gcc/g++,git,gdb/cgdb)
linux·git·vim
苏-言1 天前
Git进阶之旅:分支管理策略
git
初级代码游戏1 天前
git:恢复纯版本库
git·恢复·纯版本库
苏-言1 天前
Git进阶之旅:Git 多人合作
git
lingllllove1 天前
git rebase的使用
git
苏-言1 天前
Git进阶之旅:tag 标签 & IDEA 整合 Git
git