Last active
July 28, 2023 07:48
-
-
Save ammar-faifi/0a292be038e148858022c2e30337af13 to your computer and use it in GitHub Desktop.
Reduce quality of `jpg` images and stack on them `png` logos
This file contains hidden or 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
from PIL import Image | |
import os | |
import glob | |
def resize_and_reduce_quality(input_dir, output_dir, max_size, quality): | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
for filename in os.listdir(input_dir): | |
input_path = os.path.join(input_dir, filename) | |
output_path = os.path.join(output_dir, filename) | |
if os.path.isdir(input_path): | |
continue | |
try: | |
with Image.open(input_path) as img: | |
img.thumbnail(max_size) | |
img.save(output_path, quality=quality) | |
except Exception as e: | |
print(f"Failed to process {filename}: {e}") | |
def stack_images_on_top(background_image_path, overlay_images_dir, output_image_path): | |
OFFSET = 60 | |
# Open the background image | |
background_image = Image.open(background_image_path) | |
# Open the overlay images | |
counter = 0 | |
for filename in os.listdir(overlay_images_dir): | |
if filename.endswith(".png"): | |
overlay_image = Image.open(os.path.join(overlay_images_dir, filename)) | |
overlay_image.thumbnail((200, 200)) | |
# Paste the overlay image on top of the background image | |
background_image.paste( | |
overlay_image, (1200 * counter + OFFSET, 20), overlay_image | |
) | |
counter += 1 | |
# Save the resulting stacked image | |
background_image.save(output_image_path, quality=100) | |
def stack_images_in_directory(directory_path, overlay_images_dir, output_directory): | |
# Create the output directory if it doesn't exist | |
if not os.path.exists(output_directory): | |
os.makedirs(output_directory) | |
# Loop through all the images in the directory | |
for filename in os.listdir(directory_path): | |
print(f"Processing {filename}") | |
if filename.endswith(".JPG") or filename.endswith(".jpeg"): | |
image_path = os.path.join(directory_path, filename) | |
output_image_path = os.path.join(output_directory, filename) | |
# Stack the overlay image on top of the current image | |
stack_images_on_top(image_path, overlay_images_dir, output_image_path) | |
if __name__ == "__main__": | |
main_dir = "./images/" | |
for dir_ in os.listdir(main_dir): | |
input_dir = os.path.join(main_dir, dir_) | |
if not os.path.isdir(input_dir): | |
continue | |
print(f"In {dir_}") | |
overlay_images_dir = "/Users/ammar-imac/Downloads/logos/" | |
output_dir = os.path.join(input_dir, "processed") | |
output_dir2 = output_dir | |
# aspect ratio is preserved | |
max_image_size = (1500, 1500) | |
image_quality = 85 | |
resize_and_reduce_quality(input_dir, output_dir, max_image_size, image_quality) | |
stack_images_in_directory(output_dir, overlay_images_dir, output_dir2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment