def resize_icon(src, dst, sizes, src_storage=private_storage, dst_storage=public_storage, **kw): """Resizes addon/websites 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, src_storage=src_storage, dst_storage=dst_storage) pngcrush_image.delay(size_dst, storage=dst_storage, **kw) with src_storage.open(src) as fd: icon_hash = _hash_file(fd) src_storage.delete(src) log.info('Icon resizing completed for: %s' % dst) return {'icon_hash': icon_hash} except Exception, e: log.error("Error resizing icon: %s; %s" % (e, dst))
def _resize_image(old_im, size): new_dest = tempfile.mktemp() resize_image(old_im.name, new_dest, src_storage=local_storage, dst_storage=local_storage) return new_dest
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))
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): Webapp.objects.filter(pk__in=chunk).update(icon_type='image/png')
def test_resize_transparency(): src = get_image_path('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)
def test_resize_transparency(): src = get_image_path('transparent.png') dest = tempfile.mkstemp(dir=settings.TMP_PATH)[1] expected = src.replace('.png', '-expected.png') if storage_is_remote(): copy_to_storage(src, src, src_storage=local_storage) try: resize_image(src, dest, (32, 32), remove_src=False) with public_storage.open(dest) as dfh: with open(expected) as efh: assert dfh.read() == efh.read() finally: if public_storage.exists(dest): public_storage.delete(dest)
def resize_icon(src, dst, sizes, src_storage=private_storage, dst_storage=public_storage, **kw): """Resizes addon/websites 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, src_storage=src_storage, dst_storage=dst_storage) pngcrush_image.delay(size_dst, storage=dst_storage, **kw) with src_storage.open(src) as fd: icon_hash = _hash_file(fd) src_storage.delete(src) log.info("Icon resizing completed for: %s" % dst) return {"icon_hash": icon_hash} except Exception, e: log.error("Error resizing icon: %s; %s" % (e, dst))
def resize_promo_imgs(src, dst, sizes, **kw): """Resizes webapp/website promo imgs.""" log.info('[1@None] Resizing promo imgs: %s' % dst) try: for s in sizes: size_dst = '%s-%s.png' % (dst, s) # Crop only to the width, keeping the aspect ratio. resize_image(src, size_dst, (s, 0), remove_src=False) pngcrush_image.delay(size_dst, **kw) with private_storage.open(src) as fd: promo_img_hash = _hash_file(fd) private_storage.delete(src) log.info('Promo img hash resizing completed for: %s' % dst) return {'promo_img_hash': promo_img_hash} except Exception, e: log.error("Error resizing promo img hash: %s; %s" % (e, dst))
def resize_icon(src, dst, sizes, locally=False, **kw): """Resizes addon/websites icons.""" log.info("[1@None] Resizing icon: %s" % dst) open_ = open if locally else storage.open delete = os.unlink if locally else storage.delete 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) with open_(src) as fd: icon_hash = _hash_file(fd) delete(src) log.info("Icon resizing completed for: %s" % dst) return {"icon_hash": icon_hash} except Exception, e: log.error("Error resizing icon: %s; %s" % (e, dst))
def resize_preview(src, pk, **kw): """Resizes preview images and stores the sizes on the preview.""" instance = Preview.objects.get(pk=pk) 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 private_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. private_storage.delete(src) return True except Exception, e: log.error("Error saving preview: %s; %s" % (e, thumb_dst))
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))
def resize_promo_imgs(src, dst, sizes, locally=False, **kw): """Resizes webapp/website promo imgs.""" log.info("[1@None] Resizing promo imgs: %s" % dst) try: for s in sizes: size_dst = "%s-%s.png" % (dst, s) # Crop only to the width, keeping the aspect ratio. resize_image(src, size_dst, (s, 0), remove_src=False, locally=locally) pngcrush_image.delay(size_dst, **kw) if locally: with open(src) as fd: promo_img_hash = _hash_file(fd) os.remove(src) else: with storage.open(src) as fd: promo_img_hash = _hash_file(fd) storage.delete(src) log.info("Promo img hash resizing completed for: %s" % dst) return {"promo_img_hash": promo_img_hash} except Exception, e: log.error("Error resizing promo img hash: %s; %s" % (e, dst))
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
def _resize_image(old_im, size): new_dest = tempfile.mktemp() resize_image(old_im.name, new_dest, storage=local_storage) return new_dest