avif image support #374

This commit is contained in:
rimu 2024-12-06 09:44:56 +13:00
parent 6cc6fceb5b
commit 4f95d1237e
6 changed files with 79 additions and 66 deletions

View file

@ -1072,7 +1072,8 @@ def make_image_sizes_async(file_id, thumbnail_width, medium_width, directory, to
source_image_response = None source_image_response = None
if source_image_response and source_image_response.status_code == 200: if source_image_response and source_image_response.status_code == 200:
content_type = source_image_response.headers.get('content-type') content_type = source_image_response.headers.get('content-type')
if content_type and content_type.startswith('image'): if content_type:
if content_type.startswith('image') or (content_type == 'application/octet-stream' and file.source_url.endswith('.avif')):
source_image = source_image_response.content source_image = source_image_response.content
source_image_response.close() source_image_response.close()
@ -1091,6 +1092,8 @@ def make_image_sizes_async(file_id, thumbnail_width, medium_width, directory, to
file_ext = '.jpg' file_ext = '.jpg'
elif file_ext == '.svg+xml': elif file_ext == '.svg+xml':
return # no need to resize SVG images return # no need to resize SVG images
elif file_ext == '.octet-stream':
file_ext = '.avif'
else: else:
file_ext = os.path.splitext(file.source_url)[1] file_ext = os.path.splitext(file.source_url)[1]
file_ext = file_ext.replace('%3f', '?') # sometimes urls are not decoded properly file_ext = file_ext.replace('%3f', '?') # sometimes urls are not decoded properly
@ -1107,6 +1110,9 @@ def make_image_sizes_async(file_id, thumbnail_width, medium_width, directory, to
final_place = os.path.join(directory, new_filename + file_ext) final_place = os.path.join(directory, new_filename + file_ext)
final_place_thumbnail = os.path.join(directory, new_filename + '_thumbnail.webp') final_place_thumbnail = os.path.join(directory, new_filename + '_thumbnail.webp')
if file_ext == '.avif': # this is quite a big plugin so we'll only load it if necessary
import pillow_avif
# Load image data into Pillow # Load image data into Pillow
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485
image = Image.open(BytesIO(source_image)) image = Image.open(BytesIO(source_image))

View file

@ -159,6 +159,8 @@ class CreateImageForm(CreatePostForm):
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485
# Do not allow fascist meme content # Do not allow fascist meme content
try: try:
if '.avif' in uploaded_file.filename:
import pillow_avif
image_text = pytesseract.image_to_string(Image.open(BytesIO(uploaded_file.read())).convert('L')) image_text = pytesseract.image_to_string(Image.open(BytesIO(uploaded_file.read())).convert('L'))
except FileNotFoundError as e: except FileNotFoundError as e:
image_text = '' image_text = ''

View file

@ -673,6 +673,8 @@ def add_post(actor, type):
if file_ext.lower() == '.heic': if file_ext.lower() == '.heic':
register_heif_opener() register_heif_opener()
if file_ext.lower() == '.avif':
import pillow_avif
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485

View file

@ -25,7 +25,7 @@ from sqlalchemy import func, desc, text
import os import os
allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo'] allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo', '.avif']
def search_for_community(address: str): def search_for_community(address: str):

View file

@ -187,7 +187,7 @@ def make_cache_key(sort=None, post_id=None, view_filter=None):
def is_image_url(url): def is_image_url(url):
common_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'] common_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.avif']
mime_type = mime_type_using_head(url) mime_type = mime_type_using_head(url)
if mime_type: if mime_type:
mime_type_parts = mime_type.split('/') mime_type_parts = mime_type.split('/')
@ -232,6 +232,8 @@ def mime_type_using_head(url):
response.raise_for_status() # Raise an exception for HTTP errors response.raise_for_status() # Raise an exception for HTTP errors
content_type = response.headers.get('Content-Type') content_type = response.headers.get('Content-Type')
if content_type: if content_type:
if content_type == 'application/octet-stream':
return ''
return content_type return content_type
else: else:
return '' return ''

View file

@ -24,6 +24,7 @@ beautifulsoup4==4.12.2
flask-caching==2.0.2 flask-caching==2.0.2
Pillow Pillow
pillow-heif pillow-heif
pillow-avif-plugin
feedgen==0.9.0 feedgen==0.9.0
celery==5.3.6 celery==5.3.6
redis==5.0.1 redis==5.0.1