Image in a module question
Christopher
chris_mk at hotmail.com
Thu Mar 28 18:58:56 EST 2002
Thanks for the feedback, I looked at the code and it gave me some
great ideas. Although, a little too late, lol (I actually may end up
using it). I ended up 're-inventing the wheel' although, in my case,
it was kind of fun (though, I still need help).
I couldn't figure out the image2py module that came with PIL (I kept
getting errors when I tried to import the resulting file). So I wrote
my own. It ends up doing what I wanted but at a price, the resulting
module is much larger than the original picture (this has to do with
the fact that I was getting errors when I tried to use the data in
strings, so I made a list instead. Any way, I'll post my code at the
end of this post (please be gentle, I am still struggling through the
learning phase of this whole programming thing ;-) ).
Pete Shinners <shredwheat at attbi.com> wrote in message news:<3CA2E042.2080307 at attbi.com>...
> i've got a working tool that does this. although it uses pygame to load
> and display the image (could probably be switched to PIL without too
> much effort)
>
> http://www.pygame.org/pcr/submissions/01142002b.html
"""
CK_Image2Py
To save an image in a module:
Make_Module()
To open an image in a module:
import ImageTk
from Tkinter import *
myImage = CK_Image2Py.Open_Module() # Enter the module
root = Tk() # create a Tk() object
myNew = ImageTk.PhotoImage(myImage) # Convert the image into Tk
Label(root, image = myNew).pack() # Include the image in an object
root.mainloop() # Run the Tk object
"""
import sys
import SysSetUp
import zlib
import tkFileDialog
import Tkinter
import Image
import StringIO
import ImageTk
from Tkinter import *
def Find_Image():
myPicFile = tkFileDialog.askopenfilename()
return myPicFile
def Compress_Image(Img_File_Name):
myString = file(Img_File_Name, 'rb').read()
myCompString = zlib.compress(myString)
return myCompString
def SaveString(Compressed_String):
myModule = tkFileDialog.asksaveasfilename()
myList = []
for x in Compressed_String:
append_this = r"""%s""" % x
myList.append(append_this)
myString = 'IMAGE_List = %s' % myList
file(myModule, 'wb').write(myString)
def Decode_Module(module_list):
module_string = ''
for x in module_list:
module_string += x
myString = zlib.decompress(module_string)
return myString
def Make_Image(Image_String):
myImage = Image.open(StringIO.StringIO(Image_String))
return myImage
def Make_Module():
myImageFile = Find_Image()
myCompressedImage = Compress_Image(myImageFile)
SaveString(myCompressedImage)
def Open_Module():
myModule = Find_Image()
myModulePath = myModule[:myModule.rfind(r"""/""")]
sys.path.append(myModulePath)
myModuleName = myModule[(myModule.rfind(r"""/""")+1):][:-3]
exec("from %s import IMAGE_List" % myModuleName)
myImageString = Decode_Module(IMAGE_List)
The_Image = Make_Image(myImageString)
return The_Image
def Open_Specific_Module(Module_Name):
myModuleName = Module_Name[:-3]
exec("from %s import IMAGE_List" % myModuleName)
myImageString = Decode_Module(IMAGE_List)
The_Image = Make_Image(myImageString)
return The_Image
if __name__ == "__main__":
import Image
if len(sys.argv) == 1:
myImage = Open_Module() # Enter the module
mainloop()
else:
Open_Specific_Module(sys.argv[1])
root = Tk() # create a Tk() object
myNew = ImageTk.PhotoImage(myImage) # Convert the image into Tk
widget = Label(root, image = myNew)
widget.pack() # Include the image in an object
root.mainloop() # Run the Tk object
More information about the Python-list
mailing list