Пример #1
0
def www_image(filename, subdir, force=False, getsize=False, size=128, verbose=0):
    """
    creates a webserver image image and returns its size.
    """
    orientation_map = {
        #   vflip  hflip  orientate
        1: (False, False, 0, 'Horizontal (normal)'),
        2: (False, True,  0, 'Mirrored horizontal'),
        3: (False, False, 2, 'Rotated 180'),
        4: (True,  False, 0, 'Mirrored vertical'),
        5: (False, True,  3, 'Mirrored horizontal then rotated 90 CCW'),
        6: (False, False, 1, 'Rotated 90 CW'),
        7: (False, True,  1, 'Mirrored horizontal then rotated 90 CW'),
        8: (False, False, 3, 'Rotated 90 CCW'),
    }

    imagename = www_image_path(filename, subdir)
    if verbose >= 2:
        print 'Checking image %r, force=%r getsize=%r' % (imagename, force, getsize)
    if os.path.exists(imagename):
        if not force:
            if getsize:
                image = kaa.imlib2.open(imagename)
                return image.size
            return (0, 0)
    else:
        force = True
    try:
        try:
            if not os.path.isdir(os.path.dirname(imagename)):
                if verbose >= 1:
                    print 'Make directory %r' % os.path.dirname(imagename)
                os.makedirs(os.path.dirname(imagename), mode=04775)
        except (OSError, IOError), why:
            logger.warning('error creating dir %s: %s', os.path.dirname(imagename), why)
            raise
        if force or os.stat(filename)[ST_MTIME] > os.stat(imagename)[ST_MTIME]:
            orientation_str = ''
            image = kaa.imlib2.open(filename)
            try:
                tags = exif.process_file(open(filename, 'rb'))
                orientation = tags.get('Image Orientation').values[0]
                flip_vert, flip_hori, orientate, orientate_str = orientation_map[orientation]
                if orientation != 1:
                    if flip_vert:
                        image.flip_vertical()
                        if verbose >= 2:
                            print 'flipped vertically'
                    if flip_hori:
                        image.flip_horizontal()
                        if verbose >= 2:
                            print 'flipped horizontally'
                    if orientate:
                        if verbose >= 2:
                            print orientate_str
                        image.orientate(orientate)
            except AttributeError, why:
                pass
            except Exception, why:
                print why
Пример #2
0
def format_image(settings, item, width, height, force=0, anamorphic=0):
    try:
        type = item.display_type
    except:
        try:
            type = item.info['mime'].replace('/', '_')
        except:
            type = item.type

    cname = '%s-%s-%s-%s-%s-%s-%s' % (settings.icon_dir, item.image, type,
                                      item.type, width, height, force)

    if item['rotation']:
        cname = '%s-%s' % (cname, item['rotation'])

    if item.media and item.media.item == item:
        cname = '%s-%s' % (cname, item.media)

    cimage = format_imagecache[cname]

    if cimage:
        return cimage

    image = None
    imagefile = None

    if item.image:
        if isinstance(item.image, imlib2.Image):
            image = osd.loadbitmap(item.image)
        else:
            image = load_imagecache['thumb://%s' % item.image]
            if not image:
                image = osd.loadbitmap('thumb://%s' % item.image)
                load_imagecache['thumb://%s' % item.image] = image

        if not item['rotation']:
            try:
                f = open(item.filename, 'rb')
                tags = exif.process_file(f)
                f.close()
                if tags.has_key('Image Orientation'):
                    orientation = tags['Image Orientation']
                    if str(orientation) == "Rotated 90 CCW":
                        item['rotation'] = 90
                    elif str(orientation) == "Rotated 180":
                        item['rotation'] = 180
                    elif str(orientation) == "Rotated 90 CW":
                        item['rotation'] = 270
            except Exception, e:
                pass

        if image and item['rotation']:
            image = pygame.transform.rotate(image, item['rotation'])
