Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Proof): generate image thumbnail (size 400) on upload #486

Merged
merged 6 commits into from
Oct 3, 2024

Conversation

raphodn
Copy link
Member

@raphodn raphodn commented Oct 2, 2024

What

New Proof.image_thumb_path field
Generate a size 400 thumbnail with Pillow

Todo

  • generate the thumbnail for existing images

@raphodn raphodn linked an issue Oct 2, 2024 that may be closed by this pull request
@github-actions github-actions bot added the Proofs label Oct 2, 2024
@raphodn raphodn changed the title feat(Proof): generate image thumbnail (400) on create feat(Proof): generate image thumbnail (size 400) on create Oct 2, 2024
@raphodn raphodn force-pushed the raphodn/proof-image-thumb branch 2 times, most recently from 7c80974 to ad95b03 Compare October 2, 2024 22:12
@raphodn raphodn marked this pull request as ready for review October 3, 2024 07:43
@raphodn raphodn changed the title feat(Proof): generate image thumbnail (size 400) on create feat(Proof): generate image thumbnail (size 400) on upload Oct 3, 2024
@raphodn raphodn merged commit 9f72de6 into main Oct 3, 2024
8 checks passed
@raphodn raphodn deleted the raphodn/proof-image-thumb branch October 3, 2024 08:15
@raphodn
Copy link
Member Author

raphodn commented Oct 3, 2024

Script to generate thumbnails in staging & prod

from django.conf import settings
from open_prices.proofs.models import Proof
from open_prices.proofs.utils import generate_thumbnail

for proof in Proof.objects.filter(file_path__isnull=False, image_thumb_path__isnull=True):
    try:
        current_dir_id_str = proof.file_path.split("/")[0]
        current_dir = settings.IMAGES_DIR / current_dir_id_str
        file_stem = proof.file_path.split("/")[1].split(".")[0]
        extension = "." + proof.file_path.split("/")[1].split(".")[1]
        mimetype = proof.mimetype

        image_thumb_path = generate_thumbnail(current_dir, current_dir_id_str, file_stem, extension, mimetype)

        Proof.objects.filter(id=proof.id).update(image_thumb_path=image_thumb_path)
    except Exception as e:
        print("===================")
        print(proof.__dict__)
        print(e)

@raphodn
Copy link
Member Author

raphodn commented Oct 3, 2024

Generated errors for a few JPEG images

OSError: cannot write mode RGBA as JPEG

fixed here: a7f18e9
thanks to: https://stackoverflow.com/questions/48248405/cannot-write-mode-rgba-as-jpeg#comment108750538_48248432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Create thumbnail of proof images
1 participant