Ensure maxium aspect ratio in RSS images.

This commit is contained in:
Bastian Kleineidam 2013-12-08 15:55:39 +01:00
parent 48b06220c7
commit 67c2203e7e
3 changed files with 30 additions and 1 deletions

View file

@ -1,3 +1,9 @@
Dosage 2.9 (released xx.xx.2013)
Features:
- events: Ensure maximum aspect ratio on displayed RSS images.
Dosage 2.8 (released 8.12.2013) Dosage 2.8 (released 8.12.2013)
Features: Features:

View file

@ -10,6 +10,11 @@ import codecs
import json import json
from . import rss, util, configuration from . import rss, util, configuration
# Maximum width or height to display an image in exported pages.
# Note that only the displayed size is adjusted, not the image itself.
MaxImageSize = (800, 800)
class EventHandler(object): class EventHandler(object):
"""Base class for writing events to files. The currently defined events are """Base class for writing events to files. The currently defined events are
start(), comicDownloaded() and end().""" start(), comicDownloaded() and end()."""
@ -81,9 +86,13 @@ class RSSEventHandler(EventHandler):
def comicDownloaded(self, comic, filename, text=None): def comicDownloaded(self, comic, filename, text=None):
"""Write RSS entry for downloaded comic.""" """Write RSS entry for downloaded comic."""
imageUrl = self.getUrlFromFilename(filename) imageUrl = self.getUrlFromFilename(filename)
size = getDimensionForImage(filename, MaxImageSize)
title = '%s - %s' % (comic.name, os.path.basename(filename)) title = '%s - %s' % (comic.name, os.path.basename(filename))
pageUrl = comic.referrer pageUrl = comic.referrer
description = '<img src="%s"/>' % imageUrl description = '<img src="%s"' % imageUrl
if size:
description += ' width="%d" height="%d"' % size
description += '/>'
if text: if text:
description += '<br/>%s' % text description += '<br/>%s' % text
description += '<br/><a href="%s">View Comic Online</a>' % pageUrl description += '<br/><a href="%s">View Comic Online</a>' % pageUrl
@ -105,6 +114,19 @@ class RSSEventHandler(EventHandler):
self.rss.write(self.rssfn) self.rss.write(self.rssfn)
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."""
try:
from PIL import Image
except ImportError:
return None
img = Image.open(filename)
img.thumbnail(maxsize)
return img.size
class HtmlEventHandler(EventHandler): class HtmlEventHandler(EventHandler):
"""Output in HTML format.""" """Output in HTML format."""

View file

@ -2,3 +2,4 @@
requests requests
# optional: # optional:
argcomplete argcomplete
PIL