From ec232d9a178a9a4cc48a82ed557b2c00a4add483 Mon Sep 17 00:00:00 2001 From: freamon Date: Fri, 17 Jan 2025 22:47:40 +0000 Subject: [PATCH] Catch UnidentifiedImageError if non-image uploaded --- app/community/forms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/community/forms.py b/app/community/forms.py index cd034504..bc5857dd 100644 --- a/app/community/forms.py +++ b/app/community/forms.py @@ -12,7 +12,7 @@ from app.constants import DOWNVOTE_ACCEPT_ALL, DOWNVOTE_ACCEPT_MEMBERS, DOWNVOTE DOWNVOTE_ACCEPT_TRUSTED from app.models import Community, utcnow from app.utils import domain_from_url, MultiCheckboxField -from PIL import Image, ImageOps +from PIL import Image, ImageOps, UnidentifiedImageError from io import BytesIO import pytesseract @@ -173,6 +173,8 @@ class CreateImageForm(CreatePostForm): image_text = pytesseract.image_to_string(Image.open(BytesIO(uploaded_file.read())).convert('L')) except FileNotFoundError as e: image_text = '' + except UnidentifiedImageError as e: + image_text = '' if 'Anonymous' in image_text and ( 'No.' in image_text or ' N0' in image_text): # chan posts usually contain the text 'Anonymous' and ' No.12345' self.image_file.errors.append(