Images2gif不工作的Python 3.我檢查PIL的文檔,它支持讀取GIF圖像,但保存時,圖像不是動畫。我需要爲我們應用的用戶上傳的圖片加水印,我已經完成了以下代碼。它沒有GIF圖像。
def watermark(fp, text, position=None, font=None, quality=85):
if isinstance(fp, bytes):
fp = BytesIO(fp)
im = Image.open(fp)
water_im = Image.new("RGBA", im.size)
water_draw = ImageDraw.ImageDraw(water_im)
if isinstance(font, str):
font = ImageFont.truetype(font, 10 + int(im.size[0]/100))
if not position:
water_size = water_draw.textsize(text, font=font)
position = (im.size[0] - water_size[0] * 1.05,
im.size[1] - water_size[1] * 1.2)
water_draw.text(position, text, font=font)
water_mask = water_im.convert("L").point(lambda x: min(x, 160))
water_im.putalpha(water_mask)
if im.format == 'GIF':
for frame in ImageSequence.Iterator(im):
frame.paste(water_im, None, water_im)
else:
im.paste(water_im, None, water_im)
out = BytesIO()
im.save(out, im.format, quality=quality, **im.info)
return out.getvalue()
我找不到任何關於images2gif – Daniel
的文檔@Daniel - 它在源代碼中。 –