Commits

Anonymous committed 70e0e54

Implemented resize

Comments (0)

Files changed (3)

+syntax: glob
+
+test/
+*.pyc
+*.swp
+Easy Flickr
+===========
+
+Easy Flickr is a command line tool to reduce keystrokes and
+mouse clicks when I am uploading photos to flickr.
+
+Features
+--------
+
+* Resize photos and limit file size
+* Publish photos to flickr
+"""Easy Flickr"""
+from __future__ import division, print_function
+
+import os
+
+from PIL import Image
+
+_default_len = 720.0 #px
+_default_size = 300 #kB
+_high_q = 95 # highest quality
+_low_q = 80 # lowest quality
+
+def resize(img, size=None, no_enlarge=True):
+    """
+    Resize a photo to size, maintain aspect ratio no matter what.
+    
+    * If size was None, use default algorithm.
+    * If no_enlarge was True and any dimension of img.size was less than the
+      specified size, do nothing
+
+    Default algorithm:
+        
+        shorter_side = min(img.size[0], img.size[1])
+
+        if shorter_side > _default_len:
+            shorter_side = _default_len
+            set longer_side to a new value maintains the aspect ratio
+        else:
+            do nothing
+
+    
+    img -- PIL.Image instance
+    size -- (width, height)
+    no_enlarge -- True/False
+    """
+    p = 1.0
+    if size is None:
+        shorter = min(img.size[0], img.size[1])
+        if shorter > _default_len:
+            p = _default_len / shorter
+    else:
+        if no_enlarge:
+            if size[0] != 0 and img.size[0] > size[0]:
+                p = size[0] / img.size[0]
+            elif size[1] != 0 and img.size[1] > size[1]:
+                p = size[1] / img.size[1]
+        else:
+            if size[0] != 0:
+                p = size[0] / img.size[0]
+            elif size[1] != 0:
+                p = size[1] / img.size[1]
+    size = (img.size[0] * p, img.size[1] * p)
+    return img.resize(size)
+
+if __name__ == '__main__':
+    pass