Thumbnails for images and videos #87
@ -114,8 +114,8 @@ class UtilsTest(TestCase):
|
|||||||
def test_get_thumbnail_upload_to(self):
|
def test_get_thumbnail_upload_to(self):
|
||||||
for file_hash, kwargs, expected in (
|
for file_hash, kwargs, expected in (
|
||||||
('foobar', {}, 'thumbnails/fo/foobar.png'),
|
('foobar', {}, 'thumbnails/fo/foobar.png'),
|
||||||
('deadbeaf', {'width': None, 'height': None}, 'thumbnails/de/deadbeaf.png'),
|
('deadbeef', {'width': None, 'height': None}, 'thumbnails/de/deadbeef.png'),
|
||||||
('deadbeaf', {'width': 640, 'height': 360}, 'thumbnails/de/deadbeaf_640x360.png'),
|
('deadbeef', {'width': 640, 'height': 360}, 'thumbnails/de/deadbeef_640x360.png'),
|
||||||
):
|
):
|
||||||
with self.subTest(file_hash=file_hash, kwargs=kwargs):
|
with self.subTest(file_hash=file_hash, kwargs=kwargs):
|
||||||
self.assertEqual(get_thumbnail_upload_to(file_hash, **kwargs), expected)
|
self.assertEqual(get_thumbnail_upload_to(file_hash, **kwargs), expected)
|
||||||
|
@ -232,12 +232,12 @@ def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNA
|
|||||||
Return a dict of size keys to output paths of generated thumbnail images.
|
Return a dict of size keys to output paths of generated thumbnail images.
|
||||||
"""
|
"""
|
||||||
thumbnails = {}
|
thumbnails = {}
|
||||||
|
image = Image.open(file_path)
|
||||||
for size_key, (w, h) in THUMBNAIL_SIZES.items():
|
for size_key, (w, h) in THUMBNAIL_SIZES.items():
|
||||||
output_path = get_thumbnail_upload_to(file_hash, width=w, height=h)
|
output_path = get_thumbnail_upload_to(file_hash, width=w, height=h)
|
||||||
size = (w, h)
|
size = (w, h)
|
||||||
with tempfile.TemporaryFile() as f:
|
with tempfile.TemporaryFile() as f:
|
||||||
logger.info('Resizing %s to %s (%s)', file_path, size, output_format)
|
logger.info('Resizing %s to %s (%s)', file_path, size, output_format)
|
||||||
image = Image.open(file_path)
|
|
||||||
resize_image(
|
resize_image(
|
||||||
image,
|
image,
|
||||||
size,
|
size,
|
||||||
@ -254,6 +254,7 @@ def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNA
|
|||||||
default_storage.delete(output_path)
|
default_storage.delete(output_path)
|
||||||
default_storage.save(output_path, f)
|
default_storage.save(output_path, f)
|
||||||
thumbnails[size_key] = {'size': size, 'path': output_path}
|
thumbnails[size_key] = {'size': size, 'path': output_path}
|
||||||
|
image.close()
|
||||||
return thumbnails
|
return thumbnails
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user