Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python Pillow's Transpose.FLIP_TOP_BOTTOM?

I am using Pillow 9.1.0 to flip an image upside-down.

from PIL import Image

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

Recently a warning shown up:

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)

I tried to import Transpose from PIL but it did not work.

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)

How to import and use Transpose.FLIP_TOP_BOTTOM properly?

like image 774
Szabolcs Dombi Avatar asked Mar 06 '26 20:03

Szabolcs Dombi


1 Answers

It's all written in the deprecation document. Instead of Image.FLIP_TOP_BOTTOM, you should be using Image.Transpose.FLIP_TOP_BOTTOM.

like image 119
BlackBeans Avatar answered Mar 08 '26 08:03

BlackBeans