-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-masks-to-images.py
More file actions
25 lines (20 loc) · 1.01 KB
/
Copy pathadd-masks-to-images.py
File metadata and controls
25 lines (20 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from PIL import Image, ImageChops
import os
# Paths to your folders
image_dir = '/mnt/c/Users/tomhol/Documents/Zephyr/brouk'
mask_dir = '/mnt/c/Users/tomhol/Documents/Zephyr/brouk'
output_dir = '/mnt/c/Users/tomhol/Documents/Zephyr/brouk2'
for filename in os.listdir(image_dir):
if filename.endswith(".jpg"):
img = Image.open(os.path.join(image_dir, filename)).convert("RGB")
# Assuming mask has the same name but .png extension
mask = Image.open(os.path.join(mask_dir, filename.replace(".jpg", ".mask.png"))).convert("RGB")
# If masks are smaller, resize to match the image size
mask = mask.resize(img.size)
# multiply the image with the mask so the masked areas become black
img = ImageChops.multiply(img, mask)
# and now save the file as jpg with the masked areas as solid black
img = img.convert("RGB")
# set the jpg quality to 95
img.save(os.path.join(output_dir, filename), quality=95)
print(f"Processed {filename}")