コード例 #1
0
ファイル: utils.py プロジェクト: edx/course-discovery
def custom_render_variations(file_name, variations, storage, replace=True):
    """ Utility method used to override default behaviour of StdImageFieldFile by
    passing it replace=True.

    Args:
        file_name (str): name of the image file.
        variations (dict): dict containing variations of image
        storage (Storage): Storage class responsible for storing the image.

    Returns:
        False (bool): to prevent its default behaviour
    """

    for variation in variations.values():
        StdImageFieldFile.render_variation(file_name, variation, replace, storage)

    # to prevent default behaviour
    return False
コード例 #2
0
ファイル: models.py プロジェクト: ClairePhila/django-stdimage
def custom_render_variations(file_name, variations, storage, replace=False):
    """Resize image to 100x100."""
    for _, variation in variations.items():
        variation_name = StdImageFieldFile.get_variation_name(
            file_name,
            variation['name']
        )
        if storage.exists(variation_name):
            storage.delete(variation_name)

        with storage.open(file_name) as f:
            with Image.open(f) as img:
                size = 100, 100
                img = img.resize(size)

                with BytesIO() as file_buffer:
                    img.save(file_buffer, 'JPEG')
                    f = ContentFile(file_buffer.getvalue())
                    storage.save(variation_name, f)

    return False