Update of /cvsroot/freevo/freevo/src/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4306

Modified Files:
        thumbnail.py 
Log Message:
create image thumbnails on-the-fly

Index: thumbnail.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/util/thumbnail.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** thumbnail.py        6 Feb 2005 18:22:03 -0000       1.8
--- thumbnail.py        10 Apr 2005 18:05:22 -0000      1.9
***************
*** 39,45 ****
  import os
  import logging
! 
! # the logging object
! log = logging.getLogger('util')
  
  # mevas for imlib2 support
--- 39,43 ----
  import os
  import logging
! import stat
  
  # mevas for imlib2 support
***************
*** 48,88 ****
  # freevo utils
  import fileops
- import cache
  import vfs
  
  
! def read_raw_thumbnail(filename):
!     """
!     Read a Freevo 'raw' thumbnail. The thumbnail itself is raw data, no
!     compression is used. This functions uses a lot of disc space, but is
!     very fast.
!     """
!     f = open(filename)
!     header = f.read(10)
!     if not header[:3] == 'FRI':
!         # raw data is a pickled imaged
!         return cache.load(filename)
!     data = f.read(), (ord(header[3]), ord(header[4])), header[5:].strip(' ')
!     f.close()
!     return mevas.imagelib.new(data[1], data[0], data[2])
! 
! 
! def create_raw_thumbnail(source, thumbnail):
!     """
!     Create a 'raw' thumbnail for the given source and store it to thumbnail.
!     Return the image object.
!     """
!     image = mevas.imagelib.open(source)
!     if image.width > 255 or image.height > 255:
!         image.scale_preserve_aspect((255,255))
!     f = open(thumbnail, 'w')
!     if image.has_alpha:
!         mode = 'RGBA'
!     else:
!         mode = 'RGB'
!     f.write('FRI%s%s%5s' % (chr(image.width), chr(image.height), mode))
!     f.write(str(image.get_raw_data(mode)))
!     f.close()
!     return image
  
  
--- 46,57 ----
  # freevo utils
  import fileops
  import vfs
+ from callback import *
  
+ # the logging object
+ log = logging.getLogger('util')
  
! # list for thumbnails to create in bg
! _create_jobs = []
  
  
***************
*** 91,147 ****
      Returns the filename of the thumbnail if it exists. None if not.
      """
!     if filename.endswith('.raw') or filename.endswith('.thumb.jpg'):
          return filename
!     thumb = vfs.getoverlay(filename + '.raw')
!     try:
!         if fileops.mtime(thumb) > fileops.mtime(filename):
!             return thumb
!         os.unlink(thumb)
!     except (IOError, OSError):
!         pass
!     if not filename.endswith('.jpg'):
          return None
!     thumb = vfs.getoverlay(filename[:-3] + 'thumb.jpg')
!     try:
!         if fileops.mtime(thumb) > fileops.mtime(filename):
!             return thumb
!         os.unlink(thumb)
!     except (IOError, OSError):
!         pass
      return None
  
  
! def create(filename):
      """
      Create a thumbnail for the given filename in the vfs.
      """
!     if not os.path.isdir(os.path.dirname(vfs.getoverlay(filename))):
          os.makedirs(os.path.dirname(vfs.getoverlay(filename)))
!     if filename.endswith('.jpg'):
!         thumb = vfs.getoverlay(filename[:-3] + 'thumb.jpg')
!         # epeg support for fast jpg thumbnailing
!         return mevas.imagelib.thumbnail(filename, thumb, (255, 255))
! 
!     thumb = vfs.getoverlay(filename + '.raw')
      try:
!         return create_raw_thumbnail(filename, thumb)
!     except Exception, e:
!         log.error('thumbnail.create error:\n%s' % e)
!         fileops.touch(thumb)
          return None
  
  
! def load(filename):
      """
      Return the thumbnail. Create one, if it doesn't exists.
      """
!     thumb = get_name(filename)
      if thumb:
-         if thumb.endswith('raw'):
-             try:
-                 return read_raw_thumbnail(thumb)
-             except:
-                 return None
          return mevas.imagelib.open(thumb)
!     return create(filename)
! 
--- 60,113 ----
      Returns the filename of the thumbnail if it exists. None if not.
      """
!     s = os.stat(filename)
!     if s[stat.ST_SIZE] < 30000:
          return filename
!     base = filename[:filename.rfind('.')]
!     if base.endswith('.thumb'):
!         return filename
!     thumb = vfs.getoverlay(base + '.thumb' + filename[filename.rfind('.'):])
!     if not os.path.isfile(thumb):
          return None
! 
!     if os.stat(thumb)[stat.ST_MTIME] > s[stat.ST_MTIME]:
!         return thumb
!     os.unlink(thumb)
      return None
  
  
! def create(filename, check_overlay=True):
      """
      Create a thumbnail for the given filename in the vfs.
      """
!     if check_overlay and \
!            not os.path.isdir(os.path.dirname(vfs.getoverlay(filename))):
          os.makedirs(os.path.dirname(vfs.getoverlay(filename)))
!     base  = filename[:filename.rfind('.')]
!     thumb = vfs.getoverlay(base + '.thumb' + filename[filename.rfind('.'):])
!     if filename in _create_jobs:
!         _create_jobs.remove(filename)
!         if _create_jobs:
!             call_later(10, create, _create_jobs[0])
      try:
!         return mevas.imagelib.thumbnail(filename, thumb, (255, 255))
!     except:
          return None
  
  
! def load(filename, bg=False):
      """
      Return the thumbnail. Create one, if it doesn't exists.
      """
!     try:
!         thumb = get_name(filename)
!     except OSError:
!         return None
      if thumb:
          return mevas.imagelib.open(thumb)
!     if not bg:
!         return create(filename)
!     if filename in _create_jobs:
!         return
!     _create_jobs.append(filename)
!     call_later(10, create, _create_jobs[0])
!     return None



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to