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

相关推荐
2501_9032386511 分钟前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
神明木佑2 小时前
datalist 是什么?以及作用是什么?
git
那片海还在吗4 小时前
使用 Git、Postman、Newman、Jenkins 与邮件构建自动化接口测试及通知流程
git·jenkins·postman
heeheeai12 小时前
git 国内源
git
the_seventh_dog12 小时前
git | 团队协作开发注意事项
git
yinshuilan15 小时前
今日运维之-Mac笔记本python环境问题
运维·git·python·brew
辄也16 小时前
git拉取远程分支到本地分支
git
皮克斯的进化之路16 小时前
Git原理+使用(超详细)
git
wrjwww16 小时前
【Git学习笔记】Git常用命令
笔记·git·学习
大乔乔布斯17 小时前
WSL,Power shell 和CMD, Git bash的区别
开发语言·git·bash