1

I'm trying to write a function to blur an image with Pythons PIL (Pillow) module. This is as far as I got:

from PIL import Image, ImageFilter
from random import randint

def blur_image(image):
    try:
        original = Image.open(image)
        blurred = original.filter(ImageFilter.BLUR)

        # Add random integers to the new file
        rand = randint(1, 99999)
        blurred.save(str(rand) + image, "JPEG")
    except:
        print "Unable to load image."

So what I basically want is:

  1. Use a function to pass the image URL and open it
  2. Blur the image
  3. Concatenate some random integers to the filename
  4. Save the file as JPEG to a location in my Django media folder.

Where do the save() function save the newly created file or does it just replace it?

And another question. Do I have to create a regex or something to use this function in my templates? Never done that before. E.g:

{% blur_image(image) %}

That above is obviously not correct, but just to show you what I want.

Y7da
  • 403
  • 2
  • 5
  • 14
  • 1
    There's an excellent `imagekit` app for those kinds of things. Give it a try: https://github.com/matthewwithanm/django-imagekit – Alex Morozov Jan 19 '16 at 09:13
  • Do you actually need to save the image? it may be better to just keep the image data in memory – Sayse Jan 19 '16 at 09:36
  • @Sayse: Probably because I need the url to change a background to a div class and I need a new image for that. – Y7da Jan 19 '16 at 09:39
  • 1
    This may interest you.. [Is there a way to set background-image as a base64 encoded image in javascript?](http://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image-in-javascript), its not actually about javascript but shows how to set a css background with image data – Sayse Jan 19 '16 at 09:40

0 Answers0