From 992c5266642ad2767bd0bcff257dcb1d7b635bd5 Mon Sep 17 00:00:00 2001 From: Joseph Hallett Date: Tue, 4 Aug 2015 22:44:33 +0100 Subject: [PATCH] Implemented image resizing if image won't fit in window --- rainbowstream/c_image.py | 11 +++++++++++ rainbowstream/rainbow.py | 2 ++ 2 files changed, 13 insertions(+) diff --git a/rainbowstream/c_image.py b/rainbowstream/c_image.py index a044e07..b26142d 100644 --- a/rainbowstream/c_image.py +++ b/rainbowstream/c_image.py @@ -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']) diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 114e425..b15ef59 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -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.')) -- 1.9.1