def resize_and_autorotate(file_name, variations, storage): with storage.open(file_name) as f: with Image.open(f) as image: file_format = image.format try: exif = image._getexif() except AttributeError: exif = None # if image has exif data about orientation, let's rotate it orientation_key = 274 # cf ExifTags if exif and orientation_key in exif: orientation = exif[orientation_key] rotate_values = {3: Image.ROTATE_180, 6: Image.ROTATE_270, 8: Image.ROTATE_90} if orientation in rotate_values: image = image.transpose(rotate_values[orientation]) with BytesIO() as file_buffer: image.save(file_buffer, file_format) f = ContentFile(file_buffer.getvalue()) storage.delete(file_name) storage.save(file_name, f) # render stdimage variations render_variations(file_name, variations, replace=True, storage=storage) return False # prevent default rendering
def render_field_variations(kwargs): try: render_variations(**kwargs) global BAR BAR += 1 except: raise Exception("".join(traceback.format_exception(*sys.exc_info())))
def resizeAndRotate(file_name, variations, storage): rotated = False with storage.open(file_name) as f: try: image = Image.open(f) except: return False else: with image: try: file_format = image.format exif = image._getexif() if exif and ORIENTATION_KEY in exif: orientation = exif[ORIENTATION_KEY] if orientation in ROTATE_VALUES: image = image.transpose(ROTATE_VALUES[orientation]) rotated = True if rotated: with BytesIO() as file_buffer: image.save(file_buffer, file_format) f = ContentFile(file_buffer.getvalue()) storage.delete(file_name) storage.save(file_name, f) except: return True render_variations(file_name, variations, storage=storage) return False
def render_field_variations(kwargs): kwargs['storage'] = get_storage_class(kwargs['storage'])() do_render = kwargs.pop('do_render') if callable(do_render): do_render = do_render(**kwargs) if do_render: render_variations(**kwargs)
def photo_render_variations(file_name, variations, storage): with storage.open(file_name) as f: with Image.open(f) as image: file_format = image.format logging.info('File format: ' + file_format) if file_format == 'JPEG': exif = image._getexif() # if image has exif data about orientation, let's rotate it orientation_key = 274 # cf ExifTags if exif and orientation_key in exif: orientation = exif[orientation_key] rotate_values = { 3: Image.ROTATE_180, 6: Image.ROTATE_270, 8: Image.ROTATE_90 } if orientation in rotate_values: image = image.transpose(rotate_values[orientation]) file_buffer = BytesIO() image.save(file_buffer, file_format) f = ContentFile(file_buffer.getvalue()) storage.delete(file_name) storage.save(file_name, f) # render stdimage variations render_variations(file_name, variations, replace=True, storage=storage) return False # prevent default rendering
def preprocess(file_name, variations, storage): with storage.open(file_name) as f: with Image.open(f) as image: file_format = 'PNG' # resize to a maximum of 1000x1000 keeping aspect ratio image.thumbnail((1000, 1000), resample=Image.ANTIALIAS) # Create a disk as mask mindimension = min(1000, image.size[1], image.size[0]) bigsize = (mindimension * 3, mindimension * 3) mask = Image.new('L', bigsize, 0) draw = ImageDraw.Draw(mask) draw.ellipse((0, 0) + bigsize, fill=255) mask = mask.resize((mindimension, mindimension), Image.ANTIALIAS) # only keep the image that fit in the mask output = ImageOps.fit(image, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) with BytesIO() as file_buffer: output.save(file_buffer, file_format) f = ContentFile(file_buffer.getvalue()) # delete the original big image storage.delete(file_name) # save the resized version with the same filename and format storage.save(file_name, f) # render stdimage variations render_variations(file_name, variations, replace=True, storage=storage) return False # prevent default rendering
def render_field_variations(kwargs): try: kwargs['storage'] = get_storage_class(kwargs['storage'])() render_variations(**kwargs) global BAR BAR += 1 except: raise Exception("".join(traceback.format_exception(*sys.exc_info())))
def render_linear(field, images, count, replace): init_progressbar(count) args_list = [ dict(file_name=file_name, variations=field.variations, replace=replace, storage=field.storage) for file_name in images ] for args in args_list: render_variations(**args) finish_progressbar()
def render_field_variations(kwargs): try: kwargs['storage'] = get_storage_class(kwargs['storage'])() do_render = kwargs.pop('do_render') if callable(do_render): do_render = do_render(**kwargs) if do_render: render_variations(**kwargs) global BAR BAR += 1 except: raise Exception("".join(traceback.format_exception(*sys.exc_info())))
def render_field_variations(kwargs): kwargs['storage'] = get_storage_class(kwargs['storage'])() ignore_missing = kwargs.pop('ignore_missing') do_render = kwargs.pop('do_render') try: if callable(do_render): kwargs.pop('field_class') do_render = do_render(**kwargs) if do_render: render_variations(**kwargs) except FileNotFoundError as e: if not ignore_missing: raise CommandError( 'Source file was not found, terminating. ' 'Use -i/--ignore-missing to skip this error.') from e
def download_remote_images(): for article in Article.objects.all(): if not article.original_image: continue try: result = urlretrieve(article.original_image.__str__()) article.image.save( os.path.basename(article.original_image.__str__()), File(open(result[0], 'rb'))) render_variations(result[0], image_variations, replace=True) article.save() except: logger.exception(result) logger.exception(result[0]) logger.exception('Unable to download remote image for %s' % article.original_image)
def test_render_variations(self, image_upload_file): instance = ManualVariationsModel.customer_manager.create( image=image_upload_file) path = os.path.join(IMG_DIR, 'image.thumbnail.jpg') assert not os.path.exists(path) render_variations(file_name=instance.image.name, variations={ 'thumbnail': { 'name': 'thumbnail', 'width': 150, 'height': 150, 'crop': True, 'resample': Image.ANTIALIAS } }) assert os.path.exists(path)
def render_field_variations(kwargs): kwargs["storage"] = get_storage_class(kwargs["storage"])() ignore_missing = kwargs.pop("ignore_missing") do_render = kwargs.pop("do_render") try: if callable(do_render): kwargs.pop("field_class") do_render = do_render(**kwargs) if do_render: render_variations(**kwargs) except FileNotFoundError as e: if not ignore_missing: print(ignore_missing) raise CommandError( "Source file was not found, terminating. " "Use -i/--ignore-missing to skip this error.") from e return kwargs["file_name"]
def test_render_variations(self, image_upload_file): instance = ManualVariationsModel.customer_manager.create( image=image_upload_file) path = os.path.join(IMG_DIR, "image.thumbnail.jpg") assert not os.path.exists(path) render_variations( file_name=instance.image.name, variations={ "thumbnail": { "name": "thumbnail", "width": 150, "height": 150, "crop": True, "resample": Image.ANTIALIAS, } }, ) assert os.path.exists(path)
def test_render_variations(self, image_upload_file): instance = ManualVariationsModel.customer_manager.create( image=image_upload_file ) path = os.path.join(IMG_DIR, 'image.thumbnail.jpg') assert not os.path.exists(path) render_variations( file_name=instance.image.name, variations={ 'thumbnail': { 'name': 'thumbnail', 'width': 150, 'height': 150, 'crop': True, 'resample': Image.ANTIALIAS } } ) assert os.path.exists(path)
def process_photo_image(file_name, variations): render_variations(file_name, variations, replace=True)
def render_job(**kwargs): render_variations(**kwargs) return False
def process_stdimage(file_name, variations, storage): render_variations(file_name, variations, replace=True, storage=storage) obj = get_model('api', 'User').objects.get(avatar=file_name) obj.save()