Created
January 15, 2025 19:45
-
-
Save rcastill/11cb95272173ca8c41a149084d605faf to your computer and use it in GitHub Desktop.
Draw polygons to input image. Output image to stdout. Can pipe to a file or image magick's display tool for example.
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
import sys | |
import itertools | |
import json | |
import argparse | |
from PIL import Image, ImageDraw | |
def parse_args(): | |
parser = argparse.ArgumentParser(usage="python3 drawpoly.py -p p1.json ... <input_image >out.jpg") | |
parser.add_argument("-p", "--poly-jsons", type=str, required=True, nargs="+", help=""" | |
JSON file(s) path containing polygon to draw. | |
Format: { points: [{x: int, y: int}, ...] }""") | |
return parser.parse_args() | |
def poly(polyj): | |
abs_points = polyj["points"] | |
assert isinstance(abs_points, list) and len(abs_points) > 2, "invalid polygon" | |
return [ | |
(p["x"], p["y"]) | |
for p in abs_points | |
] | |
def draw_poly(img, points, color): | |
img2 = img.copy() | |
draw = ImageDraw.Draw(img2) | |
draw.polygon(points, fill = color) | |
return Image.blend(img, img2, 0.5) | |
def main(): | |
args = parse_args() | |
img = Image.open(sys.stdin.buffer).convert("RGBA") | |
polys = [] | |
for poly_json in args.poly_jsons: | |
with open(poly_json, encoding="utf-8") as polyf: | |
polyj = json.load(polyf) | |
points = poly(polyj) | |
print("points", points, file=sys.stderr) | |
polys.append(points) | |
for points, color in zip(polys, itertools.cycle(COLORS)): | |
img = draw_poly(img, points, color) | |
img.save(sys.stdout, format="png") | |
COLORS = [ | |
"DarkSalmon", | |
"ForestGreen", | |
"AliceBlue", | |
"Azure", | |
"Blue", | |
"AntiqueWhite", | |
"Aquamarine", | |
"Beige", | |
"Brown", | |
"BlanchedAlmond", | |
"BlueViolet", | |
"BurlyWood", | |
"CadetBlue", | |
"Chartreuse", | |
"Chocolate", | |
"Coral", | |
"CornflowerBlue", | |
"Cornsilk", | |
"Crimson", | |
"Cyan", | |
"DarkBlue", | |
"DarkCyan", | |
"DarkGoldenRod", | |
"DarkGray", | |
"DarkGrey", | |
"DarkGreen", | |
"DarkKhaki", | |
"DarkMagenta", | |
"DarkOliveGreen", | |
"DarkOrange", | |
"DarkOrchid", | |
"DarkRed", | |
"DarkSeaGreen", | |
"DarkSlateBlue", | |
"DarkSlateGray", | |
"DarkSlateGrey", | |
"DarkTurquoise", | |
"DarkViolet", | |
"DeepPink", | |
"DeepSkyBlue", | |
"DimGray", | |
"DimGrey", | |
"DodgerBlue", | |
"FireBrick", | |
"FloralWhite", | |
"Fuchsia", | |
"Gainsboro", | |
"GhostWhite", | |
"Gold", | |
"GoldenRod", | |
"Gray", | |
"Grey", | |
"Green", | |
"GreenYellow", | |
"HoneyDew", | |
"HotPink", | |
"IndianRed", | |
"Indigo", | |
"Ivory", | |
"Khaki", | |
"Lavender", | |
"LavenderBlush", | |
"LawnGreen", | |
"LemonChiffon", | |
"LightBlue", | |
"LightCoral", | |
"LightCyan", | |
"LightGoldenRodYellow", | |
"LightGray", | |
"LightGrey", | |
"LightGreen", | |
"LightPink", | |
"LightSalmon", | |
"LightSeaGreen", | |
"LightSkyBlue", | |
"LightSlateGray", | |
"LightSlateGrey", | |
"LightSteelBlue", | |
"LightYellow", | |
"Lime", | |
"LimeGreen", | |
"Linen", | |
"Magenta", | |
"Maroon", | |
"MediumAquaMarine", | |
"MediumBlue", | |
"MediumOrchid", | |
"MediumPurple", | |
"MediumSeaGreen", | |
"MediumSlateBlue", | |
"MediumSpringGreen", | |
"MediumTurquoise", | |
"MediumVioletRed", | |
"MidnightBlue", | |
"MintCream", | |
"MistyRose", | |
"Moccasin", | |
"NavajoWhite", | |
"Navy", | |
"OldLace", | |
"Olive", | |
"OliveDrab", | |
"Orange", | |
"OrangeRed", | |
"Orchid", | |
"PaleGoldenRod", | |
"PaleGreen", | |
"PaleTurquoise", | |
"PaleVioletRed", | |
"PapayaWhip", | |
"PeachPuff", | |
"Peru", | |
"Pink", | |
"Plum", | |
"PowderBlue", | |
"Purple", | |
"RebeccaPurple", | |
"Red", | |
"RosyBrown", | |
"RoyalBlue", | |
"SaddleBrown", | |
"Salmon", | |
"SandyBrown", | |
"SeaGreen", | |
"SeaShell", | |
"Sienna", | |
"Silver", | |
"SkyBlue", | |
"SlateBlue", | |
"SlateGray", | |
"SlateGrey", | |
"Snow", | |
"SpringGreen", | |
"SteelBlue", | |
"Tan", | |
"Teal", | |
"Thistle", | |
"Tomato", | |
"Turquoise", | |
"Violet", | |
"Wheat", | |
"White", | |
"WhiteSmoke", | |
"Yellow", | |
"YellowGreen" | |
] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment