I had two scripts which I merged but 2 problems arised:
-
for light colors which are overlayed the result is way too bright as it overlays the original pixels that were brighter than others with total white rather than their color. For example “lightblue” “pink” and “grey”. While the rest of the colors when overlayed the result is perfect
-
it won’t process the png which first word is “2white” (3rd condition).
from PIL import Image
import os
def overlay_pixels(image, condition, color):
for x in range(image.size[0]):
for y in range(image.size[1]):
pixel = image.getpixel((x, y))
if condition(pixel):
# Preserve the original alpha value
new_pixel = tuple(a + b for a, b in zip(pixel[:3], color)) + (pixel[3],)
image.putpixel((x, y), new_pixel)
# Define the directory containing the images
folder_path = r'C:UsersUtenteDesktopa1'
dir_auto = folder_path # Assuming the output folder is the same as input for simplicity
# Iterate over all files in the directory
for filename in os.listdir(folder_path):
if filename.lower().endswith('.png') and filename.lower().split()[0] == "black":
filepath = os.path.join(dir_auto, filename)
# Open the original image
original_image = Image.open(filepath).convert("RGBA")
# Step 2: Create copies and rename
for color_name, overlay_condition, overlay_color in [
("pink", lambda p: p[3] > 0, (252, 130, 216)),
("white", lambda p: p[3] > 0, (255, 255, 255)),
("blue", lambda p: p[3] > 0, (21, 31, 148)),
("red", lambda p: p[3] > 0, (168, 0, 0)),
("lightblue", lambda p: p[3] > 0, (99, 168, 231)),
("navyblue", lambda p: p[3] > 0, (4, 21, 64)),
("grey", lambda p: p[3] > 0, (134, 134, 134)),
("fuchsia", lambda p: p[3] > 0, (226, 46, 108)),
]:
copy_image = original_image.copy()
overlay_pixels(copy_image, overlay_condition, overlay_color)
name_parts = filename.split()
if name_parts[0].lower() == "1black":
new_filename = f"1{color_name} {' '.join(name_parts[1:])}"
else:
new_filename = f"{color_name} {' '.join(name_parts[1:])}"
new_filepath = os.path.join(dir_auto, new_filename)
copy_image.save(new_filepath)
print(f"{new_filename} colorato")
# Break the loop after overlaying and renaming for each color
break
# Additional processing for specific filenames
for filename in os.listdir(dir_auto):
if filename.lower().endswith('.png') and filename.lower().split()[0] == "1black":
filepath = os.path.join(dir_auto, filename)
# Open the original image
original_image = Image.open(filepath).convert("RGBA")
# Step 2: Create copies and rename
for color_name, overlay_condition, overlay_color in [
("pink", lambda p: p[3] > 0, (252, 130, 216)),
("white", lambda p: p[3] > 0, (255, 255, 255)),
("blue", lambda p: p[3] > 0, (21, 31, 148)),
("red", lambda p: p[3] > 0, (168, 0, 0)),
("lightblue", lambda p: p[3] > 0, (99, 168, 231)),
("navyblue", lambda p: p[3] > 0, (4, 21, 64)),
("grey", lambda p: p[3] > 0, (134, 134, 134)),
("fuchsia", lambda p: p[3] > 0, (226, 46, 108)),
]:
copy_image = original_image.copy()
overlay_pixels(copy_image, overlay_condition, overlay_color)
name_parts = filename.split()
new_filename = f"1{color_name} {' '.join(name_parts[1:])}"
new_filepath = os.path.join(dir_auto, new_filename)
copy_image.save(new_filepath)
print(f"{new_filename} colorato")
# Break the loop after overlaying and renaming for each color
break
if filename.lower().endswith('.png') and filename.lower().split()[0] == "2white":
filepath = os.path.join(dir_auto, filename)
# Open the original image
original_image = Image.open(filepath).convert("RGBA")
# Step 2: Create copies and rename
for color_name, overlay_condition, overlay_color in [
("2pink", lambda p: p[3] > 0, (252, 130, 216)),
("2blue", lambda p: p[3] > 0, (21, 31, 148)),
("2red", lambda p: p[3] > 0, (168, 0, 0)),
("2black", lambda p: p[3] > 0, (0, 0, 0)),
]:
copy_image = original_image.copy()
overlay_pixels(copy_image, overlay_condition, overlay_color)
name_parts = filename.split()
new_filename = f"{color_name} {' '.join(name_parts[1:])}"
new_filepath = os.path.join(dir_auto, new_filename)
copy_image.save(new_filepath)
print(f"{new_filename} colorato")
I’d be very thankful if someone could help me one with this.
Here’s the results for example: