Created
June 29, 2017 23:41
-
-
Save dominicgs/dae1d29a9fb4d7d0592bea35398d4fa1 to your computer and use it in GitHub Desktop.
Remap OSH Park PCB preview images to other soldermask and silkscreen colours
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
#!/usr/bin/env python3 | |
import argparse | |
import struct | |
from PIL import Image | |
if __name__ == "__main__": | |
colour_map = { | |
(193, 177, 203) : (193, 177, 203),# silkscreen | |
(197, 183, 208) : (197, 183, 208), # silkscreen + copper | |
(88, 88, 87) : (88, 88, 87), # PCB substrate | |
(0, 0, 0) : (0, 0, 0), # black background | |
(51, 0, 85) : (51, 0, 85), # soldermask | |
(177, 136, 131) : (177, 136, 131), # copper | |
(64, 18, 100) : (64, 18, 100) # soldermask + copper | |
} | |
parser = argparse.ArgumentParser(description="Utility for remapping OSH Park preview image colours") | |
parser.add_argument('-i', dest='input', metavar='<input>', | |
required=True, type=str, help="Input file") | |
parser.add_argument('-o', dest='output', metavar='<output>', | |
type=str, help="Output file") | |
parser.add_argument('-m', dest='mask', metavar='<mask>', | |
type=str, help="Soldermask colour in hex format") | |
parser.add_argument('-s', dest='silkscreen', metavar='<silkscreen>', | |
type=str, help="Silkscreen colour in hex format") | |
args = parser.parse_args() | |
if args.mask: | |
r, g, b = struct.unpack('BBB',bytes.fromhex(args.mask)) | |
colour_map[(51, 0, 85)] = (r,g,b) | |
rn = r + 13 | |
gn = g + 18 | |
bn = b + 15 | |
colour_map[(64, 18, 100)] = (rn,gn,bn) | |
if args.silkscreen: | |
r, g, b = struct.unpack('BBB',bytes.fromhex(args.silkscreen)) | |
colour_map[(193, 177, 203)] = (r,g,b) | |
rn = r + 4 | |
gn = g + 6 | |
bn = b + 5 | |
colour_map[(197, 183, 208)] = (rn,gn,bn) | |
im = Image.open(args.input) | |
print(im.format, im.size, im.mode) | |
out_img = Image.new('RGBA', im.size, (0,0,0,0)) | |
pixels = out_img.load() | |
rgba_im = im.convert('RGBA') | |
for x in range(im.size[0]): | |
for y in range(im.size[1]): | |
r, g, b, a = rgba_im.getpixel((x, y)) | |
if a != 0: | |
rn, gn, bn = colour_map[(r,g,b)] | |
pixels[x,y] = (rn, gn, bn, a) | |
if args.output: | |
out_img.save(args.output) | |
else: | |
out_img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment