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

相关推荐
rannn_1111 天前
【Git教程】概述、常用命令、Git-IDEA集成
java·git·后端·intellij-idea
春日见1 天前
虚拟机上由于网络问题无法正常git clone
linux·服务器·网络·人工智能·git·ubuntu·debug
冉佳驹1 天前
Linux ——— Git的核心操作流程、进程状态及环境变量相关知识
linux·git·进程·环境变量·进程状态·fork
cherry有点甜·1 天前
【git】git为什么会出现双向合并
git
笨鸟不是菜鸟1 天前
gitignore文件如何添加忽略文件或文件夹
git
灰色人生qwer1 天前
git add . 添加超长文件名报错了怎么办?
git·python·elasticsearch
星海拾遗2 天前
git rebase记录
大数据·git·elasticsearch
ljh5746491192 天前
PhpStorm 2022.3 版本中,修改使用 Git 提交时看到弹出式的对话框模式
ide·git·php·phpstorm
云闲不收2 天前
git rebase
git
江上清风山间明月2 天前
git pull和git checkout在恢复文件的区别
git·pull·checkout