diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2009-12-23 15:42:47 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2009-12-23 15:42:47 +0100 |
commit | ca704f2f01495153c4ad8b2721a99afcb6654576 (patch) | |
tree | cb4f25ba1c7a2d22e2e7eca5f955f6e856e21da8 | |
parent | f1ab2a9437d876e82e5c22fd622b94f60e554be1 (diff) |
Print image sizes.
-rw-r--r-- | modules/url_titles.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/modules/url_titles.py b/modules/url_titles.py index 0f7d64a..7297d4d 100644 --- a/modules/url_titles.py +++ b/modules/url_titles.py @@ -5,6 +5,7 @@ info = { } import re, urllib2, htmlentitydefs, gzip, cStringIO, spotimeta +from PIL import ImageFile class Module: re_http = re.compile(r'(http://[^\ ]+)') @@ -95,6 +96,25 @@ class Module: buf = u.read(1024) if m: titles.append(m.groups()[0]) + elif u.headers['content-type'] in ('image/gif', 'image/png', 'image/jpeg'): + def pretty_size(size): + suffixes = (('B', 2**10), ('KiB', 2**20), ('MiB', 2**30), ('GiB', 2**40), ('TiB', 2**50)) + for suf, lim in suffixes: + if size > lim: + continue + else: + return '%s %s' % (str(round(size/float(lim/2**10), 2)), suf) + p = ImageFile.Parser() + size = 0 + while 1: + s = u.read(1024) + size += len(s) + if not s: + break + p.feed(s) + im = p.close() + titles.append('%s image: %dx%d (%s)' % ((im.format,) + tuple(im.size) + (pretty_size(size),))) + del im u.close() if len(titles) == 1: s = format_text(titles[0]) |