Skip to content

Commit

Permalink
rollback webp converting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePromidius committed Feb 28, 2024
1 parent a943fb5 commit 82b1cad
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/MangaManager/Common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,13 @@ def convert_to_webp(image_raw_data: IO[bytes]) -> bytes:
:return:
"""
try:
logger.info("Converting image")
image = Image.open(image_raw_data)
# Check if resizing is needed
is_grayscale = image.mode in ('L', 'LA') or all(
min(channel) == max(channel) for channel in ImageStat.Stat(image).extrema
)
# Choose resampling method based on image content
if is_grayscale:
# For grayscale images (e.g., manga pages)
resample_method = Image.NEAREST # or Image.BILINEAR
else:
# For colored images (e.g., covers)
resample_method = Image.BICUBIC # or Image.LANCZOS
# Check if resizing is needed
if any(dim > max_dim for dim, max_dim in zip(image.size, max_dimensions)):
# Resize the image with the chosen resampling method
image.thumbnail(max_dimensions, resample=resample_method)
converted_image_data = BytesIO()
image.save(converted_image_data, format="webp")
converted_image_data.seek(0)
return converted_image_data.getvalue()
image = Image.open(image_raw_data).convert()
# print(image.size, image.mode, len(image.getdata()))
converted_image = BytesIO()

image.save(converted_image, format="webp")
image.close()
return converted_image.getvalue()
except Exception as e:
logger.error(f"Exception converting image: {e}")
raise
Expand Down

0 comments on commit 82b1cad

Please sign in to comment.