Exemplo n.º 1
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        thumbnail_size = APP_PREVIEW_SIZES[0][:2]
        image_size = APP_PREVIEW_SIZES[1][:2]
        with storage.open(src, 'rb') as fp:
            size = Image.open(fp).size
        if size[0] > size[1]:
            # If the image is wider than tall, then reverse the wanted size
            # to keep the original aspect ratio while still resizing to
            # the correct dimensions.
            thumbnail_size = thumbnail_size[::-1]
            image_size = image_size[::-1]

        sizes['thumbnail'] = resize_image(src,
                                          thumb_dst,
                                          thumbnail_size,
                                          remove_src=False)
        sizes['image'] = resize_image(src,
                                      full_dst,
                                      image_size,
                                      remove_src=False)
        instance.sizes = sizes
        instance.save()
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 2
0
def convert(directory, delete=False):
    print 'Converting icons in %s' % directory

    pks = []
    k = 0
    for path, names, filenames in walk_storage(directory):
        for filename in filenames:
            old = os.path.join(path, filename)
            pre, ext = os.path.splitext(old)
            if (pre[-3:] in size_suffixes or ext not in extensions):
                continue

            if not storage.size(old):
                print 'Icon %s is empty, ignoring.' % old
                continue

            for size, size_suffix in zip(sizes, size_suffixes):
                new = '%s%s%s' % (pre, size_suffix, '.png')
                if os.path.exists(new):
                    continue
                resize_image(old, new, (size, size), remove_src=False)

            if ext != '.png':
                pks.append(os.path.basename(pre))

            if delete:
                storage.delete(old)

            k += 1
            if not k % 1000:
                print "... converted %s" % k

    for chunk in chunked(pks, 100):
        Addon.objects.filter(pk__in=chunk).update(icon_type='image/png')
Exemplo n.º 3
0
def resize_icon(src, dst, sizes, locally=False, **kw):
    """Resizes addon icons."""
    log.info('[1@None] Resizing icon: %s' % dst)
    try:
        for s in sizes:
            size_dst = '%s-%s.png' % (dst, s)
            resize_image(src,
                         size_dst, (s, s),
                         remove_src=False,
                         locally=locally)
            pngcrush_image.delay(size_dst, **kw)

        if locally:
            with open(src) as fd:
                icon_hash = _hash_file(fd)
            os.remove(src)
        else:
            with storage.open(src) as fd:
                icon_hash = _hash_file(fd)
            storage.delete(src)

        log.info('Icon resizing completed for: %s' % dst)
        return {'icon_hash': icon_hash}
    except Exception, e:
        log.error("Error saving addon icon: %s; %s" % (e, dst))
Exemplo n.º 4
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        thumbnail_size = APP_PREVIEW_SIZES[0][:2]
        image_size = APP_PREVIEW_SIZES[1][:2]
        with storage.open(src, 'rb') as fp:
            size = Image.open(fp).size
        if size[0] > size[1]:
            # If the image is wider than tall, then reverse the wanted size
            # to keep the original aspect ratio while still resizing to
            # the correct dimensions.
            thumbnail_size = thumbnail_size[::-1]
            image_size = image_size[::-1]

        sizes['thumbnail'] = resize_image(src, thumb_dst,
                                          thumbnail_size,
                                          remove_src=False)
        sizes['image'] = resize_image(src, full_dst,
                                      image_size,
                                      remove_src=False)
        instance.sizes = sizes
        instance.save()
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 5
0
def convert(directory, delete=False):
    print 'Converting icons in %s' % directory

    pks = []
    k = 0
    for path, names, filenames in walk_storage(directory):
        for filename in filenames:
            old = os.path.join(path, filename)
            pre, ext = os.path.splitext(old)
            if (pre[-3:] in size_suffixes or ext not in extensions):
                continue

            if not storage.size(old):
                print 'Icon %s is empty, ignoring.' % old
                continue

            for size, size_suffix in zip(sizes, size_suffixes):
                new = '%s%s%s' % (pre, size_suffix, '.png')
                if os.path.exists(new):
                    continue
                resize_image(old, new, (size, size), remove_src=False)

            if ext != '.png':
                pks.append(os.path.basename(pre))

            if delete:
                storage.delete(old)

            k += 1
            if not k % 1000:
                print "... converted %s" % k

    for chunk in chunked(pks, 100):
        Addon.objects.filter(pk__in=chunk).update(icon_type='image/png')
Exemplo n.º 6
0
    def handle(self, *args, **options):
        from django.conf import settings
        from amo.utils import resize_image

        converted = 0
        for path in (settings.COLLECTIONS_ICON_PATH, settings.PREVIEWS_PATH):
            if not os.path.isdir(path):
                sys.exit("Can't read pics path: %s" % path)

            for root, dirs, files in os.walk(path):
                for file in files:
                    # Aside from 9 files missing extentsions entirely (!)
                    # everything is jpg or gif or png
                    if file[-4:] in ('.jpg', '.gif'):
                        name, _ = os.path.splitext(file)
                        oldfile = "%s/%s" % (root, file)
                        newfile = "%s/%s.png" % (root, name)

                        if os.path.isfile(newfile):
                            print "Removing pre-existing file: %s" % (newfile)
                            os.remove(newfile)

                        print "Converting %s to %s" % (oldfile, newfile)
                        try:
                            resize_image(oldfile, newfile)
                        except IOError, e:
                            print "ERROR: (%s => %s) %s" % (oldfile, newfile, e)
                        converted += 1
                        if converted % 100 == 0:
                            print "Converted %s images..." % converted

                    elif file.endswith('.png'):
                        pass
                    else:
                        print "Not sure what to do with: %s" % file
Exemplo n.º 7
0
def resize_icon(src, dst, **kw):
    """Resizes collection icons to 32x32"""
    log.info('[1@None] Resizing icon: %s' % dst)

    try:
        resize_image(src, dst, (32, 32))
    except Exception, e:
        log.error("Error saving collection icon: %s" % e)
Exemplo n.º 8
0
def resize_photo(src, dst, **kw):
    """Resizes userpics to 200x200"""
    task_log.info('[1@None] Resizing photo: %s' % dst)

    try:
        resize_image(src, dst, (200, 200))
    except Exception, e:
        task_log.error("Error saving userpic: %s" % e)
Exemplo n.º 9
0
def resize_icon(src, dst):
    """Resizes collection icons to 32x32"""
    log.info('[1@None] Resizing icon: %s' % dst)

    try:
        resize_image(src, dst, (32, 32))
    except Exception, e:
        log.error("Error saving collection icon: %s" % e)
Exemplo n.º 10
0
def resize_photo(src, dst):
    """Resizes userpics to 200x200"""
    task_log.info('[1@None] Resizing photo: %s' % dst)

    try:
        resize_image(src, dst, (200, 200))
    except Exception, e:
        task_log.error("Error saving userpic: %s" % e)
Exemplo n.º 11
0
def _resize_image(old_im):
    new_dest = tempfile.NamedTemporaryFile()
    new_dest.close()
    resize_image(old_im.name,
                 new_dest.name,
                 size=settings.INAPP_IMAGE_SIZE,
                 locally=True)
    return new_dest
Exemplo n.º 12
0
def resize_icon(src, dst, locally=False, **kw):
    """Resizes collection icons to 32x32"""
    log.info('[1@None] Resizing icon: %s' % dst)

    try:
        resize_image(src, dst, (32, 32), locally=locally)
        return True
    except Exception, e:
        log.error("Error saving collection icon: %s" % e)
Exemplo n.º 13
0
def resize_photo(src, dst, locally=False, **kw):
    """Resizes userpics to 200x200"""
    task_log.debug('[1@None] Resizing photo: %s' % dst)

    try:
        resize_image(src, dst, (200, 200), locally=locally)
        return True
    except Exception, e:
        task_log.error("Error saving userpic: %s" % e)
Exemplo n.º 14
0
def resize_photo(src, dst, locally=False, **kw):
    """Resizes userpics to 200x200"""
    task_log.debug("[1@None] Resizing photo: %s" % dst)

    try:
        resize_image(src, dst, (200, 200), locally=locally)
        return True
    except Exception, e:
        task_log.error("Error saving userpic: %s" % e)
