import os import os.path import sys #print "This is the name of the script: ", sys.argv[0] from PIL import Image, ImageOps import datetime from random import randint as rand import numpy workdir = "./" target_filename = sys.argv[1] infilepath = workdir + "in/" outfilepath = workdir + "out/" #materialsdir = workdir # + "leaf/" target = Image.open(infilepath + target_filename).convert('RGBA') #print(target) #leaf = Image.open(materialsdir + "leaf.jpg").convert('RGBA')#.convert("L").point(lambda x: min(x, 245)) """ transform = { "Protanopia": { "R":[3, 2, 0], "G":[3, 2, 0], "B":[0, 1, 3] } } """ """ transform = { "Protanopia": { "R":[6, 4, 0], "G":[6, 4, 0], "B":[0, 2, 8] } } """ transform = { "Protanopia": { "R":[56.667/100.0, 43.333/100.0, 0], "G":[55.833/100.0, 44.167/100.0, 0], "B": [0, 24.167/100.0, 75.833/100.0] }, "Deuteranopia": { "R":[62.5/100.0, 37.5/100.0, 0], "G":[70/100.0, 30/100.0, 0], "B":[0, 30/100.0, 70/100.0] } } """ transform = { "Protanopia": { "R":[56.667, 43.333, 0], "G":[55.833, 44.167, 0], "B": [0, 24.167, 75.833] } } """ print(transform) arr = numpy.array(target) #print(arr) #print(arr.size) #print(arr[0]) R = transform["Protanopia"]["R"] G = transform["Protanopia"]["G"] B = transform["Protanopia"]["B"] """ R = transform["Deuteranopia"]["R"] G = transform["Deuteranopia"]["G"] B = transform["Deuteranopia"]["B"] """ #pretime = datetime.datetime.now() #print(pretime) #print(type(arr[0][0][0])) #it's uint8 """ for b in arr: for c in b: r = c[0] g = c[1] b = c[2] c[0] = ((r >> R[0]) + (g >> R[1]) ) #/ 100.0 c[1] = ((r >> G[0]) + (g >> G[1]) ) #/ 100.0 c[2] = ( (g >> B[1]) + (b >> B[2])) #/ 100.0 """ """ for b in arr: #print(".", end = '') for c in b: r = c[0] g = c[1] b = c[2] c[0] = (r * R[0] + g * R[1] ) #/ 100.0 c[1] = (r * G[0] + g * G[1] ) #/ 100.0 c[2] = ( g * B[1] + b * B[2]) #/ 100.0 #print(";") posttime = datetime.datetime.now() print(posttime) print("conversion time: {} seconds".format((posttime - pretime).seconds)) """ """ for b in arr: #print(b) print(".", end = '') for c in b: #print(c) r = c[0] g = c[1] b = c[2] c[0] = (r * R[0] + g * R[1] + b * R[2]) / 100.0 c[1] = (r * G[0] + g * G[1] + b * G[2]) / 100.0 c[2] = (r * B[0] + g * B[1] + b * B[2]) / 100.0 """ """ for b in arr: for c in b: r = c[0] g = c[1] b = c[2] r_new = (c[0] * R[0] + c[1] * R[1] + c[2] * R[2]) / 100.0 g_new = (c[0] * G[0] + c[1] * G[1] + c[2] * G[2]) / 100.0 b_new = (c[0] * B[0] + c[1] * B[1] + c[2] * B[2]) / 100.0 c[0] = r_new c[1] = g_new c[2] = b_new """ #print(arr[-1]) #leaf = leaf.resize(target.size) #leaf = ImageOps.fit(leaf, target.size, Image.ANTIALIAS) pretime = datetime.datetime.now() print(pretime) target.convert('RGBA') [X, Y] = target.size pixels = target.load() for x in range(0, X): for y in range(0, Y): pixel = [pixels[x,y][0], pixels[x,y][1], pixels[x,y][2], pixels[x,y][3]] r = pixel[0] g = pixel[1] b = pixel[2] """ pixel[0] = ((r >> R[0]) + (g >> R[1]) ) #/ 100.0 pixel[1] = ((r >> G[0]) + (g >> G[1]) ) #/ 100.0 pixel[2] = ( (g >> B[1]) + (b >> B[2])) #/ 100.0 """ pixel[0] = int(r * R[0] + g * R[1] ) #/ 100.0 pixel[1] = int(r * G[0] + g * G[1] ) #/ 100.0 pixel[2] = int( g * B[1] + b * B[2]) #/ 100.0 pixels[x,y] = tuple(pixel) posttime = datetime.datetime.now() print(posttime) print("conversion time: {} seconds".format((posttime - pretime).seconds)) #if leaf.size > target.size: # leaf.thumbnail(target.size, Image.ANTIALIAS) #else: # leaf.resize(target.size, Image.ANTIALIAS, validate=False) #mask = leaf.convert("L").point(lambda x: min(x, 100)) #leaf.putalpha(mask) #target.paste(leaf, (0, 0), leaf) #target = Image.fromarray(arr) target.convert('RGBA') extensionless = '.'.join(target_filename.split('.')[0:-1]) target.save(os.path.expanduser(outfilepath + extensionless + ".png"))