home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / PIL-1.1.2.zip / Lib / site-packages / PIL / ImageTk.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2001-12-25  |  7KB  |  192 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. import Tkinter
  5. import Image
  6. _pilbitmap_ok = None
  7.  
  8. def _pilbitmap_check():
  9.     global _pilbitmap_ok
  10.     if _pilbitmap_ok is None:
  11.         
  12.         try:
  13.             im = Image.new('1', (1, 1))
  14.             Tkinter.BitmapImage(data = 'PIL:%d' % im.im.id)
  15.             _pilbitmap_ok = 1
  16.         except:
  17.             _pilbitmap_ok = 0
  18.  
  19.     
  20.     return _pilbitmap_ok
  21.  
  22.  
  23. class PhotoImage:
  24.     
  25.     def __init__(self, image = None, size = None, **kw):
  26.         if image is None:
  27.             if kw.has_key('file'):
  28.                 image = Image.open(kw['file'])
  29.                 del kw['file']
  30.             elif kw.has_key('data'):
  31.                 StringIO = StringIO
  32.                 import StringIO
  33.                 image = Image.open(StringIO(kw['data']))
  34.                 del kw['data']
  35.             
  36.         
  37.         if hasattr(image, 'mode') and hasattr(image, 'size'):
  38.             mode = image.mode
  39.             if mode == 'P':
  40.                 image.load()
  41.                 
  42.                 try:
  43.                     mode = image.palette.mode
  44.                 except AttributeError:
  45.                     mode = 'RGB'
  46.  
  47.             
  48.             size = image.size
  49.             (kw['width'], kw['height']) = size
  50.         else:
  51.             mode = image
  52.             image = None
  53.         if mode not in [
  54.             '1',
  55.             'L',
  56.             'RGB',
  57.             'RGBA']:
  58.             mode = Image.getmodebase(mode)
  59.         
  60.         self._PhotoImage__mode = mode
  61.         self._PhotoImage__size = size
  62.         self._PhotoImage__photo = apply(Tkinter.PhotoImage, (), kw)
  63.         if image:
  64.             self.paste(image)
  65.         
  66.  
  67.     
  68.     def __del__(self):
  69.         name = self._PhotoImage__photo.name
  70.         self._PhotoImage__photo.name = None
  71.         
  72.         try:
  73.             self._PhotoImage__photo.tk.call('image', 'delete', name)
  74.         except:
  75.             pass
  76.  
  77.  
  78.     
  79.     def __str__(self):
  80.         return str(self._PhotoImage__photo)
  81.  
  82.     
  83.     def width(self):
  84.         return self._PhotoImage__size[0]
  85.  
  86.     
  87.     def height(self):
  88.         return self._PhotoImage__size[1]
  89.  
  90.     
  91.     def paste(self, im, box = None):
  92.         im.load()
  93.         image = im.im
  94.         if image.isblock() and im.mode == self._PhotoImage__mode:
  95.             block = image
  96.         else:
  97.             block = image.new_block(self._PhotoImage__mode, im.size)
  98.             image.convert2(block, image)
  99.         tk = self._PhotoImage__photo.tk
  100.         
  101.         try:
  102.             tk.call('PyImagingPhoto', self._PhotoImage__photo, block.id)
  103.         except Tkinter.TclError:
  104.             v = None
  105.             
  106.             try:
  107.                 import _imagingtk
  108.                 
  109.                 try:
  110.                     _imagingtk.tkinit(tk.interpaddr(), 1)
  111.                 except AttributeError:
  112.                     _imagingtk.tkinit(id(tk), 0)
  113.  
  114.                 tk.call('PyImagingPhoto', self._PhotoImage__photo, block.id)
  115.             except (ImportError, AttributeError, Tkinter.TclError):
  116.                 raise 
  117.  
  118.  
  119.  
  120.  
  121.  
  122. class BitmapImage:
  123.     
  124.     def __init__(self, image = None, **kw):
  125.         if image is None:
  126.             if kw.has_key('file'):
  127.                 image = Image.open(kw['file'])
  128.                 del kw['file']
  129.             elif kw.has_key('data'):
  130.                 StringIO = StringIO
  131.                 import StringIO
  132.                 image = Image.open(StringIO(kw['data']))
  133.                 del kw['data']
  134.             
  135.         
  136.         self._BitmapImage__mode = image.mode
  137.         self._BitmapImage__size = image.size
  138.         if _pilbitmap_check():
  139.             image.load()
  140.             kw['data'] = 'PIL:%d' % image.im.id
  141.             self._BitmapImage__im = image
  142.         else:
  143.             kw['data'] = image.tobitmap()
  144.         self._BitmapImage__photo = apply(Tkinter.BitmapImage, (), kw)
  145.  
  146.     
  147.     def __del__(self):
  148.         name = self._BitmapImage__photo.name
  149.         self._BitmapImage__photo.name = None
  150.         
  151.         try:
  152.             self._BitmapImage__photo.tk.call('image', 'delete', name)
  153.         except:
  154.             pass
  155.  
  156.  
  157.     
  158.     def width(self):
  159.         return self._BitmapImage__size[0]
  160.  
  161.     
  162.     def height(self):
  163.         return self._BitmapImage__size[1]
  164.  
  165.     
  166.     def __str__(self):
  167.         return str(self._BitmapImage__photo)
  168.  
  169.  
  170.  
  171. def _show(image, title):
  172.     
  173.     class UI(Tkinter.Label):
  174.         
  175.         def __init__(self, master, im):
  176.             if im.mode == '1':
  177.                 self.image = BitmapImage(im, foreground = 'white', master = master)
  178.             else:
  179.                 self.image = PhotoImage(im, master = master)
  180.             Tkinter.Label.__init__(self, master, image = self.image, bg = 'black', bd = 0)
  181.  
  182.  
  183.     if not (Tkinter._default_root):
  184.         raise IOError, 'tkinter not initialized'
  185.     
  186.     top = Tkinter.Toplevel()
  187.     if title:
  188.         top.title(title)
  189.     
  190.     UI(top, image).pack()
  191.  
  192.