[Tutor] Python Image Library

eryk sun eryksun at gmail.com
Wed May 17 22:52:42 EDT 2017


On Thu, May 18, 2017 at 1:58 AM, Michael C
<mysecretrobotfactory at gmail.com> wrote:
> when I run this, while it's called test.pyw, this pops up
>
> from PIL import Image
>
> im = Image.open('1.bmp')
> im.show()

Ok, I stand corrected. It's something weird, and most likely due to
Windows support here being an afterthought throw in just for coverage,
instead of something anyone actually cared about. The code does indeed
use os.system to show the file, which is just about the worst thing
one can do here for a Windows viewer.

    >>> print(inspect.getsource(ImageShow.WindowsViewer))
        class WindowsViewer(Viewer):
            format = "BMP"

            def get_command(self, file, **options):
                return ('start "Pillow" /WAIT "%s" '
                        '&& ping -n 2 127.0.0.1 >NUL '
                        '&& del /f "%s"' % (file, file))

    >>> print(inspect.getsource(ImageShow.Viewer.show_file))
        def show_file(self, file, **options):
            """Display given file"""
            os.system(self.get_command(file, **options))
            return 1

The WindowsViewer class preferrably should override show_file to  call
ShellExecuteExW directly via ctypes, and wait (if possible) to delete
the temporary file. Or at least use subprocess.call with shell=True,
which will hide the console window. Note that being able to wait on
the image viewer is not guaranteed. For example, the image viewer in
Windows 10 is an app that cannot be waited on.

Since you already have a file on disk, you could skip PIL completely.
Just use os.startfile('1.bmp').


More information about the Tutor mailing list