如何使用Python Pillow的Transpose.FLIP_TOP_BOTTOM?

3
我正在使用Pillow 9.1.0将图像上下翻转。
from PIL import Image

img = Image.open('example.png')
flipped = img.transpose(Image.FLIP_TOP_BOTTOM)

最近出现了一个警告:

DeprecationWarning: FLIP_TOP_BOTTOM is deprecated and will be removed in Pillow 10 (2023-07-01). Use Transpose.FLIP_TOP_BOTTOM instead.
  flipped = img.transpose(Image.FLIP_TOP_BOTTOM)

我尝试从PIL导入Transpose,但它没有起作用。

from PIL import Image, Transpose

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    from PIL import Image, Transpose
ImportError: cannot import name 'Transpose' from 'PIL' (.../site-packages/PIL/__init__.py)

如何正确导入和使用Transpose.FLIP_TOP_BOTTOM
2个回答

5

所有的内容都在弃用文档中写明。不要使用Image.FLIP_TOP_BOTTOM,而应该使用Image.Transpose.FLIP_TOP_BOTTOM


-2

你忘记保存翻转后的图像了。 尝试添加以下代码行。

flipped.save()

所以,最终你的代码必须像这样:

from PIL import Image

img = Image.open('example.png')
flipped = img.transpose(Image.FLIP_TOP_BOTTOM)
flipped.save('example.png')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接