from django.core.files.storage import FileSystemStorage
import os
class ImageFSStorage(FileSystemStorage):
"""
A FileSystemStorage which normalizes extensions for images.
"""
def find_extension(self, format):
"""Normalizes PIL-returned format into a standard, lowercase extension."""
format = format.lower()
if format == 'jpeg':
format = 'jpg'
return format
def save(self, name, content):
dirname = os.path.dirname(name)
basename = os.path.basename(name)
# Use PIL to determine filetype
from PIL import ImageFile as PILImageFile
p = PILImageFile.Parser()
while 1:
data = content.read(1024)
if not data:
break
p.feed(data)
if p.image:
im = p.image
break
extension = self.find_extension(im.format)
# Does the basename already have an extension? If so, replace it.
# bare as in without extension
bare_basename = basename if '.' not in basename else basename[:basename.rindex('.')]
basename = bare_basename + '.' + extension
name = os.path.join(dirname, basename)
return super(ImageFSStorage, self).save(name, content)
Comments
Hello,
Nice one, may I had it to the django-storages project?
#
_.replace('had', 'add'), hard to type in the morning :)
#
Sorry about the late reply, but you may absolutely!
#
It appears to be parsing the entire file? At least when I feed it a JPEG. A less heavy-weight solution would only look at the headers, I got some code for that on my blog for PNG, JPEG and TIFF. I noticed it because I used the version in the django-storages project.
#
Hm, scrap that, it seems it's just not working. There needs to be a content.seek(0) before the loop starts.
Also it might be a good idea to rewrite it to not being dependent on an image to be parsed. Right now it crashes with an exception in this case because im is undefined.
#
This does it for me:
#