Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python imaging library show() on Windows

I am working on a script that downloads various image files from the web and then does some processing on them using the PIL. The script uses urlretreive to dump the images to temporary files, and right now I'm just trying to open them in a viewer using the PIL image.show() method. Here is the relevant portion of the code:

def main():

link_queue = Queue.Queue()
image_queue = Queue.Queue()

links = get_image_links('test_search')

for link in links:
    link_queue.put(link)

for image in xrange(len(links)):
    #create image downloading threads
    t = ImageDownloadingThread(link_queue, image_queue)
    t.setDaemon(True)
    t.start()

link_queue.join()

image_data = image_queue.get()
image_file, image_url = image_data[0][0], image_data[1][0] 
#get the first image downloaded and take a look
image = Image.open(image_file)
image.show()

Unfortunately, while the temporary file seems to load OK (Image.open doesn't return any errors) I get nothing in the viewer when image.show() is called:

enter image description here

I have also tried opening local, non-temporary files, in case that was part of the problem and get the same result. The OS is Windows Vista 32 bit SP2. Any ideas on what might be going wrong?

like image 359
Bitrex Avatar asked Jan 19 '12 20:01

Bitrex


People also ask

What is Python Imaging Library (PIL)?

The Python Imaging Library or PIL is an amazing Python library used for image processing. This library provides so many features for working on images using Python. It is used as an image processing tool with other Python image processing libraries like OpenCV.

What happened to the Python Imaging Library?

The Python Imaging Library or PIL allowed you to do image processing in Python. The original author, Fredrik Lundh, wrote one of my favorite Python blogs when I first started learning Python. However PIL's last release was way back in 2009 and the blog also stopped getting updated.

What is the use of image module in Python?

The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. Image.sHOW () Displays this image.

How do I get the contents of a Python image file?

Click File > Save Page As (Chrome & Firefox) or File > Save As (Safari) to save the file on your computer. Copy “cImage.py” from where you saved it to C:\Python27\Lib\site-packages and C:\Python34\Lib\site-packages\ Click cImage.py, and click the button labeled Raw to see the contents of the file.


1 Answers

show() tries to execute the default image viewer with a start /wait command on a temporary image file. The /wait parameter is supposed to wait until the viewer exits, so that the file can be deleted. Unfortunately the default viewer under Vista and Windows 7 does not respond properly to /wait and return even before they've opened the file; the file gets deleted before it can be displayed.

The usual fix is to edit ImageShow.py in the PIL package and add an extra command to wait a few seconds before deleting the file. This is a kludge, but it works most of the time. Details at velocityreviews.com and here at StackOverflow.

The other way to fix it is to associate the .bmp file format with a program that waits properly before returning, for example mspaint.exe.

like image 165
Mark Ransom Avatar answered Oct 19 '22 12:10

Mark Ransom