Пример #3
0
    cache image for faster access
    """
    logger.log( 9, 'create_thumbnail(filename=%r, thumbnail=%r)', filename, thumbnail)
    thumb = vfs.getoverlay(filename + '.raw')
    image = None

    if thumbnail:
        try:
            image = kaa.imlib2.open_from_memory(thumbnail)
        except Exception, why:
            logger.info('invalid thumbnail %r: %s', filename, why)

    if not image:
        if __freevo_app__ == 'main':
            try:
                tags = exif.process_file(open(filename, 'rb'))
                if tags.has_key('JPEGThumbnail'):
                    image = kaa.imlib2.open_from_memory(tags['JPEGThumbnail'])
            except Exception, why:
                logger.warning('Error loading thumbnail %r: %s', filename, why)

        if not image or image.width < 100 or image.height < 100:
            try:
                image = kaa.imlib2.open(filename)
            except Exception, why:
                logger.warning('Cannot cache image %r: %s', filename, why)
                return None

    try:
        if image.width > 255 or image.height > 255:
            image.thumbnail((255,255))
Пример #4
0
def format_image(settings,
                 item,
                 width,
                 height,
                 force=False,
                 anamorphic=False,
                 flush_large_image=False):
    logger.log(
        9,
        'format_image(settings=%r, item=%r, width=%r, height=%r, force=%r, anamorphic=%r, flush_large_image=%r)',
        settings, item, width, height, force, anamorphic, flush_large_image)

    cname = generate_cache_key(settings, item, width, height, force)
    cimage = format_imagecache[cname]

    if cimage:
        return cimage

    try:
        type = item.display_type
    except AttributeError:
        try:
            type = item.info['mime'].replace('/', '_')
        except:
            type = item.type
    if type is None:
        type = ''

    image = None
    imagefile = None

    if item.image:
        if isinstance(item.image, imlib2.Image):
            image = osd.loadbitmap(item.image)
        else:
            if not (item.image.startswith('http://')
                    or item.image.startswith('https://')):
                if not os.path.exists(str(item.image)):
                    return None, 0, 0

        image = load_imagecache[item.image]
        if not image:
            image = load_imagecache['thumb://%s' % item.image]

        if not image:
            if item.image.startswith('http://') or item.image.startswith(
                    'https://'):
                image = osd.loadbitmap(item.image)
                load_imagecache[imagefile] = image
            else:
                for folder in ('.thumbs', '.images'):
                    image_parts = os.path.split(str(item.image))
                    ifile = os.path.join(image_parts[0], folder,
                                         image_parts[1])
                    if os.path.exists(ifile):
                        imagefile = ifile
                        break

                if imagefile:
                    image = osd.loadbitmap(imagefile)
                    imagefile = 'thumb://%s' % item.image
                    load_imagecache[imagefile] = image
                else:
                    imagefile = item.image
                    f = open(imagefile, 'rb')
                    tags = exif.process_file(f)
                    f.close()
                    if 'JPEGThumbnail' in tags:
                        sio = StringIO.StringIO(tags['JPEGThumbnail'])
                        image = pygame.image.load(sio)
                        sio.close()
                        imagefile = 'thumb://%s' % item.image
                        load_imagecache[imagefile] = image
                    else:
                        image = osd.loadbitmap(imagefile)
                        load_imagecache[imagefile] = image

            # DJW need to skip this code if the image is from .thumbs or .images as
            # the image has already been rotated in the cache.
            if not item['rotation']:
                try:
                    f = open(imagefile, 'rb')
                    tags = exif.process_file(f)
                    f.close()
                    if tags.has_key('Image Orientation'):
                        orientation = tags['Image Orientation']
                        logger.debug('%s orientation=%s', item['name'],
                                     tags['Image Orientation'])
                        if str(orientation) == "Rotated 90 CCW":
                            item['rotation'] = 270
                        elif str(orientation) == "Rotated 180":
                            item['rotation'] = 180
                        elif str(orientation) == "Rotated 90 CW":
                            item['rotation'] = 90
                except Exception, e:
                    logger.info('%s', e)

            if image and item['rotation']:
                # pygame reverses the image rotation
                if config.IMAGEVIEWER_REVERSED_IMAGES:
                    rotation = item['rotation']
                else:
                    rotation = (360 - item['rotation']) % 360
                image = pygame.transform.rotate(image, rotation)
                load_imagecache[imagefile] = image
Пример #5
0
    thumb = vfs.getoverlay(filename + '.raw')
    image = None

    if thumbnail:
        try:
            image = imlib2.open_from_memory(thumbnail)
        except Exception, e:
            print 'Invalid thumbnail for %s' % filename
            if config.DEBUG:
                print 'invalid thumbnail:', e

    if not image:
        if __freevo_app__ == 'main':
            try:
                f = open(filename, 'rb')
                tags = exif.process_file(f)
                f.close()

                if tags.has_key('JPEGThumbnail'):
                    image = imlib2.open_from_memory(tags['JPEGThumbnail'])
            except Exception, e:
                print 'Error loading thumbnail %s' % filename
                if config.DEBUG:
                    print 'create_thumbnail:', e

        if not image or image.width < 100 or image.height < 100:
            try:
                image = imlib2.open(filename)
            except Exception, e:
                print 'error caching image %s' % filename
                if config.DEBUG:
Пример #6
0
def format_image(settings, item, width, height, force=0, anamorphic=0):
    try:
        type = item.display_type
    except:
        try:
            type = item.info['mime'].replace('/', '_')
        except:
            type = item.type


    if isinstance(item.image, str):
        item_image=Unicode(item.image)
    else:
        item_image=item.image

    cname = '%s-%s-%s-%s-%s-%s-%s' % (settings.icon_dir, item_image, type,
                                      item.type, width, height, force)

    if hasattr(item, 'rotation') and item['rotation']:
        cname = '%s-%s' % (cname, item['rotation'])

    if item.media and item.media.item == item:
        cname = '%s-%s' % (cname, item.media)

    cimage = format_imagecache[cname]

    if cimage:
        return cimage

    image     = None
    imagefile = None

    if item.image:
        if isinstance(item.image, imlib2.Image):
            image = osd.loadbitmap(item.image)
        else:
            #print 'thumb://%r %r' % (item.image, item_image)
            image = load_imagecache['thumb://%s' % item.image]
            if not image:
                image = osd.loadbitmap('thumb://%s' % item.image)
                load_imagecache['thumb://%s' % item.image] = image

        if not item['rotation']:
            try:
                f=open(item.image, 'rb')
                tags=exif.process_file(f)
                f.close()
                if tags.has_key('Image Orientation'):
                    orientation = tags['Image Orientation']
                    _debug_('%s orientation=%s' % (item['name'], tags['Image Orientation']))
                    if str(orientation) == "Rotated 90 CCW":
                        item['rotation'] = 270
                    elif str(orientation) == "Rotated 180":
                        item['rotation'] = 180
                    elif str(orientation) == "Rotated 90 CW":
                        item['rotation'] = 90
            except Exception, e:
                _debug_('%s' % (e), DINFO)

        if image and item['rotation']:
            # pygame reverses the image rotation
            if config.IMAGEVIEWER_REVERSED_IMAGES:
                rotation = (360 - item['rotation']) % 360
            else:
                rotation = item['rotation']
            image = pygame.transform.rotate(image, rotation)
Пример #7
0
def format_image(settings, item, width, height, force=False, anamorphic=False, flush_large_image=False, get_exif_thumbnail=True):
    logger.log( 9, 'format_image(settings=%r, item=%r, width=%r, height=%r, force=%r, anamorphic=%r, flush_large_image=%r, get_exif_thumbnail=%r)', 
        settings, item, width, height, force, anamorphic, flush_large_image, get_exif_thumbnail)

    cname = generate_cache_key(settings, item, width, height, force)
    cimage = format_imagecache[cname]

    if cimage:
        return cimage

    try:
        type = item.display_type
    except AttributeError:
        try:
            type = item.info['mime'].replace('/', '_')
        except:
            type = item.type
    if type is None:
        type = ''

    image     = None
    imagefile = None

    if item.image:
        if isinstance(item.image, imlib2.Image):
            image = osd.loadbitmap(item.image)
        else:
            if not (item.image.startswith('http://') or item.image.startswith('https://')):
                if not os.path.exists(str(item.image)):
                    return None, 0, 0
        
        image = load_imagecache[item.image]
        if not image:
            image = load_imagecache['thumb://%s' % item.image]

        if not image:
            if item.image.startswith('http://') or item.image.startswith('https://'):
                image = osd.loadbitmap(item.image)
                load_imagecache[imagefile] = image
            else:
                for folder in ('.thumbs', '.images'):
                    image_parts = os.path.split(str(item.image))
                    ifile = os.path.join(image_parts[0], folder, image_parts[1])
                    if os.path.exists(ifile):
                        imagefile = ifile
                        break

                if imagefile:
                    image = osd.loadbitmap(imagefile)
                    imagefile = 'thumb://%s' % item.image
                    load_imagecache[imagefile] = image
                else:
                    imagefile = item.image
                    f = open(imagefile, 'rb')
                    tags = exif.process_file(f)
                    f.close()
                    if 'JPEGThumbnail' in tags:
                        sio = StringIO.StringIO(tags['JPEGThumbnail'])
                        image = pygame.image.load(sio)
                        sio.close()
                        imagefile = 'thumb://%s' % item.image
                        load_imagecache[imagefile] = image
                    else:
                        image = osd.loadbitmap(imagefile)
                        load_imagecache[imagefile] = image
                    


            # DJW need to skip this code if the image is from .thumbs or .images as
            # the image has already been rotated in the cache.
            if not item['rotation']:
                try:
                    f = open(imagefile, 'rb')
                    tags = exif.process_file(f)
                    f.close()
                    if tags.has_key('Image Orientation'):
                        orientation = tags['Image Orientation']
                        logger.debug('%s orientation=%s', item['name'], tags['Image Orientation'])
                        if str(orientation) == "Rotated 90 CCW":
                            item['rotation'] = 270
                        elif str(orientation) == "Rotated 180":
                            item['rotation'] = 180
                        elif str(orientation) == "Rotated 90 CW":
                            item['rotation'] = 90
                except Exception, e:
                    logger.info('%s', e)

            if image and item['rotation']:
                # pygame reverses the image rotation
                if config.IMAGEVIEWER_REVERSED_IMAGES:
                    rotation = (360 - item['rotation']) % 360
                else:
                    rotation = item['rotation']
                image = pygame.transform.rotate(image, rotation)
                load_imagecache[imagefile] = image
Пример #8
0
def www_image(filename,
              subdir,
              force=False,
              getsize=False,
              size=128,
              verbose=0):
    """
    creates a webserver image image and returns its size.
    """
    orientation_map = {
        #   vflip  hflip  orientate
        1: (False, False, 0, 'Horizontal (normal)'),
        2: (False, True, 0, 'Mirrored horizontal'),
        3: (False, False, 2, 'Rotated 180'),
        4: (True, False, 0, 'Mirrored vertical'),
        5: (False, True, 3, 'Mirrored horizontal then rotated 90 CCW'),
        6: (False, False, 1, 'Rotated 90 CW'),
        7: (False, True, 1, 'Mirrored horizontal then rotated 90 CW'),
        8: (False, False, 3, 'Rotated 90 CCW'),
    }

    imagename = www_image_path(filename, subdir)
    if verbose >= 2:
        print 'Checking image %r, force=%r getsize=%r' % (imagename, force,
                                                          getsize)
    if os.path.exists(imagename):
        if not force:
            if getsize:
                image = kaa.imlib2.open(imagename)
                return image.size
            return (0, 0)
    else:
        force = True
    try:
        try:
            if not os.path.isdir(os.path.dirname(imagename)):
                if verbose >= 1:
                    print 'Make directory %r' % os.path.dirname(imagename)
                os.makedirs(os.path.dirname(imagename), mode=04775)
        except (OSError, IOError), why:
            logger.warning('error creating dir %s: %s',
                           os.path.dirname(imagename), why)
            raise
        if force or os.stat(filename)[ST_MTIME] > os.stat(imagename)[ST_MTIME]:
            orientation_str = ''
            image = kaa.imlib2.open(filename)
            try:
                tags = exif.process_file(open(filename, 'rb'))
                orientation = tags.get('Image Orientation').values[0]
                flip_vert, flip_hori, orientate, orientate_str = orientation_map[
                    orientation]
                if orientation != 1:
                    if flip_vert:
                        image.flip_vertical()
                        if verbose >= 2:
                            print 'flipped vertically'
                    if flip_hori:
                        image.flip_horizontal()
                        if verbose >= 2:
                            print 'flipped horizontally'
                    if orientate:
                        if verbose >= 2:
                            print orientate_str
                        image.orientate(orientate)
            except AttributeError, why:
                pass
            except Exception, why:
                print why
Пример #9
0
    """
    logger.log(9, 'create_thumbnail(filename=%r, thumbnail=%r)', filename,
               thumbnail)
    thumb = vfs.getoverlay(filename + '.raw')
    image = None

    if thumbnail:
        try:
            image = kaa.imlib2.open_from_memory(thumbnail)
        except Exception, why:
            logger.info('invalid thumbnail %r: %s', filename, why)

    if not image:
        if __freevo_app__ == 'main':
            try:
                tags = exif.process_file(open(filename, 'rb'))
                if tags.has_key('JPEGThumbnail'):
                    image = kaa.imlib2.open_from_memory(tags['JPEGThumbnail'])
            except Exception, why:
                logger.warning('Error loading thumbnail %r: %s', filename, why)

        if not image or image.width < 100 or image.height < 100:
            try:
                image = kaa.imlib2.open(filename)
            except Exception, why:
                logger.warning('Cannot cache image %r: %s', filename, why)
                return None

    try:
        if image.width > 255 or image.height > 255:
            image.thumbnail((255, 255))