Add watermark and create thumbnail

90 views
Skip to first unread message

Alexander Artamoshin

unread,
Oct 9, 2015, 9:15:00 AM10/9/15
to Django ImageKit
I need to fit user's uploaded image to 1000px and put a watermark. Also I need to create thumbnail (without watermark).

class Watermark(object):
    def process(self, img):
        draw = ImageDraw.Draw(img)
        draw.line((0, 0) + img.size, fill=128)
        draw.line((0, img.size[1], img.size[0], 0), fill=128)
        return img


class Photo(models.Model):
    image = ProcessedImageField(upload_to='photo',
                                processors=[
                                    ResizeToFit(1000, 1000, upscale=False),
                                    Watermark(),
                                ],
                                format='JPEG')
    thumbnail = ImageSpecField(source='image',
                               processors=[
                                   ResizeToFill(200, 200),
                               ],
                               format='JPEG')

Trouble: thumbnail is created from the already processed image. How to create a thumbnail from the original image, given that the original image should not be saved?
With easy_thumbnails I may to generate thumbnail on model pre_save signal, but using signal trick with ImageKit (like instance.thumbnail.generate()) raises I/O error.

Bala Subramanyam Vemu

unread,
Oct 11, 2015, 2:50:01 PM10/11/15
to Django ImageKit
Hi

When you are doing the watermark don't directly modify the original image instead use .copy() and return a new image

and then you can run the thumbnail processor thereby the original image will be available for both the fields

I am not sure whether the original upload image is discarded if we are not storing it

Bala
Reply all
Reply to author
Forward
0 new messages