17844

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?

Answer1:

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

Recommend

  • Create folder tree dynamically from a string
  • can only concatenate tuple (not “unicode”) to tuple
  • Media files not showing on Debug False
  • django - can't see the uploaded media files from apache
  • How to select sequential duplicates in SQL Server
  • How can i decode an mp3 and encode it as aac with ezstream
  • Fail SonarQube quality gate when coverage decreases
  • window.onbeforeunload in javascript
  • Plotting Route with Multiple Points in iOS
  • What's wrong with my datatrigger binding?
  • MVVM: Image Bind Source from FileOpenPicker
  • Emacs lua-mode issue: (void-function interactively-called-p)
  • Want to understand iframe breakout code
  • Are there any issues with placing image as background on an extended JFrame?
  • Python PIL to extract number from image
  • C#: Import/Export Settings into/from a File
  • Why does it draw lines in the wrong place?
  • Floated image with variable width and heading with background image
  • Disable Kendo Autocomplete
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • How to view images from protected folder with php?
  • How can I set a binding to a Combox in a UserControl?
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • AJAX Html Editor Extender upload image appearing blank
  • How to have background script and something similar to a default popup?
  • Xcode 4 NSLog Macro link in Xcode 3
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Python urlparse: small issue
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Change an a tag attribute in JavaScript based on screen width
  • GridView Sorting works once only
  • XCode can't find symbols for a specific iOS library/framework project
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j