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

Modified Files:
        __init__.py fileops.py mediainfo.py 
Added Files:
        thumbnail.py 
Log Message:
move thumbnail to extra file

Index: mediainfo.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/util/mediainfo.py,v
retrieving revision 1.59
retrieving revision 1.60
diff -C2 -d -r1.59 -r1.60
*** mediainfo.py        28 Aug 2004 17:17:05 -0000      1.59
--- mediainfo.py        7 Sep 2004 18:52:51 -0000       1.60
***************
*** 10,13 ****
--- 10,16 ----
  # -----------------------------------------------------------------------
  # $Log$
+ # Revision 1.60  2004/09/07 18:52:51  dischi
+ # move thumbnail to extra file
+ #
  # Revision 1.59  2004/08/28 17:17:05  dischi
  # force rechecking if it seems a dvd but is not detected as one
***************
*** 73,76 ****
--- 76,80 ----
  import eventhandler
  import rc
+ import thumbnail
  
  class FileOutdatedException(Exception):
***************
*** 335,341 ****
          info = mmpython.Factory().create(filename)
          if info:
!             thumbnail = None
              if info.has_key('thumbnail'):
!                 thumbnail = info.thumbnail
                  
              info = self.simplify(info)
--- 339,345 ----
          info = mmpython.Factory().create(filename)
          if info:
!             thumbnail_file = None
              if info.has_key('thumbnail'):
!                 thumbnail_file = info.thumbnail
                  
              info = self.simplify(info)
***************
*** 353,362 ****
                              info[variable] = video[variable]
  
!             if thumbnail and config.IMAGE_USE_EXIF_THUMBNAIL and config.CACHE_IMAGES:
!                 util.cache_image(filename, thumbnail)
              elif config.CACHE_IMAGES and info.has_key('mime') and info['mime'] and \
                       info['mime'].startswith('image'):
!                 util.cache_image(filename)
! 
              return info
          return {}
--- 357,367 ----
                              info[variable] = video[variable]
  
!             if thumbnail_file and config.IMAGE_USE_EXIF_THUMBNAIL and 
config.CACHE_IMAGES:
!                 if not thumbnail.get_name(filename):
!                     thumbnail.create(filename)
              elif config.CACHE_IMAGES and info.has_key('mime') and info['mime'] and \
                       info['mime'].startswith('image'):
!                 if not thumbnail.get_name(filename):
!                     thumbnail.create(filename)
              return info
          return {}

Index: fileops.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/util/fileops.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** fileops.py  29 Aug 2004 18:37:05 -0000      1.26
--- fileops.py  7 Sep 2004 18:52:51 -0000       1.27
***************
*** 10,13 ****
--- 10,16 ----
  # -----------------------------------------------------------------------
  # $Log$
+ # Revision 1.27  2004/09/07 18:52:51  dischi
+ # move thumbnail to extra file
+ #
  # Revision 1.26  2004/08/29 18:37:05  dischi
  # epeg support for fast jpg thumbnailing
***************
*** 59,62 ****
--- 62,66 ----
  import fnmatch
  import traceback
+ import mevas
  
  # image stuff
***************
*** 65,73 ****
  from mmpython.image import EXIF as exif
  
- try:
-     import epeg
- except ImportError:
-     _debug_('epeg not found')
-     
  if float(sys.version[0:3]) < 2.3:
      PICKLE_PROTOCOL = 1
--- 69,72 ----
***************
*** 466,563 ****
  #
  
- def read_thumbnail(filename):
-     """
-     return image cached inside filename
-     """
-     f = open(filename)
-     header = f.read(10)
-     if not header[:3] == 'FRI':
-         return read_pickle(filename)
-     data = f.read(), (ord(header[3]), ord(header[4])), header[5:].strip(' ')
-     f.close()
-     return data
- 
- 
- def create_thumbnail(filename, thumbnail=None):
-     """
-     cache image for faster access
-     """
-     thumb = vfs.getoverlay(filename + '.raw')
-     image = None
- 
-     if thumbnail:
-         try:
-             image = Image.open(cStringIO.StringIO(thumbnail))
-         except Exception, e:
-             _debug_('Invalid thumbnail for %s' % filename, 0)
-             if config.DEBUG:
-                 print e
- 
-     if not image:
-         try:
-             # epeg support for fast jpg thumbnailing
-             return epeg.fri_thumbnail(filename, thumb)
-         except:
-             pass
- 
-         if __freevo_app__ == 'main':
-             try:
-                 f=open(filename, 'rb')
-                 tags=exif.process_file(f)
-                 f.close()
-                 
-                 if tags.has_key('JPEGThumbnail'):
-                     image = Image.open(cStringIO.StringIO(tags['JPEGThumbnail']))
-             except Exception, e:
-                 print 'Error loading thumbnail %s' % filename
-                 if config.DEBUG:
-                     print e
- 
-         if not image or image.size[0] < 100 or image.size[1] < 100:
-             try:
-                 image = Image.open(filename)
-             except Exception, e:
-                 print 'error caching image %s' % filename
-                 if config.DEBUG:
-                     print e
-                 return None
-         
-     try:
-         if image.size[0] > 255 or image.size[1] > 255:
-             image.thumbnail((255,255), Image.ANTIALIAS)
- 
-         if image.mode == 'P':
-             image = image.convert('RGB')
- 
-         data = (image.tostring(), image.size, image.mode)
- 
-         f = vfs.open(thumb, 'w')
-         f.write('FRI%s%s%5s' % (chr(image.size[0]), chr(image.size[1]), image.mode))
-         f.write(data[0])
-         f.close()
-         return data
-     except Exception, e:
-         print 'error caching image %s' % filename
-         if config.DEBUG:
-             print e
-         return None
-         
- 
- def cache_image(filename, thumbnail=None, use_exif=False):
-     """
-     cache image for faster access, return cached image
-     """
-     thumb = vfs.getoverlay(filename + '.raw')
-     try:
-         if os.stat(thumb)[stat.ST_MTIME] > os.stat(filename)[stat.ST_MTIME]:
-             data = read_thumbnail(thumb)
-             if data:
-                 return data
-     except OSError:
-         pass
- 
-     data = create_thumbnail(filename, thumbnail)
-     if not data:
-         # epeg
-         return read_thumbnail(filename)
-     
--- 465,466 ----

Index: __init__.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/util/__init__.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** __init__.py 26 Aug 2004 15:30:39 -0000      1.19
--- __init__.py 7 Sep 2004 18:52:51 -0000       1.20
***************
*** 10,13 ****
--- 10,16 ----
  # -----------------------------------------------------------------------
  # $Log$
+ # Revision 1.20  2004/09/07 18:52:51  dischi
+ # move thumbnail to extra file
+ #
  # Revision 1.19  2004/08/26 15:30:39  dischi
  # add weakref
***************
*** 92,93 ****
--- 95,97 ----
  
      import mediainfo
+     import thumbnail

--- NEW FILE: thumbnail.py ---
# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# thumbnail.py - Thumbnail functions
# -----------------------------------------------------------------------
# $Id: thumbnail.py,v 1.1 2004/09/07 18:52:51 dischi Exp $
#
# The file includes functions to create and load thumbnails for images.
# The thumbnails itself are stored in the vfs
#
# For thumbnail creation, pyepeg will be used for fast jpeg thumbnails,
# other file types will be handled by the imlib2 backend of mevas. All
# thumbnails are stored at 255x255 pixel (keeping the aspect ratio).
#
# Notes:
# Todo:        
#
# -----------------------------------------------------------------------
# $Log: thumbnail.py,v $
# Revision 1.1  2004/09/07 18:52:51  dischi
# move thumbnail to extra file
#
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */

__all__ = ( 'create', 'get_name', 'load' )

import os
import stat

# mevas for imlib2 support
import mevas

try:
    # pyepeg for fast jpg thumbnails
    import epeg
except ImportError:
    _debug_('epeg not found')

# pickle support
from fileops import read_pickle, save_pickle, touch

# vfs supoort for saving the thumbnail
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 read_pickle(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


def get_name(filename):
    """
    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 os.stat(thumb)[stat.ST_MTIME] > os.stat(filename)[stat.ST_MTIME]:
            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 os.stat(thumb)[stat.ST_MTIME] > os.stat(filename)[stat.ST_MTIME]:
            return thumb
        os.unlink(thumb)
    except (IOError, OSError):
        pass
    return None
    

def create(filename):
    """
    Create a thumbnail for the given filename in the vfs.
    """
    print 'create', filename
    if filename.endswith('.jpg'):
        thumb = vfs.getoverlay(filename[:-3] + 'thumb.jpg')
        try:
            # epeg support for fast jpg thumbnailing
            epeg.jpg_thumbnail(filename, thumb, 255, 255)
            return mevas.imagelib.open(thumb)
        except Exception, e:
            _debug_(e)

    thumb = vfs.getoverlay(filename + '.raw')
    try:
        return create_raw_thumbnail(filename, thumb)
    except Exception, e:
        _debug_(e)
        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)




-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to