def test_image_resize(): test_img = os.path.join(os.path.dirname(__file__), 'static', 'pyramid.png') test_out = os.path.join(os.path.dirname(__file__), 'static', 'test.png') assert os.path.exists(test_img) if os.path.exists(test_out): os.unlink(test_out) assert not os.path.exists(test_out) make_thumbnail(test_img, test_out) assert os.path.exists(test_out) os.unlink(test_out)
def thumb_open(self, attachment): """Returns the attachment's thumbnail image as a file-like object""" s = self._filename(attachment) t = self._thumb_filename(attachment) # Regenerate the thumbnail if it's stale if os.path.exists(s) and t is not None: if (not os.path.exists(t) or self.thumb_updated(attachment) < self.updated(attachment)): path = os.path.dirname(t) if not os.path.exists(path): os.makedirs(path) mime_type = self.mime_type(attachment) if mime_type == 'image/svg+xml': # Just copy the SVG over - we'll resize it when we display it shutil.copyfile(s, t) elif can_resize(mime_type): # Otherwise, resize to a JPEG make_thumbnail(s, t) if os.path.exists(t): return io.open(t, 'rb') else: # XXX Return some generic thumbnail? return None