Search code examples
pythondjangoeasy-thumbnails

Get the image (picture) of an django database using django-image-cropping and easy-thumbnails


I have an django installation with the plugins django-image-cropping and easy-thumbnails in use.

I want to add users pictures to there vCard using vObject.

models.py:
(...)
class Person(TranslatableModel):
    (...)
    pic = ImageCropField(_(u"profile picture"),
        blank=True,
        null=True,
        upload_to=settings.USER_PICTURE_DIR,
    )
    picture_cropped = ImageRatioField(
        'pic',
        '{}x{}'.format(*settings.USER_PICTURE_SIZE)
    )
(...)

So far all I can find about this topic is, how to get an URL to the original Picture, but I don't know how to get the cropped Picture nor do I know how to get the Picture itself and not an URL nor a pseudo-file-type.


Solution

  • You are using an older version of django-image-cropping as the ImageCropField does no longer exist in recent versions.

    For the latest version it is documented how to get the URL for a cropped thumbnail from your python code: https://github.com/jonasundderwolf/django-image-cropping#frontend

    from image_cropping.utils import get_backend
    thumbnail_url = get_backend().get_thumbnail_url(
        yourmodel.image,
        {
            'size': (430, 360),
            'box': yourmodel.cropping,
            'crop': True,
            'detail': True,
        }
    )
    

    In older versions you would use:

    from easy_thumbnails.files import get_thumbnailer
    thumbnail_url = get_thumbnailer(yourmodel.image).get_thumbnail({
        'size': (430, 360),
        'box': yourmodel.cropping,
        'crop': True,
        'detail': True,
    }).url