Exemplo n.º 15
0
def resize_icon(src, dst, locally=False, **kw):
    """Resizes collection icons to 32x32"""
    log.info('[1@None] Resizing icon: %s' % dst)

    try:
        resize_image(src, dst, (32, 32), locally=locally)
        return True
    except Exception, e:
        log.error("Error saving collection icon: %s" % e)
Exemplo n.º 16
0
def test_resize_transparency():
    src = os.path.join(settings.ROOT, "apps", "amo", "tests", "images", "transparent.png")
    dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1]
    expected = src.replace(".png", "-expected.png")
    try:
        resize_image(src, dest, (32, 32), remove_src=False, locally=True)
        with open(dest) as dfh:
            with open(expected) as efh:
                assert dfh.read() == efh.read()
    finally:
        if os.path.exists(dest):
            os.remove(dest)
Exemplo n.º 17
0
def test_resize_transparency():
    src = os.path.join(settings.ROOT, 'apps', 'amo', 'tests', 'images',
                       'transparent.png')
    dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1]
    expected = src.replace('.png', '-expected.png')
    try:
        resize_image(src, dest, (32, 32), remove_src=False)
        with open(dest) as dfh:
            with open(expected) as efh:
                assert dfh.read() == efh.read()
    finally:
        if os.path.exists(dest):
            os.remove(dest)
Exemplo n.º 18
0
def test_resize_transparency():
    src = os.path.join(settings.ROOT, 'apps', 'amo', 'tests',
                       'images', 'transparent.png')
    dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1]
    expected = src.replace('.png', '-expected.png')
    try:
        resize_image(src, dest, (32, 32), remove_src=False)
        with open(dest) as dfh:
            with open(expected) as efh:
                assert dfh.read() == efh.read()
    finally:
        if os.path.exists(dest):
            os.remove(dest)
Exemplo n.º 19
0
def resize_icon(src, dst, size, **kw):
    """Resizes addon icons."""
    log.info("[1@None] Resizing icon: %s" % dst)
    try:
        if isinstance(size, list):
            for s in size:
                resize_image(src, "%s-%s.png" % (dst, s), (s, s), remove_src=False)
            os.remove(src)
        else:
            resize_image(src, dst, (size, size), remove_src=True)
        return True
    except Exception, e:
        log.error("Error saving addon icon: %s" % e)
Exemplo n.º 20
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = {}
    log.info("[1@None] Resizing preview and storing size: %s" % thumb_dst)
    try:
        sizes["thumbnail"] = resize_image(src, thumb_dst, amo.ADDON_PREVIEW_SIZES[0], remove_src=False)
        sizes["image"] = resize_image(src, full_dst, amo.ADDON_PREVIEW_SIZES[1], remove_src=False)
        instance.sizes = sizes
        instance.save()
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 21
0
def resize_preview(src, thumb_dst, full_dst, **kw):
    """Resizes preview images."""
    log.info('[1@None] Resizing preview: %s' % thumb_dst)
    try:
        # Generate the thumb.
        size = amo.ADDON_PREVIEW_SIZES[0]
        resize_image(src, thumb_dst, size, remove_src=False)

        # Resize the original.
        size = amo.ADDON_PREVIEW_SIZES[1]
        resize_image(src, full_dst, size, remove_src=True)
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 22
0
def resize_icon(src, dst, size, **kw):
    """Resizes addon icons."""
    log.info('[1@None] Resizing icon: %s' % dst)
    try:
        if isinstance(size, list):
            for s in size:
                resize_image(src, '%s-%s.png' % (dst, s), (s, s),
                             remove_src=False)
            os.remove(src)
        else:
            resize_image(src, dst, (size, size), remove_src=True)
        return True
    except Exception, e:
        log.error("Error saving addon icon: %s" % e)
Exemplo n.º 23
0
def resize_preview(src, thumb_dst, full_dst, **kw):
    """Resizes preview images."""
    log.info('[1@None] Resizing preview: %s' % thumb_dst)
    try:
        # Generate the thumb.
        size = amo.ADDON_PREVIEW_SIZES[0]
        resize_image(src, thumb_dst, size, remove_src=False)

        # Resize the original.
        size = amo.ADDON_PREVIEW_SIZES[1]
        resize_image(src, full_dst, size, remove_src=True)
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 24
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        sizes['thumbnail'] = resize_image(src, thumb_dst,
                                          amo.ADDON_PREVIEW_SIZES[0],
                                          remove_src=False)
        sizes['image'] = resize_image(src, full_dst,
                                      amo.ADDON_PREVIEW_SIZES[1],
                                      remove_src=False)
        instance.sizes = sizes
        instance.save()
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 25
0
def test_resize_transparency_for_P_mode_bug_1181221():
    # We had a monkeypatch that was added in
    # https://github.com/jbalogh/zamboni/commit/10340af6d1a64a16f4b9cade9faa69976b5b6da5  # noqa
    # which caused the issue in bug 1181221. Since then we upgraded Pillow, and
    # we don't need it anymore. We thus don't have this issue anymore.
    src = os.path.join(settings.ROOT, 'apps', 'amo', 'tests', 'images',
                       'icon64.png')
    dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1]
    expected = src.replace('.png', '-expected.png')
    try:
        resize_image(src, dest, (32, 32), remove_src=False, locally=True)
        with open(dest) as dfh:
            with open(expected) as efh:
                assert dfh.read() == efh.read()
    finally:
        if os.path.exists(dest):
            os.remove(dest)
Exemplo n.º 26
0
def test_resize_transparency_for_P_mode_bug_1181221():
    # We had a monkeypatch that was added in
    # https://github.com/jbalogh/zamboni/commit/10340af6d1a64a16f4b9cade9faa69976b5b6da5  # noqa
    # which caused the issue in bug 1181221. Since then we upgraded Pillow, and
    # we don't need it anymore. We thus don't have this issue anymore.
    src = os.path.join(settings.ROOT, 'apps', 'amo', 'tests',
                       'images', 'icon64.png')
    dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1]
    expected = src.replace('.png', '-expected.png')
    try:
        resize_image(src, dest, (32, 32), remove_src=False, locally=True)
        with open(dest) as dfh:
            with open(expected) as efh:
                assert dfh.read() == efh.read()
    finally:
        if os.path.exists(dest):
            os.remove(dest)
Exemplo n.º 27
0
def resize_icon(src, dst, size, locally=False, **kw):
    """Resizes addon icons."""
    log.info('[1@None] Resizing icon: %s' % dst)
    try:
        if isinstance(size, list):
            for s in size:
                resize_image(src, '%s-%s.png' % (dst, s), (s, s),
                             remove_src=False, locally=locally)
            if locally:
                os.remove(src)
            else:
                storage.delete(src)
        else:
            resize_image(src, dst, (size, size), remove_src=True,
                         locally=locally)
        return True
    except Exception, e:
        log.error("Error saving addon icon: %s" % e)
Exemplo n.º 28
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        sizes['thumbnail'] = resize_image(src, thumb_dst,
                                          amo.ADDON_PREVIEW_SIZES[0],
                                          remove_src=False)
        sizes['image'] = resize_image(src, full_dst,
                                      amo.ADDON_PREVIEW_SIZES[1],
                                      remove_src=False)
        instance.sizes = sizes
        instance.save()
        # Finally delete the temporary now useless source file.
        os.unlink(src)
        return True
    except Exception, e:
        log.error("Error saving preview: %s" % e)
Exemplo n.º 29
0
def resize_icon(src, dst, size, locally=False, **kw):
    """Resizes addon icons."""
    log.info('[1@None] Resizing icon: %s' % dst)
    try:
        if isinstance(size, list):
            for s in size:
                resize_image(src, '%s-%s.png' % (dst, s), (s, s),
                             remove_src=False, locally=locally)
            if locally:
                os.remove(src)
            else:
                storage.delete(src)
        else:
            resize_image(src, dst, (size, size), remove_src=True,
                         locally=locally)

        log.info('Icon resizing completed for: %s' % dst)
        return True
    except Exception, e:
        log.error("Error saving addon icon: %s; %s" % (e, dst))
