+from fnpdjango.utils.text.slughifi import slughifi
+
+
+# TODO: Using sorl.thumbnail for now,
+# but this should be done in Librarian,
+# directly using convert or PIL as a fallback.
+def get_image(src_img_path, width=None, default_width=1600, formats=('PNG', 'JPEG', 'GIF')):
+ """ Returns an object with `url` and `storage` attributes,
+ or None if using the original image is OK.
+ """
+
+ from PIL import Image
+ from sorl.thumbnail import get_thumbnail
+
+ # Does it need converting?
+ # Yes, if width is given explicitly.
+ convert = width is not None
+ if not convert:
+ # Looks like it doesn't need converting.
+ # But let's try opening it and checking its type.
+ try:
+ simg = Image.open(src_img_path)
+ except IOError:
+ # It doesn't look like image,
+ # but maybe it's convertable to one.
+ convert = True
+ else:
+ if simg.format not in formats:
+ # It's an image, but it's in some weird format.
+ convert = True
+ width = simg.size[0]
+
+ if convert:
+ if width is None:
+ width = default_width
+ return get_thumbnail(src_img_path, '%sx%s' % (width, 10*width))
+ else:
+ return None