Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- import Image
- from math import sqrt, radians, degrees, atan, cos
- #try more difference functions
- #uses the CIE-L*ab color space and a formula to determine human perceived closest color (hopefully)
- #since i am using formulas from easyrgb.com
- #decided against using img.convert("RGB", (0.412453, 0.357580, 0.180423, 0,0.212671, 0.715160, 0.072169, 0,0.019334, 0.119193, 0.950227, 0))
- #since it returns different values to that of their calculators/formulas
- def rgb_to_xyz((r,g,b)):
- var_R = (r / 255)
- var_G = (g / 255)
- var_B = (b / 255)
- if var_R > 0.04045:
- var_R = ((var_R + 0.055) / 1.055)**2.4
- else:
- var_R = var_R / 12.92
- if var_G > 0.04045:
- var_G = ((var_G + 0.055) / 1.055)**2.4
- else:
- var_G = var_G / 12.92
- if var_B > 0.04045:
- var_B = ((var_B + 0.055) / 1.055)**2.4
- else:
- var_B = var_B / 12.92
- var_R = var_R * 100
- var_G = var_G * 100
- var_B = var_B * 100
- #//Observer. = 2degrees, Illuminant = D65
- x = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805
- y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722
- z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505
- return (x,y,z)
- def xyz_to_CIE((x,y,z)): #CIE-L*ab
- #//Observer. = 2degrees, Illuminant = D65
- ref_X = 95.047
- ref_Y = 100.000
- ref_Z = 108.883
- var_X = x / ref_X
- var_Y = y / ref_Y
- var_Z = z / ref_Z
- if var_X > 0.008856:
- var_X = var_X**(1/3)
- else:
- var_X = (7.787 * var_X) + (16 / 116)
- if var_Y > 0.008856:
- var_Y = var_Y**(1/3)
- else:
- var_Y = (7.787 * var_Y) + (16 / 116)
- if var_Z > 0.008856:
- var_Z = var_Z**(1/3)
- else:
- var_Z = (7.787 * var_Z) + (16 / 116)
- l = (116 * var_Y) - 16
- a = 500 * (var_X - var_Y)
- b = 200 * (var_Y - var_Z)
- return (l,a,b)
- #see if (hope) this works
- def delta_cmc((l_1,a_1,b_1),(l_2,a_2,b_2)): #(color1,color2) #useless cus of shitty sqrt() and complexnumbers, cant bring self to delete though
- #weight factors?? according to Delta E 1994 default is 1, cant go wrong with that
- WHT_L = 1
- WHT_C = 1
- xC1 = sqrt((a_1**2) + (b_1**2))
- xC2 = sqrt((a_2**2) + (b_2**2))
- xff = sqrt((xC1**4) / ((xC1**4) + 1900))
- xH1 = CieLab2Hue(a_1,b_1)
- if xH1 < 164 or xH1 > 345:
- #gonna assume dtor() is math.radians()
- xTT = 0.36 + abs(0.4 * cos(radians(35+xH1)))
- else:
- xTT = 0.56 + abs(0.2 * cos(radians(168 + xH1)))
- if l_1 < 16:
- xSL = 0.511
- else:
- xSL = (0.040975 * l_1) / (1 + (0.01765 * l_1))
- xSC = (( 0.0638 * xC1) / (1 + ( 0.0131 * xC1))) + 0.638
- xSH = ((xff * xTT) + 1 - xff) * xSC
- #problem line, negative numbers and sqrt cmath sqrt will be a complex number and could cause more probs, let us try delta e, rip time and effort
- xDH = sqrt((a_2 - a_1)**2 + (b_2 - b_1)**2 - (xC2 - xC1)**2)
- xSL = (l_2 - l_1) / WHT_L * xSL
- xSC = (xC2 - xC1) / WHT_C * xSC
- xSH = xDH / xSH
- difference = sqrt( xSL**2 + xSC**2 + xSH**2 )
- return difference
- def CieLab2Hue(var_a,var_b): #useless cus of shitty sqrt() and complexnumbers, cant bring self to delete though
- var_bias = 0
- if var_a >= 0 and var_b == 0:
- return 0
- if var_a < 0 and var_b == 0:
- return 180
- if var_a == 0 and var_b > 0:
- return 90
- if var_a == 0 and var_b < 0:
- return 270
- if var_a > 0 and var_b >0:
- var_bias = 0
- if var_a < 0:
- var_bias = 180
- if var_a > 0 and var_b < 0:
- var_bias = 360
- return (degrees(atan(var_b / var_a)) + var_bias)
- def delta_e((l_1,a_1,b_1),(l_2,a_2,b_2)):
- return sqrt(((l_1 - l_2)** 2) + ((a_1 - a_2)** 2) + ((b_1 - b_2)** 2))
- irc_col_dict = {
- 0: (100.0, 0.00526049995830391, -0.010408184525267927),
- 1: (0.0, 0.0, 0.0),
- 2: (19.337930164156077, 57.93988058702585, -78.91252810690023),
- 3: (33.749874811328155, -25.305934914705812, 17.830787565575545),
- 4: (50.127098188235095, 76.51560993021577, 64.2045794240172),
- 5: (38.44032625765076, 27.12131604833304, 12.218027820189882),
- 6: (26.74404347163763, 56.266786794809065, -41.54468176765732),
- 7: (55.39076120070011, 36.3170169909443, 63.583988733661755),
- 8: (90.65912922581248, -13.13248503845954, 87.21652464209376),
- 9: (78.05843999805951, -78.1436695202375, 75.4204196073617),
- 10: (41.5095009467383, -45.41465884551366, 45.96447959102795),
- 11: (91.11652110946342, -48.079618466228716, -14.138127754846131),
- 12: (31.871191585323395, 78.48934872890887, -106.900340057816),
- 13: (60.319933664076004, 98.25421868616114, -60.84298422386232),
- 14: (53.19277745493915, 0.003137832783772776, -0.006208372375593463),
- 15: (84.19846444703293, 0.004543913948662492, -0.008990380233764306)
- }
- img = Image.open('sprite.png')
- sprite_cols = img.getcolors()
- col_dict = {}
- for count ,color in sprite_cols:
- for k,v in irc_col_dict.iteritems():
- #print delta_e(color,v)
- tmp = dict([(delta_e(xyz_to_CIE(rgb_to_xyz(color)),v),k) for k,v in irc_col_dict.iteritems()])
- col_dict[color] = tmp[min(tmp)]
- pix = img.load()
- w,h = img.size
- ascii = [
- ''.join([
- '\x03%i,%i..' % (col_dict[pix[x,y]],col_dict[pix[x,y]]) for x in xrange(0,w)
- ]) for y in xrange(0,h)
- ]
- file = open('ascii.txt', 'w')
- for line in ascii:
- file.write('%s\n' % line)
- file.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement