如何使用Python Pillow的Transpose.FLIP_TOP_BOTTOM?

Sza*_*mbi 3 python deprecated python-imaging-library python-3.x

我正在使用Pillow 9.1.0将图像上下翻转。

from PIL import Image

img = Image.open('example.png')
flipped = img.transpose(Image.FLIP_TOP_BOTTOM)
Run Code Online (Sandbox Code Playgroud)

最近出现一个警告:

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)
Run Code Online (Sandbox Code Playgroud)

我尝试从 PIL 导入 Transpose 但没有成功。

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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

如何Transpose.FLIP_TOP_BOTTOM正确导入和使用?

Bla*_*ans 5

这一切都写在弃用文档中。相反Image.FLIP_TOP_BOTTOM,您应该使用Image.Transpose.FLIP_TOP_BOTTOM.