Пример #1
0
def _save_image(img_file, filename, format, dimensions):
    import shutil
    from PIL import Image
    from imagekit.utils import open_image
    from pilkit.utils import extension_to_format
    from cnntools.common_utils import resize_mindim

    # Skip if we already downloaded the image
    if os.path.exists(filename):
        return

    parent_dir = os.path.dirname(filename)
    ensuredir(parent_dir)

    if not dimensions and not format:
        img_file.seek(0)
        with open(filename, 'wb') as f:
            shutil.copyfileobj(img_file, f)
    else:
        if dimensions and len(dimensions) == 2:
            image = open_image(img_file)

            if image.size != tuple(dimensions):
                image = image.resize(dimensions, Image.ANTIALIAS)
        elif dimensions and len(dimensions) == 1:
            # Here we specified the minimum dimension
            image = open_image(img_file)
            image = resize_mindim(image, dimensions[0])
        else:
            image = open_image(img_file)

        if not format:
            format = extension_to_format(os.path.splitext(filename)[1].lower())

        image.save(filename, format)
Пример #2
0
def download_photo_task(photo_id,
                        filename,
                        format=None,
                        larger_dimension=None):
    """ Downloads a photo and stores it, potentially downsampling it and
    potentially converting formats """

    parent_dir = os.path.dirname(filename)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

    photo = Photo.objects.get(id=photo_id)
    if not larger_dimension and not format:
        photo.image_orig.seek(0)
        with open(filename, 'wb') as f:
            shutil.copyfileobj(photo.image_orig, f)
    else:
        if larger_dimension <= 512:
            image = open_image(photo.image_512)
        elif larger_dimension <= 1024:
            image = open_image(photo.image_1024)
        elif larger_dimension <= 2048:
            image = open_image(photo.image_2048)
        else:
            image = open_image(photo.image_orig)

        if max(image.size) > larger_dimension:
            if image.size[0] > image.size[1]:
                image = image.resize(
                    (larger_dimension,
                     larger_dimension * image.size[1] / image.size[0]),
                    Image.ANTIALIAS)
            else:
                image = image.resize(
                    (larger_dimension * image.size[0] / image.size[1],
                     larger_dimension), Image.ANTIALIAS)

        if not format:
            format = extension_to_format(os.path.splitext(filename).lower())

        image.save(filename, format)
Пример #3
0
def download_photo_task(photo_id, filename, format=None, larger_dimension=None):
    """ Downloads a photo and stores it, potentially downsampling it and
    potentially converting formats """

    parent_dir = os.path.dirname(filename)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

    photo = Photo.objects.get(id=photo_id)
    if not larger_dimension and not format:
        photo.image_orig.seek(0)
        with open(filename, 'wb') as f:
            shutil.copyfileobj(photo.image_orig, f)
    else:
        if larger_dimension <= 512:
            image = open_image(photo.image_512)
        elif larger_dimension <= 1024:
            image = open_image(photo.image_1024)
        elif larger_dimension <= 2048:
            image = open_image(photo.image_2048)
        else:
            image = open_image(photo.image_orig)

        if max(image.size) > larger_dimension:
            if image.size[0] > image.size[1]:
                image = image.resize((
                    larger_dimension,
                    larger_dimension * image.size[1] / image.size[0]), Image.ANTIALIAS)
            else:
                image = image.resize((
                    larger_dimension * image.size[0] / image.size[1],
                    larger_dimension), Image.ANTIALIAS)

        if not format:
            format = extension_to_format(os.path.splitext(filename).lower())

        image.save(filename, format)
Пример #4
0
def test_unknown_extension():
    extension_to_format('.txt')
Пример #5
0
def test_extension_to_format():
    eq_(extension_to_format('.jpeg'), 'JPEG')
    eq_(extension_to_format('.rgba'), 'SGI')
Пример #6
0
def test_extension_to_format():
    eq_(extension_to_format('.jpeg'), 'JPEG')
    eq_(extension_to_format('.rgba'), 'SGI')
Пример #7
0
def test_unknown_extension():
    extension_to_format('.txt')