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

相关推荐
高级IT技术专家secops99810 分钟前
在统信UOS/麒麟Kylin OS操作系统中配置APT和GIT代理
运维·服务器·git·系统安全·kylin
信号处理学渣1 小时前
Git命令行中vim的操作
git·编辑器·vim
Connie14511 小时前
git tag使用场景和实践
git
Jake_的技能小屋4 小时前
关于使用git init --bare 裸仓库的使用
git
西瓜本瓜@8 小时前
在Android中如何使用Protobuf上传协议
android·java·开发语言·git·学习·android-studio
4Forsee11 小时前
【Git】从本地存档到协作开发的Git简单使用
git
Э时间行者于我13 小时前
git同时删除多个分支
git
我的golang之路果然有问题20 小时前
给git配置SSH(github,gitee)
经验分享·笔记·git·学习·gitee·ssh·github
漫步企鹅21 小时前
[Git] Git Stash 命令详解
git·git push·git pull·git commit·git pull rebase
船长@Quant1 天前
协作开发攻略:Git全面使用指南 — 第二部分 高级技巧与最佳实践
git·版本控制·源代码管理·协作开发