Как отметили @sanyam и @Pam, мы можем сохранить преобразованное изображение, и оно отображается правильно. Это связано с тем, что в Windows изображения сохраняются как временные BMP-файлы, прежде чем они будут показаны с помощью системного средства просмотра изображений по умолчанию, согласно документации PIL :
Image.show(title=None, command=None)
Displays this image. This method is mainly intended for debugging purposes.
On Unix platforms, this method saves the image to a temporary PPM file, and calls
either the xv utility or the display utility, depending on which one can be found.
On macOS, this method saves the image to a temporary BMP file, and opens it with
the native Preview application.
On Windows, it saves the image to a temporary BMP file, and uses the standard BMP
display utility to show it (usually Paint).
Чтобы исправить эту проблему, мы можем исправить код подушки, чтобы использовать формат PNG по умолчанию. Для начала нам нужно найти корень пакета Pillow:
import PIL
print(PIL.__path__)
В моей системе вывод:
[»D: \ Anaconda \ Lib \ сайт-пакеты \ PIL»]
Перейдите в этот каталог и откройте файл ImageShow.py
. Я добавляю следующий код после строки register(WindowsViewer)
:
class WindowsPNGViewer(Viewer):
format = "PNG"
def get_command(self, file, **options):
return ('start "Pillow" /WAIT "%s" '
'&& ping -n 2 127.0.0.1 >NUL '
'&& del /f "%s"' % (file, file))
register(WindowsPNGViewer, -1)
После этого я могу правильно отобразить изображение с альфа-каналом.
Ссылки