summaryrefslogtreecommitdiff
path: root/app/media/resize.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2021-08-14 09:39:08 -0400
committerluxagraf <sng@luxagraf.net>2021-08-14 09:39:08 -0400
commit9327e01c60b114286f5a142552aae60843029a48 (patch)
tree7f353f4299d50013c3b90f7b749fc6ab211b32d4 /app/media/resize.py
parent0feb366b10cf422f12d18fa753e5d876120f4195 (diff)
initial commit
Diffstat (limited to 'app/media/resize.py')
-rw-r--r--app/media/resize.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/app/media/resize.py b/app/media/resize.py
new file mode 100644
index 0000000..13c0151
--- /dev/null
+++ b/app/media/resize.py
@@ -0,0 +1,53 @@
+import os
+import io
+
+from django.conf import settings
+
+try:
+ import Image
+ import ImageFile
+except ImportError:
+ try:
+ from PIL import Image
+ from PIL import ImageFile
+ except ImportError:
+ raise ImportError("Could not import the Python Imaging Library.")
+
+ImageFile.MAXBLOCK = 1000000
+
+
+def make_local_copies(self,photo):
+ orig_dir = settings.IMAGES_ROOT + '/flickr/full/' + photo.pub_date.strftime("%Y")
+ if not os.path.isdir(orig_dir):
+ os.makedirs(orig_dir)
+ im = io.StringIO(fname.read().decode('UTF-8')) # constructs a StringIO holding the image
+ img = Image.open(im)
+ local_full = '%s/%s.jpg' % (orig_dir, photo.flickr_id)
+ img.save(local_full)
+
+ #calculate crop:
+ cur_width, cur_height = img.size
+ new_width, new_height = 291, 350
+ ratio = max(float(new_width) / cur_width, float(new_height) / cur_height)
+ x = (cur_width * ratio)
+ y = (cur_height * ratio)
+ xd = abs(new_width - x)
+ yd = abs(new_height - y)
+ x_diff = int(xd / 2)
+ y_diff = int(yd / 2)
+ box = (int(x_diff), int(y_diff), int(x_diff + new_width), int(y_diff + new_height))
+
+ # create resized file
+ resized = img.resize((int(x), int(y)), Image.ANTIALIAS).crop(box)
+ # save resized file
+ resized_filename = '%s/%s.jpg' % (crop_dir, set.id)
+ try:
+ if img.format == 'JPEG':
+ resized.save(resized_filename, 'JPEG', quality=95, optimize=True)
+ else:
+ resized.save(resized_filename)
+ except IOError as e:
+ if os.path.isfile(resized_filename):
+ os.unlink(resized_filename)
+ raise e
+ os.unlink(img)