Created
May 6, 2019 16:38
-
-
Save mllnd/8166ecea12c9213cb7bcf0e96f66f358 to your computer and use it in GitHub Desktop.
Python Flag Overlays
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, ImageDraw | |
# The RGBA opacity for flag stripes | |
# 0 is the lowest opacity, 255 highest (no opacity at all) | |
rgba_opacity = 155 | |
overlays = { | |
"EST": { | |
"orientation": "horizontal", | |
"colors": [ | |
(0, 114, 206, rgba_opacity), | |
(0, 0, 0, rgba_opacity), | |
(255, 255, 255, rgba_opacity), | |
] | |
}, | |
"FRA": { | |
"orientation": "vertical", | |
"colors": [ | |
(0, 35, 149, rgba_opacity), | |
(255, 255, 255, rgba_opacity), | |
(237, 41, 57, rgba_opacity), | |
] | |
}, | |
"ITA": { | |
"orientation": "vertical", | |
"colors": [ | |
(0, 146, 70, rgba_opacity), | |
(255, 255, 255, rgba_opacity), | |
(206, 43, 55, rgba_opacity) | |
] | |
}, | |
"DEU": { | |
"orientation": "horizontal", | |
"colors": [ | |
(0, 0, 0, rgba_opacity), | |
(221, 0, 0, rgba_opacity), | |
(255, 206, 0, rgba_opacity) | |
] | |
} | |
} | |
while True: | |
# Get the flag overlay from input | |
overlay = input("Which flag overlay do you want to apply? [" + ', '.join(list(overlays)) + "] ").upper() | |
if overlay in overlays: | |
break | |
else: | |
print("Invalid input! Please try again.") | |
# Open the source image in project directory | |
source_img = Image.open("base.jpg").convert("RGBA") | |
# Make a blank image for the rectangle, initialized to a transparent color. | |
tmp = Image.new("RGBA", source_img.size, (255, 255, 255, 0)) | |
# Create a drawing context for it. | |
draw = ImageDraw.Draw(tmp) | |
# Extract width and height from tuple for later use | |
width, height = source_img.size | |
orientation = overlays[overlay]["orientation"] | |
stripes = len(overlays[overlay]["colors"]) | |
# Draw the stripes on a temporary image. | |
# After that, merge the temporary image on the source image | |
counter = 0 | |
if orientation == "horizontal": | |
stripe = height / stripes | |
for x in overlays[overlay]["colors"]: | |
if counter == 0: | |
draw.rectangle(((0, 0), (width, stripe)), x) | |
else: | |
draw.rectangle(((0, stripe * counter), (width, stripe * (counter + 1))), x) | |
counter += 1 | |
else: | |
stripe = width / stripes | |
for x in overlays[overlay]["colors"]: | |
if counter == 0: | |
draw.rectangle(((0, 0), (stripe, height)), x) | |
else: | |
draw.rectangle(((stripe * counter, 0), (stripe * (counter + 1), height)), x) | |
counter += 1 | |
# Merge the source image and temporary image | |
# We need to convert it to RGB to save it as JPG | |
out = Image.alpha_composite(source_img, tmp).convert("RGB") | |
out.save("overlay_"+overlay+".jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment