[Tutor] Saving an Image in a Given Format

Kent Johnson kent37 at tds.net
Sun Mar 30 14:57:32 CEST 2008

Wayne Watson wrote:
> I see that I misunderstood the syntax of Python for:
> current_image.convert 
> current_image.save
> current_image is a "pointer" whose type is some class, so convert() and 
> save() must be methods in that class.


> The question is what class/module? 
> PIL?

That is a likely candidate, especially if you know the program uses PIL. 
But it could be another image class, or a custom wrapper around PIL...

You should look for the place where the images are created. Or in your 
code below you could put
print self.current_image.__class__
before the save() statements and see what it tells you.

It's curious that saveGIF() needs the convert() call. Maybe that is what 
you need in the save every 10 minutes call? What format is the original 
data? What is the format of the tiff files saved every 10 minutes?

You have to do a bit of detective work to sort this out.

> Wayne Watson wrote:
>> I'm pretty new to Python and libraries. I'm actually trying to modify 
>> some code someone else wrote. There are two ways images are saved. One 
>> is for the user to select a "Save as GIF" menu item, or save as tiff, or 
>> others. The other way is that the user wants a collected image from a 
>> camera saved every, say, 10 minutes. The "save" process is different for 
>> some reason. Here are the two code segments involved (NOTE my comments 
>> and questions below B.):
>> A. Save every 10 minutes
>>         t = time.localtime(now_time)
>>         s = "a%4d%02d%02d_%02d%02d%02d.tif" % (  
>>             t.tm_year, t.tm_mon, t.tm_mday,
>>             t.tm_hour, t.tm_min, t.tm_sec )
>>         s = os.path.join("Exposures",s)   <========== auto-exposures
>>         if not os.path.exists("Exposures"):
>>             os.mkdir("Exposures")
>>         self.current_image.save(s)  <============ save image
>>         if self.trigger_mode:
>>             self.Trigger()
>>             self.CheckEvent()
>> contrast this with where the user specifically wants the image he sees 
>> saved as a gif:
>> B. From menu option
>> def SaveGIF(self):
>>         if self.current_path:
>>             default_path = splitext(basename(self.current_path))[0] + 
>> ".gif"
>>             path = asksaveasfilename(defaultextension=".gif",
>>                                      title="Save as GIF",
>>                                      initialfile=default_path,
>>                                      filetypes=GIF_FILE_TYPES)
>>         else:
>>             path = asksaveasfilename(defaultextension=".gif",
>>                                      title="Save as GIF",
>>                                      filetypes=GIF_FILE_TYPES)
>>         if not path:
>>             return
>>         gif = self.current_image.convert("RGB")
>>         gif.save(path) <===========Save as gif.
>> The programmer told me if I change the tif in A. to gif, jpg or 
>> whatever, it would work. Instead I get a file the of zero length when I 
>> use jpg. Can anyone explain why this wouldn't work? I see that 
>> current_image.convert is involved in one place and current_image.save in 
>> the first. What module owns these "current" methods?
>> Hmmm, maybe I needed to use jpeg?

More information about the Tutor mailing list