Skip to content

Commit

Permalink
add convert image to webp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nriver committed Jun 7, 2024
1 parent 816b4d5 commit 9fdd563
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/trilium_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from loguru import logger
from natsort import natsort

from .utils.file_util import replace_extension
from .utils.markdown_math import reconstructMath, sanitizeInput
from .utils.note_util import beautify_content, sort_note_by_headings
from .utils.param_util import clean_param, format_query_string
Expand Down Expand Up @@ -1284,8 +1285,9 @@ def delete_attachment(self, attachmentId: str) -> bool:

def optimize_image_attachments(self, noteId: str, quality: int = 90):
"""
comporess image attachments
comporess image attachments, this keeps the original format
:param noteId:
:param quality:
:return:
"""

Expand All @@ -1312,6 +1314,48 @@ def optimize_image_attachments(self, noteId: str, quality: int = 90):
except:
pass

def optimize_image_attachments_to_webp(self, noteId: str, quality: int = 90):
"""
comporess image attachments, this tries to convert the original image to webp
:param noteId:
:param quality:
:return:
"""

attachments = self.get_attachments(noteId)
for attachment in attachments:
try:
logger.info(attachment)
if not attachment['role'] == 'image' and attachment['contentLength'] > 0:
continue
image_data = self.get_attachment_content(attachment['attachmentId'])
# try to convert to webp
extension = 'webp'
compressed_data = compress_image_bytes(image_data, extension, quality)
size_before = len(image_data)
size_after = len(compressed_data)
logger.info(f"Size before compression: {size_before} bytes")
logger.info(f"Size after compression: {size_after} bytes")
if size_after < size_before:
logger.info('replace image')
# update image content data
res = self.update_attachment_content(
attachment['attachmentId'], compressed_data, is_file=False
)
logger.info(res)
# update image file name and mime
res = self.update_attachment(
attachmentId=attachment['attachmentId'],
title=replace_extension(attachment['title'], 'webp'),
role='image',
mime='image/webp',
)
logger.info(res)
else:
logger.info('skip image')
except Exception as e:
logger.error(e)

def sort_note_content(self, noteId: str, locale_str: str = 'zh_CN.UTF-8'):
"""
Sort note content by headings
Expand Down
16 changes: 16 additions & 0 deletions src/trilium_py/utils/file_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os


def replace_extension(filename: str, new_extension: str) -> str:
"""
Replaces the extension of a given filename with a new extension.
:param filename: Original filename
:param new_extension: New extension to replace the old one (e.g., '.webp')
:return: Filename with the new extension
"""
if not new_extension.startswith('.'):
new_extension = '.' + new_extension

base = os.path.splitext(filename)[0]
return base + new_extension

0 comments on commit 9fdd563

Please sign in to comment.