Exemplo n.º 30
0
def resize_icon(src, dst, sizes, locally=False, **kw):
    """Resizes addon icons."""
    log.info('[1@None] Resizing icon: %s' % dst)
    try:
        for s in sizes:
            resize_image(src, '%s-%s.png' % (dst, s), (s, s),
                         remove_src=False, locally=locally)
        if locally:
            with open(src) as fd:
                icon_hash = _hash_file(fd)
            os.remove(src)
        else:
            with storage.open(src) as fd:
                icon_hash = _hash_file(fd)
            storage.delete(src)

        log.info('Icon resizing completed for: %s' % dst)
        return {'icon_hash': icon_hash}
    except Exception, e:
        log.error("Error saving addon icon: %s; %s" % (e, dst))
Exemplo n.º 31
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = instance.sizes or {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        thumbnail_size = APP_PREVIEW_SIZES[0][:2]
        image_size = APP_PREVIEW_SIZES[1][:2]
        with storage.open(src, 'rb') as fp:
            size = Image.open(fp).size
        if size[0] > size[1]:
            # If the image is wider than tall, then reverse the wanted size
            # to keep the original aspect ratio while still resizing to
            # the correct dimensions.
            thumbnail_size = thumbnail_size[::-1]
            image_size = image_size[::-1]

        if kw.get('generate_thumbnail', True):
            sizes['thumbnail'] = resize_image(src,
                                              thumb_dst,
                                              thumbnail_size,
                                              remove_src=False)
        if kw.get('generate_image', True):
            sizes['image'] = resize_image(src,
                                          full_dst,
                                          image_size,
                                          remove_src=False)
        instance.sizes = sizes
        instance.save()
        log.info('Preview resized to: %s' % thumb_dst)

        # Remove src file now that it has been processed.
        try:
            os.remove(src)
        except OSError:
            pass

        return True

    except Exception, e:
        log.error("Error saving preview: %s; %s" % (e, thumb_dst))
Exemplo n.º 32
0
def resize_preview(src, instance, **kw):
    """Resizes preview images and stores the sizes on the preview."""
    thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
    sizes = instance.sizes or {}
    log.info('[1@None] Resizing preview and storing size: %s' % thumb_dst)
    try:
        thumbnail_size = APP_PREVIEW_SIZES[0][:2]
        image_size = APP_PREVIEW_SIZES[1][:2]
        with storage.open(src, 'rb') as fp:
            size = Image.open(fp).size
        if size[0] > size[1]:
            # If the image is wider than tall, then reverse the wanted size
            # to keep the original aspect ratio while still resizing to
            # the correct dimensions.
            thumbnail_size = thumbnail_size[::-1]
            image_size = image_size[::-1]

        if kw.get('generate_thumbnail', True):
            sizes['thumbnail'] = resize_image(src, thumb_dst,
                                              thumbnail_size,
                                              remove_src=False)
        if kw.get('generate_image', True):
            sizes['image'] = resize_image(src, full_dst,
                                          image_size,
                                          remove_src=False)
        instance.sizes = sizes
        instance.save()
        log.info('Preview resized to: %s' % thumb_dst)

        # Remove src file now that it has been processed.
        try:
            os.remove(src)
        except OSError:
            pass

        return True

    except Exception, e:
        log.error("Error saving preview: %s; %s" % (e, thumb_dst))
Exemplo n.º 33
0
def _resize_image(old_im):
    new_dest = tempfile.NamedTemporaryFile()
    new_dest.close()
    resize_image(old_im.name, new_dest.name,
                 size=settings.INAPP_IMAGE_SIZE, locally=True)
    return new_dest
Exemplo n.º 34
0
def _resize_image(old_im, size):
    new_dest = tempfile.NamedTemporaryFile()
    new_dest.close()
    resize_image(old_im.name, new_dest.name, locally=True)
    return new_dest
Exemplo n.º 35
0
def _resize_image(old_im, size):
    new_dest = tempfile.NamedTemporaryFile()
    new_dest.close()
    resize_image(old_im.name, new_dest.name, locally=True)
    return new_dest