def upload_media(image, never_expire=True, file_type='.jpg'): """Given a link and an image, uploads the image to s3 into an image based on the link's fullname""" url = str() mime_type = mimetypes.guess_type("file" + file_type)[0] # Requires a filename with the extension f = tempfile.NamedTemporaryFile(suffix=file_type, delete=False) try: img = image if isinstance(img, basestring): img = str_to_image(img) if not img.mode == 'RGBA': # Indexed images will not convert properly img = img.convert('RGBA') if file_type == ".jpg": # PIL does not play nice when converting alpha channels to jpg background = Image.new('RGBA', img.size, (255, 255, 255)) background.paste(img, img) img = background.convert('RGB') img.save(f, quality=85, optimize=True) if file_type == ".png": optimize_png(f.name, g.png_optimizer) contents = open(f.name).read() file_name = get_filename_from_content(contents) if g.media_store == "s3": url = s3_upload_media(contents, file_name=file_name, mime_type=mime_type, file_type=file_type, never_expire=True) finally: os.unlink(f.name) return url
def force_thumbnail(link, image_data, never_expire=True, file_type=".jpg"): image = str_to_image(image_data) image = prepare_image(image) thumb_url = upload_media(image, never_expire=never_expire, file_type=file_type) update_link(link, thumbnail=thumb_url, media_object=None, thumbnail_size=image.size)
def upload_icon(file_name, image_data, size): assert g.media_store == 's3' image = str_to_image(image_data) image.format = 'PNG' image.thumbnail(size, Image.ANTIALIAS) icon_data = image_to_str(image) return s3_upload_media(icon_data, file_name=file_name, mime_type='image/png', file_type='.png', never_expire=True, replace=True)
def upload_media(image, never_expire=True, file_type='.jpg'): """Given a link and an image, uploads the image to s3 into an image based on the link's fullname""" url = str() mime_type = mimetypes.guess_type("file" + file_type)[ 0] # Requires a filename with the extension f = tempfile.NamedTemporaryFile(suffix=file_type, delete=False) try: img = image do_convert = True if isinstance(img, basestring): img = str_to_image(img) if img.format == "PNG" and file_type == ".png": img.verify() f.write(image) f.close() do_convert = False if do_convert: img = img.convert('RGBA') if file_type == ".jpg": # PIL does not play nice when converting alpha channels to jpg background = Image.new('RGBA', img.size, (255, 255, 255)) background.paste(img, img) img = background.convert('RGB') img.save( f, quality=85 ) # Bug in the JPG encoder with the optimize flag, even if set to false else: img.save(f, optimize=True) if file_type == ".png": optimize_png(f.name, g.png_optimizer) elif file_type == ".jpg": optimize_jpeg(f.name, g.jpeg_optimizer) contents = open(f.name).read() file_name = get_filename_from_content(contents) if g.media_store == "s3": url = s3_upload_media(contents, file_name=file_name, mime_type=mime_type, file_type=file_type, never_expire=True) finally: os.unlink(f.name) return url
def upload_media(image, never_expire=True, file_type='.jpg'): """Given a link and an image, uploads the image to s3 into an image based on the link's fullname""" url = str() mime_type = mimetypes.guess_type("file" + file_type)[0] # Requires a filename with the extension f = tempfile.NamedTemporaryFile(suffix=file_type, delete=False) try: img = image do_convert = True if isinstance(img, basestring): img = str_to_image(img) if img.format == "PNG" and file_type == ".png": img.verify() f.write(image) f.close() do_convert = False if do_convert: img = img.convert('RGBA') if file_type == ".jpg": # PIL does not play nice when converting alpha channels to jpg background = Image.new('RGBA', img.size, (255, 255, 255)) background.paste(img, img) img = background.convert('RGB') img.save(f, quality=85) # Bug in the JPG encoder with the optimize flag, even if set to false else: img.save(f, optimize=True) if file_type == ".png": optimize_png(f.name, g.png_optimizer) elif file_type == ".jpg": optimize_jpeg(f.name, g.jpeg_optimizer) contents = open(f.name).read() file_name = get_filename_from_content(contents) if g.media_store == "s3": url = s3_upload_media(contents, file_name=file_name, mime_type=mime_type, file_type=file_type, never_expire=True) finally: os.unlink(f.name) return url
def force_thumbnail(link, image_data, never_expire=True): image = str_to_image(image_data) image = prepare_image(image) upload_thumb(link, image, never_expire=never_expire) update_link(link, thumbnail=True, media_object=None)
def force_thumbnail(link, image_data, never_expire = True): image = str_to_image(image_data) image = prepare_image(image) upload_thumb(link, image, never_expire = never_expire) update_link(link, thumbnail = True, media_object = None)