PIL图像旋转和背景问题

时间:2019-09-07 11:32:38

标签: python python-imaging-library

我正在尝试使用PILrotate()旋转透明gif,但是旋转后会得到钻石。

SO上的类似问题通过使用

得以解决
transparency = img.info['transparency']
img.save('result.gif',transparency=transparency)

或通过使用

img.save('result.gif', **img.info)

但是我得到的是图片中的以下结果。

Original Rotated

我的代码是

from PIL import Image

file = 'change2_1.gif'

with Image.open(file) as im:
    transparency = im.info['transparency']
    img = im.rotate(45, expand=True)
    img.save('result.gif', transparency=transparency)

1 个答案:

答案 0 :(得分:0)

帖子编辑:尝试使用它使黑色背景透明

from PIL import Image

img = Image.open('img.png')
img = img.convert("RGBA")
datas = img.getdata()

with Image.open(file) as im:
transparency = im.info['transparency']
img = im.rotate(90, expand=True)
img.save('result.gif', transparency=transparency)

newData = []
for item in datas:
    if item[0] == 0 and item[1] == 0 and item[2] == 0:
        newData.append((255, 255, 255, 0))
    else:
        newData.append(item)