Saving Image in a temporary file in django
Question: I am very new in python and django.I have developed a project using django.
Here all the images are watermarked.I have watermarked all the images using
the following code...

    
    
    from PIL import Image
        def image_watermark(request,image_id):
            photo = Photo.objects.get(pk=image_id)
            watermark = Image.open('{0}/{1}'.format(settings.MEDIA_ROOT,'wmark.png'))
            img = Image.open(photo.photo.file)
    
            img.paste(watermark,(img.size[0]-watermark.size[0],img.size[1]- watermark.size[1]),watermark)
    
            img.save('{0}/{1}'.format(settings.MEDIA_ROOT,photo.photo.name), quality=80)
    
    
            wrapper = FileWrapper(open(photo.photo.url, 'rb'))
            response = StreamingHttpResponse(wrapper, 'image/jpeg')
            response['Content-Length'] = os.path.getsize(photo.photo.url)
            response['Content-Disposition'] = 'attachment; filename=photo.jpg'
            return response
    

all of my images are watermarked now.But the problem is,as a user ,if i
download these images,i found that the downloaded images are also
watermarked,but i want to download the actual image,not the watermarked
image,how can i solve the problem?


Answer: You need to store the watermarked image into a different file. First you need
to create another column to store the Watermarked image.

    
    
    class Photo(models.Model):
        ...
        watermarked_photo = ImageField()
    

Then when you save the watermarked image into that column.

    
    
    from PIL import Image
    def image_watermark(request,image_id):
        photo = Photo.objects.get(pk=image_id)
    
        # Only need to watermark when there's no watermark
        if not photo.watermarked_photo.name:
            photo.watermarked_photo.name = 'watermarked_' + photo.photo.name
            watermark = Image.open('{0}/{1}'.format(settings.MEDIA_ROOT,'wmark.png'))
            img = Image.open(photo.photo.file)
            img.paste(watermark,(img.size[0]-watermark.size[0],img.size[1]- watermark.size[1]),watermark)
            img.save('{0}/{1}'.format(settings.MEDIA_ROOT, photo.watermarked_photo.name), quality=80)
            photo.save()
    
        wrapper = FileWrapper(open(photo.watermarked_photo.url, 'rb'))
        response = StreamingHttpResponse(wrapper, 'image/jpeg')
        response['Content-Disposition'] = 'attachment; filename=photo.jpg'
        return response
    

My Django skill is a bit rusty, so this code might not work without some
modification. But the idea should be solid.

If you only want to use a temp file, then try this

    
    
    from PIL import Image
    import tempfile
    
    def image_watermark(request,image_id):
        photo = Photo.objects.get(pk=image_id)
        watermark = Image.open('{0}/{1}'.format(settings.MEDIA_ROOT,'wmark.png'))
        img = Image.open(photo.photo.file)
        img.paste(watermark,(img.size[0]-watermark.size[0],img.size[1]- watermark.size[1]),watermark)
    
        tmpfile = tempfile.TemporaryFile()
        img.save(tmpfile, img.format, quality=80)
    
        tmpfile.seek(0)
        wrapper = FileWrapper(tmpfile)
        response = StreamingHttpResponse(wrapper, 'image/jpeg')
        response['Content-Disposition'] = 'attachment; filename=photo.jpg'
        return response
    

