Пример #1
0
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())))
Пример #3
0
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):
    try:
        render_variations(**kwargs)
        global BAR
        BAR += 1
    except:
        raise Exception("".join(traceback.format_exception(*sys.exc_info())))
Пример #5
0
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)
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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_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()
Пример #11
0
 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())))
Пример #13
0
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())))
Пример #14
0
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
Пример #15
0
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)
Пример #17
0
 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)
Пример #18
0
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"]
Пример #19
0
 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)
Пример #20
0
 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)
Пример #21
0
def process_photo_image(file_name, variations):
    render_variations(file_name, variations, replace=True)
Пример #22
0
def render_job(**kwargs):
    render_variations(**kwargs)
    return False
Пример #23
0
def render_job(**kwargs):
    render_variations(**kwargs)
    return False
Пример #24
0
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()