Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...