Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: image with alpha is not displayed correctly #3695

Closed
jdhao opened this issue Mar 6, 2019 · 7 comments · Fixed by #4080
Closed

Windows: image with alpha is not displayed correctly #3695

jdhao opened this issue Mar 6, 2019 · 7 comments · Fixed by #4080
Labels
BMP Platform A catchall for platform-related Windows
Projects

Comments

@jdhao
Copy link

jdhao commented Mar 6, 2019

What did you do?

The pillow package has a method called Image.putalpha() which is used to add or change the alpha channel of an image.

I tried to play with this method and found that I can not change the background color of an image. The original image is

owl

This is my code to add alpha to it

from PIL import Image

im_owl = Image.open("owl.jpg")

alpha = Image.new("L", im_owl.size, 50)
im_owl.putalpha(alpha)

im_owl.show()

What did you expect to happen?

The produced image shows the effect of adding alpha channel to image.

What actually happened?

The produced image is nothing different from the original image. I have tried with different value of alpha and see no difference.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 1803
  • Python: 3.6.5
  • Pillow: 5.3.0

After consulting the pillow doc about Image.show() method, it seems that pillow will convert the Image object to bmp files and show it with default system image viewer. That explains the reason why no difference between the original and alpha-composited image.

It would be great if the image is shown as its original format, not as bmp files.

@hugovk
Copy link
Member

hugovk commented Mar 6, 2019

Yep, the docs say:

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).

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show

Note "method is mainly intended for debugging purposes".

You can save the image first and then use the viewer of your choice to open it.

Alternatively, it's possible to override it to use your own choice of viewer. See ImageShow.py and:

Pillow/src/PIL/Image.py

Lines 2899 to 2909 in 8194677

# --------------------------------------------------------------------
# Simple display support. User code may override this.
def _show(image, **options):
# override me, as necessary
_showxv(image, **options)
def _showxv(image, title=None, **options):
from . import ImageShow
ImageShow.show(image, title, **options)

@hugovk
Copy link
Member

hugovk commented Mar 6, 2019

Perhaps a WindowsPngViewer with format = "PNG" would work (maybe subclass WindowsViewer), and then register(WindowsPngViewer, -1)? I don't have a Windows machine handy to test.

@jdhao
Copy link
Author

jdhao commented Mar 6, 2019

On Mac, I think the problem is the same. If the mode the Image object is RGBA, it should be displayed as PNG format, not as BMP format. It would be great to add that option to correctly display RGBA image.

If it can not be done, at least there should be a warning that the displayed image is not a faithful representation of the true image. It really took me a while to figure out the problem is that the displayed image is not the same as the true image.

@hugovk
Copy link
Member

hugovk commented Mar 6, 2019

Actually, Mac switched from BMP to PNG in #2527 and we've not updated the Image.py docstring yet (I've made #3696 for that).

Perhaps we could default to PNG with Windows too. We'd need to make sure (1) we can save everything as PNG, or if not, save as BMP, and (2) this command works:

'start "Pillow" /WAIT "%s" '

"Pillow" is not a built-in viewer, it's a required title:

Syntax
      START "title" [/D path] [options] "command" [parameters]

Key:
   title       Text for the CMD window title bar (required.)
   path        Starting directory.
   command     The command, batch file or executable program to run.
   parameters  The parameters passed to the command.

https://ss64.com/nt/start.html

It just tells Windows to open an image file by giving the path to the file, and it'll open it in whatever program is associated with that file type. Usually IrfanView on my Windows machine, but we need it in a format that will open in something default like MS Paint (is that the default option?).

A quick look at https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions shows Windows 7, 8.1 and 10 are still supported PC versions. I don't know if we need to worry about Windows Server when it comes to showing images.

@hugovk hugovk changed the title image with alpha are not displayed correctly Windows: image with alpha is not displayed correctly Mar 6, 2019
@hugovk hugovk added the Windows label Mar 6, 2019
@jdhao
Copy link
Author

jdhao commented Mar 7, 2019

It works. I add the following code after register(WindowsViewer) in the file ImageShow.py:

    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)

After that, I can show the image with alpha channel correctly.

@aclark4life aclark4life added BMP Platform A catchall for platform-related and removed BMP labels May 11, 2019
@aclark4life aclark4life added this to Backlog in Pillow May 11, 2019
@aclark4life aclark4life moved this from Backlog to In progress in Pillow May 11, 2019
@radarhere
Copy link
Member

radarhere commented Aug 15, 2019

End users should also be able to switch to PNG through -

from PIL import Image, ImageShow
ImageShow.WindowsViewer.format = "PNG"

im_owl = Image.open("owl.jpg")
im_owl.show()

Testing, I find that both BMP and PNG can save - 1, L, P, PA, F, RGB, RGBA, RGBX, CMYK, YCbCr - while PNG can save some modes that BMP can't - for example, LA and I. So I don't think we need to worry about using BMP as a fallback for certain image modes.

@radarhere
Copy link
Member

I've created PR #4080 to resolve this.

Pillow automation moved this from In progress to Closed Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BMP Platform A catchall for platform-related Windows
Projects
Pillow
  
Closed
Development

Successfully merging a pull request may close this issue.

4 participants