Select to view content in your preferred language

ArcMap Crashes when running Python script using PIL

3364
4
06-08-2012 01:06 PM
GerryJames
Emerging Contributor
I am trying to run a script that generates a number of down sampled images from source images in a given folder.  I am using the Python Imaging Library (PIL) for this and the code appears to crash on the line where the output image is saved.  I have put a try/catch statement in to get more information but the process appears to crash so it doesn't even get to the error message.  The code looks like the following:

import os, glob, arcpy
import Image

pth = arcpy.GetParameterAsText(0)
sep = arcpy.GetParameterAsText(1)
extarr = sep.split(".")
ext = extarr[len(extarr)-1]
sze_w = arcpy.GetParameterAsText(2)
sze_h = arcpy.GetParameterAsText(3)
arcpy.AddMessage("Got arguments!")
for (root, dirs, files) in os.walk(pth):
     for fle in files:
         if fle.endswith(ext):
             fullfle = os.path.join(root,fle)
             img = Image.open(fullfle)
##             img.MAXBLOCK = 1024*1024
             size = sze_w,sze_h
             img.thumbnail(size, Image.ANTIALIAS)
             oset = -1 * len(sep)
             f = fle[:oset]
             fullf = os.path.join(root, f + ".thumbnail.jpg")
##             arcpy.AddMessage(fullf + " created!")
             try:
                  img.save(fullf, "PNG")
             except Exception as e:
                  arcpy.AddMessage(e)
                  
             arcpy.AddMessage(fullf + " created!")
##             #print fullf + " created!"
             del img


I should also mention that the code works just fine when run from the Python command line. 

Does anyone have any ideas why this is happening?

Thanks in advance
Gerry James
Geomatics Manager
Hatfield Consultants Partnership
Tags (2)
0 Kudos
4 Replies
NobbirAhmed
Esri Regular Contributor
What is your sep variable? What values are expected for this parameter? I'm trying to reproduce your case. Thanks.
0 Kudos
NobbirAhmed
Esri Regular Contributor
This code is working fine on 10.0 - in my script tool I only have one parameter - the path to the folder.

in_path = arcpy.GetParameterAsText(0)

for root, dirs, files in os.walk(in_path):

    for f in files:

        existing_file = os.path.join(root, f)
        file_loc = os.path.splitext(existing_file)[0]

        try:
            img = Image.open(existing_file)
            thumb_size = 128, 128
            img.thumbnail(thumb_size, Image.ANTIALIAS)
            img.save(file_loc + ".thumbnail", "PNG")

        except:
            arcpy.AddMessage("Skipping, not an image :" + existing_file)
            pass # not an image file
0 Kudos
GerryJames
Emerging Contributor
The sep and ext variables are a bit weird in this case.  I am trying to give as much flexibility as to how the output file is named and because many of our images have more than one dot (.) in the name, I allow the user to basically identify the full extension (e.g. .ecw.jpg).  This example gives the information as to where the extension should be broken so that the .thumbnail part can be inserted right after the name of the file.  In this case the output file would be something like file_name.thumbnail.jpg.
0 Kudos
NobbirAhmed
Esri Regular Contributor
Just tested the code I posted above (obtained from http://www.pythonware.com/library/pil/handbook/image.htm) with some files having multiple dots in their names such as:

IMG.Churro.ecf.PNG
Spider.man.IMG.4181.PNG
IMG.ecf.JPG

The output thumbnails came out as:

IMG.Churro.ecf.thumbnail
Spider.man.IMG.4181.thumbnail
IMG.ecf.thumbnail

If you want the extension of the thumnail image, change this line of code:

img.save(file_loc + ".thumbnail", "PNG")


To this line:

img.save(file_loc + ".thumbnail.png", "PNG")
0 Kudos