Exemplo n.º 1
0
 def derivative_image(master, width, height):
     if not master.file_exists():
         logging.error(
             'Image derivative failed for media %d, cannot find file "%s"'
             % (master.id, master.get_absolute_file_path()))
         return None, (None, None)
     import ImageFile
     ImageFile.MAXBLOCK = 16 * 1024 * 1024
     from multimedia import get_image
     try:
         file = get_image(master)
         image = Image.open(file)
         if crop_to_square:
             w, h = image.size
             if w > h:
                 image = image.crop(
                     ((w - h) / 2, 0, (w - h) / 2 + h, h))
             elif w < h:
                 image = image.crop(
                     (0, (h - w) / 2, w, (h - w) / 2 + w))
         image.thumbnail((width, height), Image.ANTIALIAS)
         output = StringIO.StringIO()
         if image.mode != "RGB":
             image = image.convert("RGB")
         image.save(output, 'JPEG', quality=85, optimize=True)
         return output.getvalue(), image.size
     except Exception, e:
         logging.error('Image derivative failed for media %d (%s)' %
                       (master.id, e))
         return None, (None, None)
Exemplo n.º 2
0
 def derivative_image(master, width, height):
     if not master.file_exists():
         logging.error('Image derivative failed for media %d, '
                       'cannot find file "%s"' %
                       (master.id, master.get_absolute_file_path()))
         return None, (None, None)
     from PIL import ImageFile
     ImageFile.MAXBLOCK = 16 * 1024 * 1024
     # Import here to avoid circular reference
     # TODO: need to move all these functions out of __init__.py
     from multimedia import get_image, overlay_image_with_mimetype_icon
     try:
         file = get_image(master)
         image = Image.open(file)
         if crop_to_square:
             w, h = image.size
             if w > h:
                 image = image.crop(
                     ((w - h) / 2, 0, (w - h) / 2 + h, h))
             elif w < h:
                 image = image.crop(
                     (0, (h - w) / 2, w, (h - w) / 2 + w))
         image.thumbnail((width, height), Image.ANTIALIAS)
         image = overlay_image_with_mimetype_icon(
             image, master.mimetype)
         output = StringIO.StringIO()
         if image.mode != "RGB":
             image = image.convert("RGB")
         image.save(output, 'JPEG', quality=85, optimize=True)
         return output.getvalue(), image.size
     except Exception, e:
         logging.exception('Image derivative failed for media %d (%s)' %
                           (master.id, e))
         return None, (None, None)
Exemplo n.º 3
0
def derivative_image(master, width, height, crop_sq=False):
    if not master.file_exists():
        log.error('Image derivative failed for media %d, cannot find file "%s"' % (
            master.id, master.get_absolute_file_path()))
        return None, (None, None)
    from PIL import ImageFile
    ImageFile.MAXBLOCK = 16 * 1024 * 1024
    # Import here to avoid circular reference
    from multimedia import get_image, overlay_image_with_mimetype_icon
    try:
        file = get_image(master)
        image = Image.open(file)
        if crop_sq:
            w, h = image.size
            if w > h:
                image = image.crop(((w - h) / 2, 0, (w - h) / 2 + h, h))
            elif w < h:
                image = image.crop((0, (h - w) / 2, w, (h - w) / 2 + w))
        image.thumbnail((width, height), Image.ANTIALIAS)
        image = overlay_image_with_mimetype_icon(image, master.mimetype)
        output = StringIO.StringIO()
        if image.mode != "RGB":
            image = image.convert("RGB")
        image.save(output, 'JPEG', quality=85, optimize=True)
        return output.getvalue(), image.size
    except Exception, e:
        log.error('Image derivative failed for media %d (%s)' % (master.id, e))
        return None, (None, None)
Exemplo n.º 4
0
 def derivative_image(master, width, height):
     if not master.file_exists():
         print('Image derivative failed for media %d, cannot find file "%s"' % (master.id, master.get_absolute_file_path()))
         logging.error('Image derivative failed for media %d, cannot find file "%s"' % (master.id, master.get_absolute_file_path()))
         return None, (None, None)
     from PIL import ImageFile
     ImageFile.MAXBLOCK = 16 * 1024 * 1024
     from multimedia import get_image
     try:
         file = get_image(master)
         image = Image.open(file)
         if crop_to_square:
             w, h = image.size
             if w > h:
                 image = image.crop(((w - h) / 2, 0, (w - h) / 2 + h, h))
             elif w < h:
                 image = image.crop((0, (h - w) / 2, w, (h - w) / 2 + w))
         image.thumbnail((width, height), Image.ANTIALIAS)
         output = StringIO.StringIO()
         if image.mode != "RGB":
             image = image.convert("RGB")
         print 'line 154 storage.__init__'
         image.save(output, 'JPEG', quality=85, optimize=True)
         #print 'output get value=' + str(output.getvalue())
         if not image:
             print 'image is null'
         else: 
             print 'image.size=' + str(image.size)
         return output.getvalue(), image.size
     except Exception, e:
         logging.error('Image derivative failed for media %d (%s)' % (master.id, e))
         return None, (None, None)
Exemplo n.º 5
0
 def derivative_image(master, width, height):
     if not master.file_exists():
         logging.error('Image derivative failed for media %d, cannot find file' % master.id)
         return None, (None, None)
     import ImageFile
     ImageFile.MAXBLOCK = 16 * 1024 * 1024
     from multimedia import get_image
     try:
         file = get_image(master)
         image = Image.open(file)
         if crop_to_square:
             w, h = image.size
             if w > h:
                 image = image.crop(((w - h) / 2, 0, (w - h) / 2 + h, h))
             elif w < h:
                 image = image.crop((0, (h - w) / 2, w, (h - w) / 2 + w))
         image.thumbnail((width, height), Image.ANTIALIAS)
         output = StringIO.StringIO()
         image.save(output, 'JPEG', quality=85, optimize=True)
         return output.getvalue(), image.size
     except Exception, e:
         logging.error('Image derivative failed for media %d (%s)' % (master.id, e))
         return None, (None, None)