Created
September 28, 2015 13:42
-
-
Save sattvikc/0e219bc3c3db1b3ab4be to your computer and use it in GitHub Desktop.
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
def __mix(back, front, alpha): | |
result = 0 | |
result += round(front * (alpha / 255)) | |
result += round(back * ((255-alpha) / 255)) | |
return result | |
colordic = {} | |
for f in range(256): | |
for a in range(256): | |
v1 = __mix(0, f, a) | |
v2 = __mix(255, f, a) | |
k = (v1, v2) | |
if not k in colordic: | |
colordic[k] = (f, a) | |
elif colordic[k][1] == a: | |
pass # we are still okay | |
else: | |
pass # This is an error case, but I don't know what to do! | |
from PIL import Image | |
def generate_transparent_image(black_img, white_img, out_img): | |
img1 = Image.open(black_img) | |
img2 = Image.open(white_img) | |
img3 = Image.new('RGBA', img1.size, (0,0,0,0)) | |
px1 = img1.load() | |
px2 = img2.load() | |
px3 = img3.load() | |
for x in range(img1.size[0]): | |
for y in range(img1.size[1]): | |
rgb1 = px1[x,y] | |
rgb2 = px2[x,y] | |
v1 = rgb1[0] | |
v2 = rgb2[0] | |
a = colordic[(v1,v2)][1] | |
px3[x,y] = rgb1[:3] + (a,) | |
img3.save(out_img) | |
def generate_alpha_image(black_img, white_img, out_img): | |
img1 = Image.open(black_img) | |
img2 = Image.open(white_img) | |
img3 = Image.new('RGBA', img1.size, (0,0,0,0)) | |
px1 = img1.load() | |
px2 = img2.load() | |
px3 = img3.load() | |
for x in range(img1.size[0]): | |
for y in range(img1.size[1]): | |
rgb1 = px1[x,y] | |
rgb2 = px2[x,y] | |
v1 = rgb1[0] | |
v2 = rgb2[0] | |
a = colordic[(v1,v2)][1] | |
px3[x,y] = (a,a,a,255) | |
img3.save(out_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment