From: Joar Wandborg Date: Sat, 2 Jul 2011 23:30:07 +0000 (+0200) Subject: Feature #400 - Resize images to fit on page X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=93214d8e0287150aef3f4370237303bc73ec448c;p=mediagoblin.git Feature #400 - Resize images to fit on page * `mediagoblin.process_media.__init__` * Added `medium` size image conversion * Updated `thumbnail` conversion to use `queued_filename` instead of `queued_file` * `media.html` * If there exists a `medium` size for the `MediaEntry`, it will display instead of the original `main` image. --- diff --git a/mediagoblin/process_media/__init__.py b/mediagoblin/process_media/__init__.py index 0dce1418..0d1abcb3 100644 --- a/mediagoblin/process_media/__init__.py +++ b/mediagoblin/process_media/__init__.py @@ -22,6 +22,7 @@ from mediagoblin import mg_globals as mgg THUMB_SIZE = 200, 200 +MEDIUM_SIZE = 640, 640 def create_pub_filepath(entry, filename): @@ -43,20 +44,32 @@ def process_media_initial(media_id): mgg.queue_store, queued_filepath, 'source') - queued_file = file(queued_filename, 'r') + thumb = Image.open(queued_filename) + thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) + # ensure color mode is compatible with jpg + if thumb.mode != "RGB": + thumb = thumb.convert("RGB") - with queued_file: - thumb = Image.open(queued_file) - thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) - # ensure color mode is compatible with jpg - if thumb.mode != "RGB": - thumb = thumb.convert("RGB") + thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg') + + thumb_file = mgg.public_store.get_file(thumb_filepath, 'w') + with thumb_file: + thumb.save(thumb_file, "JPEG") + + """ + Create medium file, used in `media.html` + """ + medium = Image.open(queued_filename) + medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS) + + if medium.mode != "RGB": + medium = medium.convert("RGB") - thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg') + medium_filepath = create_pub_filepath(entry, 'medium.jpg') - thumb_file = mgg.public_store.get_file(thumb_filepath, 'w') - with thumb_file: - thumb.save(thumb_file, "JPEG") + medium_file = mgg.public_store.get_file(medium_filepath, 'w') + with medium_file: + medium.save(medium_file, "JPEG") # we have to re-read because unlike PIL, not everything reads # things in string representation :) @@ -73,6 +86,7 @@ def process_media_initial(media_id): media_files_dict = entry.setdefault('media_files', {}) media_files_dict['thumb'] = thumb_filepath media_files_dict['main'] = main_filepath + media_files_dict['medium'] = medium_filepath entry['state'] = u'processed' entry.save() diff --git a/mediagoblin/templates/mediagoblin/user_pages/media.html b/mediagoblin/templates/mediagoblin/user_pages/media.html index 3cebe2f9..e16f1e00 100644 --- a/mediagoblin/templates/mediagoblin/user_pages/media.html +++ b/mediagoblin/templates/mediagoblin/user_pages/media.html @@ -26,8 +26,13 @@

{{media.title}}

- + {% if media.media_files.medium %} + + {% else %} + + {% endif %}

Uploaded on {{ "%4d-%02d-%02d"|format(media.created.year,