-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ral-facilities/add-image-thumbnails-#29
Generate and store image thumbnails when creating #29
- Loading branch information
Showing
19 changed files
with
167 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Module for processing images. | ||
""" | ||
|
||
import base64 | ||
import logging | ||
from io import BytesIO | ||
|
||
from fastapi import UploadFile | ||
from PIL import Image, UnidentifiedImageError | ||
|
||
from object_storage_api.core.config import config | ||
from object_storage_api.core.exceptions import InvalidImageFileError | ||
|
||
logger = logging.getLogger() | ||
|
||
image_config = config.image | ||
|
||
|
||
def generate_thumbnail_base64_str(uploaded_image_file: UploadFile) -> str: | ||
""" | ||
Generates a thumbnail from an uploaded image file. | ||
:param uploaded_image_file: Uploaded image file. | ||
:return: Base64 encoded string of the thumbnail | ||
:raises: InvalidImageFileError if the given image file cannot be processed due to being invalid in some way. | ||
""" | ||
|
||
logger.debug("Generating thumbnail for uploaded image file") | ||
|
||
# Image may fail to open if the file is either not an image or is invalid in some other way | ||
try: | ||
pillow_image = Image.open(uploaded_image_file.file) | ||
except UnidentifiedImageError as exc: | ||
raise InvalidImageFileError( | ||
f"The uploaded file '{uploaded_image_file.filename}' could not be opened by Pillow" | ||
) from exc | ||
|
||
pillow_image.thumbnail( | ||
(image_config.thumbnail_max_size_pixels, image_config.thumbnail_max_size_pixels), | ||
# https://pillow.readthedocs.io/en/stable/handbook/concepts.html#filters-comparison-table | ||
resample=Image.Resampling.BICUBIC, | ||
) | ||
|
||
# Save into memory buffer using the WebP image format (There are other options available at | ||
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp) | ||
memory_image_buffer = BytesIO() | ||
pillow_image.save(memory_image_buffer, "webp") | ||
|
||
# Move buffer back to start ready for reading (it will be at the end after generating the thumbnail) | ||
uploaded_image_file.file.seek(0) | ||
|
||
# Encode the thumbnail data into a UTF-8 encoded bytestring | ||
return base64.b64encode(memory_image_buffer.getvalue()).decode("utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ dependencies = [ | |
"fastapi[all]", | ||
"PyJWT", | ||
"pymongo", | ||
"boto3" | ||
"boto3", | ||
"Pillow" | ||
] | ||
|
||
[project.urls] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Unit tests for image processing functions. | ||
""" | ||
|
||
import pytest | ||
from fastapi import UploadFile | ||
|
||
from object_storage_api.core.exceptions import InvalidImageFileError | ||
from object_storage_api.core.image import generate_thumbnail_base64_str | ||
|
||
|
||
class TestGenerateThumbnailBase64Str: | ||
"""Tests for the `generate_thumbnail_base64_str` method.""" | ||
|
||
def test_with_valid_image(self): | ||
"""Tests `generate_thumbnail_base64_str` with a valid image file provided.""" | ||
|
||
with open("test/files/image.jpg", "rb") as file: | ||
uploaded_image_file = UploadFile(file, filename="image.jpg") | ||
result = generate_thumbnail_base64_str(uploaded_image_file) | ||
|
||
assert result == "UklGRjQAAABXRUJQVlA4ICgAAADQAQCdASoCAAEAAUAmJYwCdAEO/gOOAAD+qlQWHDxhNJOjVlqIb8AA" | ||
|
||
def test_with_invalid_image(self): | ||
"""Tests `generate_thumbnail_base64_str` with an invalid image file provided.""" | ||
|
||
with open("test/files/invalid_image.jpg", "rb") as file: | ||
uploaded_image_file = UploadFile(file, filename="image.jpg") | ||
with pytest.raises(InvalidImageFileError) as exc: | ||
generate_thumbnail_base64_str(uploaded_image_file) | ||
|
||
assert str(exc.value) == f"The uploaded file '{uploaded_image_file.filename}' could not be opened by Pillow" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.