hellodk34/jpg2gif

无法保存透明像素

Au3C2 opened this issue · 5 comments

Au3C2 commented

无法保存透明像素,透明像素会被转换成白色

@Au3C2 昨晚睡前才看到 issue。今天处理了一下,但是效果差强人意。这个问题我早前也意识到了,只是没有去处理,感谢你的提醒。请尝试 release v1.1

Au3C2 commented

谢谢您,我昨天摸鱼找到了一个python程序哈哈,贴在这里,各位自取

from PIL import Image
from glob import glob
import os

def gen_frame(path):
    im = Image.open(path)
    try:
        alpha = im.getchannel('A')
    except:
        return im
    # Convert the image into P mode but only use 255 colors in the palette out of 256
    im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)

    # Set all pixel values below 128 to 255 , and the rest to 0
    mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)

    # Paste the color of index 255 and use alpha as a mask
    im.paste(255, mask)

    # The transparency index is 255
    im.info['transparency'] = 255
    
    return im

img_path = r'316401021'
img_list = glob(img_path + r'\*.png')
# im1 = gen_frame(r'316401021\0.png')
# im2 = gen_frame('frame2.png')     
out_path = img_path + r'\gif'
if not os.path.exists(out_path):
    os.mkdir(out_path)

for img in img_list:   
    img_name = os.path.basename(img).split('.')[0]
    im = gen_frame(img)
    im.save(f'{out_path}/{img_name}.gif', save_all=True, loop=0, duration=200)

@Au3C2 贴在哪里了?

@Au3C2 看到了,非常感谢分享!

Au3C2 commented

@Au3C2 贴在哪里了?

敲快了还没编辑完就发出去了,重新编辑了下