Implemented image resizing if image won't fit in window
authorJoseph Hallett <bogwonch@bogwonch.net>
Tue, 4 Aug 2015 21:44:33 +0000 (22:44 +0100)
committerJoseph Hallett <bogwonch@bogwonch.net>
Tue, 4 Aug 2015 21:53:22 +0000 (22:53 +0100)
rainbowstream/c_image.py
rainbowstream/rainbow.py

index a044e07..b26142d 100644 (file)
@@ -71,6 +71,17 @@ def image_to_display(path, start=None, length=None):
     i.load()
     width = min(w, length)
     height = int(float(h) * (float(width) / float(w)))
+
+    if c['IMAGE_RESIZE_TO_FIT'] is True:
+        # If it image won't fit in the terminal without scrolling shrink it
+        # Subtract 3 from rows so the tweet message fits in too.
+        h = 2 * (int(rows) - 3)
+        if height >= h:
+            width = int(float(width) * (float(h) / float(height)))
+            height = h
+    if (height <= 0) or (width <= 0):
+        raise ValueError("image has negative dimensions")
+
     i = i.resize((width, height), Image.ANTIALIAS)
     height = min(height, c['IMAGE_MAX_HEIGHT'])
 
index 114e425..b15ef59 100644 (file)
@@ -242,6 +242,8 @@ def init(args):
     set_config('IMAGE_ON_TERM', str(c['IMAGE_ON_TERM']))
     # Use 24 bit color
     c['24BIT'] = args.color_24bit
+    # Resize images based on the current terminal size
+    set_config('IMAGE_RESIZE_TO_FIT', str(c.get('IMAGE_RESIZE_TO_FIT', False)))
     # Check type of ONLY_LIST and IGNORE_LIST
     if not isinstance(c['ONLY_LIST'], list):
         printNicely(red('ONLY_LIST is not a valid list value.'))