Utkarsh Sinha Utkarsh Sinha - 29 days ago 23
Python Question

How to make img.show() a blocking call in Pillow?

I'm trying to see images as generated by the Python Imaging Library. Using the following snippet returns after displaying the image in a separate process. Is there a way to show images and have the python script block until I close the window?

from PIL import Image
...
img = Image.open(...)
img.show()


I could use a GUI library like Qt to achieve this. I don't want to add that just to view an image.

Answer

Pillow explicitly starts its Mac and Unix Viewers in the background. Fortunately, Pillow also exposes a way to register custom viewers so you can override this behaviour.

I use feh personally:

import os
from PIL ImageShow


class FehViewer(ImageShow.UnixViewer):
    def show_file(self, filename, **options):
        os.system('feh %s' % filename)
        return 1

ImageShow.register(FehViewer, order=-1)

[...]
Comments