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:
- Use a function to pass the image URL and open it
- Blur the image
- Concatenate some random integers to the filename
- 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.