mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 11:26:56 -08:00
avif image support #374
This commit is contained in:
parent
6cc6fceb5b
commit
4f95d1237e
6 changed files with 79 additions and 66 deletions
|
@ -1072,7 +1072,8 @@ def make_image_sizes_async(file_id, thumbnail_width, medium_width, directory, to
|
|||
source_image_response = None
|
||||
if source_image_response and source_image_response.status_code == 200:
|
||||
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_response.close()
|
||||
|
||||
|
@ -1091,6 +1092,8 @@ def make_image_sizes_async(file_id, thumbnail_width, medium_width, directory, to
|
|||
file_ext = '.jpg'
|
||||
elif file_ext == '.svg+xml':
|
||||
return # no need to resize SVG images
|
||||
elif file_ext == '.octet-stream':
|
||||
file_ext = '.avif'
|
||||
else:
|
||||
file_ext = os.path.splitext(file.source_url)[1]
|
||||
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_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
|
||||
Image.MAX_IMAGE_PIXELS = 89478485
|
||||
image = Image.open(BytesIO(source_image))
|
||||
|
|
|
@ -159,6 +159,8 @@ class CreateImageForm(CreatePostForm):
|
|||
Image.MAX_IMAGE_PIXELS = 89478485
|
||||
# Do not allow fascist meme content
|
||||
try:
|
||||
if '.avif' in uploaded_file.filename:
|
||||
import pillow_avif
|
||||
image_text = pytesseract.image_to_string(Image.open(BytesIO(uploaded_file.read())).convert('L'))
|
||||
except FileNotFoundError as e:
|
||||
image_text = ''
|
||||
|
|
|
@ -673,6 +673,8 @@ def add_post(actor, type):
|
|||
|
||||
if file_ext.lower() == '.heic':
|
||||
register_heif_opener()
|
||||
if file_ext.lower() == '.avif':
|
||||
import pillow_avif
|
||||
|
||||
Image.MAX_IMAGE_PIXELS = 89478485
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ from sqlalchemy import func, desc, text
|
|||
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):
|
||||
|
|
|
@ -187,7 +187,7 @@ def make_cache_key(sort=None, post_id=None, view_filter=None):
|
|||
|
||||
|
||||
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)
|
||||
if mime_type:
|
||||
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
|
||||
content_type = response.headers.get('Content-Type')
|
||||
if content_type:
|
||||
if content_type == 'application/octet-stream':
|
||||
return ''
|
||||
return content_type
|
||||
else:
|
||||
return ''
|
||||
|
|
|
@ -24,6 +24,7 @@ beautifulsoup4==4.12.2
|
|||
flask-caching==2.0.2
|
||||
Pillow
|
||||
pillow-heif
|
||||
pillow-avif-plugin
|
||||
feedgen==0.9.0
|
||||
celery==5.3.6
|
||||
redis==5.0.1
|
||||
|
|
Loading…
Reference in a new issue