Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法保存透明像素 #1

Closed
Au3C2 opened this issue May 4, 2022 · 5 comments
Closed

无法保存透明像素 #1

Au3C2 opened this issue May 4, 2022 · 5 comments

Comments

@Au3C2
Copy link

Au3C2 commented May 4, 2022

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

@hellodk34
Copy link
Owner

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

@Au3C2
Copy link
Author

Au3C2 commented May 5, 2022

谢谢您,我昨天摸鱼找到了一个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)

@hellodk34
Copy link
Owner

@Au3C2 贴在哪里了?

@hellodk34
Copy link
Owner

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

@Au3C2
Copy link
Author

Au3C2 commented May 5, 2022

@Au3C2 贴在哪里了?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants