From c48d96d5e36cff59dd7d4f2afe7ccc452406b884 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Sat, 4 Jan 2020 15:56:35 +0100 Subject: [PATCH] Use imagesize instead of Pillow to get image sizes --- README.md | 6 +++--- dosagelib/events.py | 30 +++++++++++++++++++----------- requirements.txt | 1 + setup.cfg | 3 +-- tox.ini | 1 - 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f7d116a5a..0ea607c00 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,11 @@ Windows users can download a complete binary (including Python) from the The simplest way to install and upgrade dosage is with [pipx]. To install the newest stable version with all optional features use: - pipx install --spec dosage[css,dimensions,bash] dosage + pipx install --spec dosage[css,bash] dosage To install the newest development version, use: - pipx install --spec "dosage[css,dimensions,bash] @ git+https://github.com/webcomics/dosage.git" dosage + pipx install --spec "dosage[css,bash] @ git+https://github.com/webcomics/dosage.git" dosage To upgrade such installations, just run: @@ -100,7 +100,7 @@ To upgrade such installations, just run: If you want to run dosage directly from the source code, you should install it in "[editable]" mode, preferable in a [virtual environment]: - pip install -e .[css,dimensions,bash] + pip install -e .[css,bash] You can invoke Dosage directly from the source code as `./dosage`, but this mode of operation is discouraged, since dependencies might be missing. diff --git a/dosagelib/events.py b/dosagelib/events.py index b5fd0405b..9ce4402e9 100644 --- a/dosagelib/events.py +++ b/dosagelib/events.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2017 Tobias Gruetzmacher +# Copyright (C) 2015-2020 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function @@ -11,6 +11,8 @@ from six.moves.urllib.parse import quote as url_quote import codecs import json +import imagesize + from . import rss, util, configuration from .output import out @@ -134,18 +136,24 @@ class RSSEventHandler(EventHandler): def getDimensionForImage(filename, maxsize): """Return scaled image size in (width, height) format. - The scaling preserves the aspect ratio. - If PIL is not found returns None.""" + The scaling preserves the aspect ratio.""" try: - from PIL import Image - except ImportError: + origsize = imagesize.get(filename) + except Exception as e: + out.warn("Could not get image size of {}: {}".format(os.path.basename(filename), e)) return None - img = Image.open(filename) - width, height = img.size - if width > maxsize[0] or height > maxsize[1]: - img.thumbnail(maxsize) - out.info("Downscaled display size from %s to %s" % ((width, height), img.size)) - return img.size + + width, height = origsize + if width > maxsize[0]: + height = max(round(height * maxsize[0] / width), 1) + width = round(maxsize[0]) + if height > maxsize[1]: + width = max(round(width * maxsize[1] / height), 1) + height = round(maxsize[1]) + + if width < origsize[0] or height < origsize[1]: + out.info("Downscaled display size from %s to %s" % (origsize, (width, height))) + return (width, height) class HtmlEventHandler(EventHandler): diff --git a/requirements.txt b/requirements.txt index 5a2e66052..cb2644c82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ colorama +imagesize lxml requests>=2.0 six diff --git a/setup.cfg b/setup.cfg index 8b945f4e4..cd6649a83 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,6 +35,7 @@ python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.* packages = find: install_requires = colorama + imagesize lxml requests>=2.0 six @@ -50,8 +51,6 @@ console_scripts = [options.extras_require] css = cssselect -dimensions = - Pillow bash = argcomplete test = diff --git a/tox.ini b/tox.ini index 5c8cca6e8..eaef96120 100644 --- a/tox.ini +++ b/tox.ini @@ -20,7 +20,6 @@ deps = # Also install extra dependencies for tests. extras = css - dimensions test [testenv:flake8]