-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from Xpirix/fix_fetch_feeds_and_resize_logo
Fix fetch_feeds script, add image resizing process
- Loading branch information
Showing
4 changed files
with
90 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ tomli==2.0.1 | |
typing_extensions==4.4.0 | ||
urllib3==1.26.13 | ||
icalendar==5.0.12 | ||
pillow==10.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from PIL import Image | ||
import os | ||
|
||
|
||
def resize_image(image_filename, max_height=120): | ||
""" | ||
Resize an image to a maximum height, keeping the aspect ratio. | ||
The image is resized in place. | ||
param image_filename: The image file to resize | ||
param max_height: The maximum height in pixels | ||
""" | ||
if ( | ||
image_filename.lower().endswith('.png') or | ||
image_filename.lower().endswith('.jpg') | ||
): | ||
if os.path.exists(image_filename): | ||
print(f'Processing: {image_filename}') | ||
with Image.open(image_filename) as img: | ||
width, height = img.size | ||
if height > max_height: | ||
new_height = max_height | ||
new_width = int((new_height / height) * width) | ||
|
||
img_resized = img.resize( | ||
(new_width, new_height), Image.LANCZOS | ||
) | ||
|
||
# Determine the file format | ||
file_format = ( | ||
'PNG' if image_filename.lower().endswith('.png') | ||
else 'JPEG' | ||
) | ||
|
||
# Save the resized image with optimization | ||
img_resized.save( | ||
image_filename, | ||
format=file_format, | ||
optimize=True, | ||
quality=85 | ||
) | ||
print(f'Resized and optimized: {image_filename}') | ||
else: | ||
print(f'No resizing needed for: {image_filename}') | ||
else: | ||
print(f'File not found: {image_filename}') |
File renamed without changes.