Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. from __future__ import division
  2. import Image
  3. from math import sqrt, radians, degrees, atan, cos
  4.  
  5. #try more difference functions
  6.  
  7. #uses the CIE-L*ab color space and a formula to determine human perceived closest color (hopefully)
  8.  
  9. #since i am using formulas from easyrgb.com
  10. #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))
  11. #since it returns different values to that of their calculators/formulas
  12. def rgb_to_xyz((r,g,b)):
  13.     var_R = (r / 255)
  14.     var_G = (g / 255)
  15.     var_B = (b / 255)
  16.    
  17.     if var_R > 0.04045:
  18.         var_R = ((var_R + 0.055) / 1.055)**2.4
  19.     else:
  20.         var_R = var_R / 12.92
  21.     if var_G > 0.04045:
  22.         var_G = ((var_G + 0.055) / 1.055)**2.4
  23.     else:
  24.         var_G = var_G / 12.92
  25.     if var_B > 0.04045:
  26.         var_B = ((var_B + 0.055) / 1.055)**2.4
  27.     else:
  28.         var_B = var_B / 12.92
  29.        
  30.     var_R = var_R * 100
  31.     var_G = var_G * 100
  32.     var_B = var_B * 100
  33.    
  34.     #//Observer. = 2degrees, Illuminant = D65
  35.     x = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805
  36.     y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722
  37.     z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505
  38.    
  39.     return (x,y,z)
  40.    
  41. def xyz_to_CIE((x,y,z)): #CIE-L*ab
  42.     #//Observer. = 2degrees, Illuminant = D65
  43.     ref_X = 95.047
  44.     ref_Y = 100.000
  45.     ref_Z = 108.883
  46.    
  47.     var_X = x / ref_X
  48.     var_Y = y / ref_Y
  49.     var_Z = z / ref_Z
  50.    
  51.     if var_X > 0.008856:
  52.         var_X = var_X**(1/3)
  53.     else:
  54.         var_X = (7.787 * var_X) + (16 / 116)
  55.     if var_Y > 0.008856:
  56.         var_Y = var_Y**(1/3)
  57.     else:
  58.         var_Y = (7.787 * var_Y) + (16 / 116)
  59.     if var_Z > 0.008856:
  60.         var_Z = var_Z**(1/3)
  61.     else:
  62.         var_Z = (7.787 * var_Z) + (16 / 116)
  63.        
  64.     l = (116 * var_Y) - 16
  65.     a = 500 * (var_X - var_Y)
  66.     b = 200 * (var_Y - var_Z)
  67.    
  68.     return (l,a,b)
  69.    
  70. #see if (hope) this works
  71. 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
  72.     #weight factors?? according to Delta E 1994 default is 1, cant go wrong with that
  73.     WHT_L = 1
  74.     WHT_C = 1
  75.    
  76.     xC1 = sqrt((a_1**2) + (b_1**2))
  77.     xC2 = sqrt((a_2**2) + (b_2**2))
  78.    
  79.     xff = sqrt((xC1**4) / ((xC1**4) + 1900))
  80.     xH1 = CieLab2Hue(a_1,b_1)
  81.    
  82.     if xH1 < 164 or xH1 > 345:
  83.         #gonna assume dtor() is math.radians()
  84.         xTT = 0.36 + abs(0.4 * cos(radians(35+xH1)))
  85.     else:
  86.         xTT = 0.56 + abs(0.2 * cos(radians(168 + xH1)))
  87.  
  88.     if l_1 < 16:
  89.         xSL = 0.511
  90.     else:
  91.         xSL = (0.040975 * l_1) / (1 + (0.01765 * l_1))
  92.  
  93.     xSC = (( 0.0638 * xC1) / (1 + ( 0.0131 * xC1))) + 0.638
  94.     xSH = ((xff * xTT) + 1 - xff) * xSC
  95.     #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
  96.     xDH = sqrt((a_2 - a_1)**2 + (b_2 - b_1)**2 - (xC2 - xC1)**2)
  97.     xSL = (l_2 - l_1) / WHT_L * xSL
  98.     xSC = (xC2 - xC1) / WHT_C * xSC
  99.     xSH = xDH / xSH
  100.    
  101.     difference = sqrt( xSL**2 + xSC**2 + xSH**2 )
  102.    
  103.     return difference
  104.    
  105. def CieLab2Hue(var_a,var_b): #useless cus of shitty sqrt() and complexnumbers, cant bring self to delete though
  106.     var_bias = 0
  107.     if var_a >= 0 and var_b == 0:
  108.         return 0
  109.     if var_a < 0 and var_b == 0:
  110.         return 180
  111.     if var_a == 0 and var_b > 0:
  112.         return 90
  113.     if var_a == 0 and var_b < 0:
  114.         return 270
  115.     if var_a > 0 and var_b >0:
  116.         var_bias = 0
  117.     if var_a < 0:
  118.         var_bias = 180
  119.     if var_a > 0 and var_b < 0:
  120.         var_bias = 360
  121.     return (degrees(atan(var_b / var_a)) + var_bias)
  122.    
  123.    
  124.    
  125. def delta_e((l_1,a_1,b_1),(l_2,a_2,b_2)):
  126.     return sqrt(((l_1 - l_2)** 2) + ((a_1 - a_2)** 2) + ((b_1 - b_2)** 2))
  127.  
  128. irc_col_dict = {
  129.     0: (100.0, 0.00526049995830391, -0.010408184525267927),
  130.     1: (0.0, 0.0, 0.0),
  131.     2: (19.337930164156077, 57.93988058702585, -78.91252810690023),
  132.     3: (33.749874811328155, -25.305934914705812, 17.830787565575545),
  133.     4: (50.127098188235095, 76.51560993021577, 64.2045794240172),
  134.     5: (38.44032625765076, 27.12131604833304, 12.218027820189882),
  135.     6: (26.74404347163763, 56.266786794809065, -41.54468176765732),
  136.     7: (55.39076120070011, 36.3170169909443, 63.583988733661755),
  137.     8: (90.65912922581248, -13.13248503845954, 87.21652464209376),
  138.     9: (78.05843999805951, -78.1436695202375, 75.4204196073617),
  139.     10: (41.5095009467383, -45.41465884551366, 45.96447959102795),
  140.     11: (91.11652110946342, -48.079618466228716, -14.138127754846131),
  141.     12: (31.871191585323395, 78.48934872890887, -106.900340057816),
  142.     13: (60.319933664076004, 98.25421868616114, -60.84298422386232),
  143.     14: (53.19277745493915, 0.003137832783772776, -0.006208372375593463),
  144.     15: (84.19846444703293, 0.004543913948662492, -0.008990380233764306)
  145. }
  146.  
  147. img = Image.open('sprite.png')
  148. sprite_cols = img.getcolors()
  149.  
  150. col_dict = {}
  151. for count ,color in sprite_cols:
  152.     for k,v in irc_col_dict.iteritems():
  153.         #print delta_e(color,v)
  154.         tmp = dict([(delta_e(xyz_to_CIE(rgb_to_xyz(color)),v),k) for k,v in irc_col_dict.iteritems()]) 
  155.         col_dict[color] = tmp[min(tmp)]
  156.  
  157. pix = img.load()
  158. w,h = img.size
  159. ascii = [
  160.     ''.join([
  161.         '\x03%i,%i..' % (col_dict[pix[x,y]],col_dict[pix[x,y]]) for x in xrange(0,w)
  162.     ]) for y in xrange(0,h)
  163. ]
  164.  
  165. file = open('ascii.txt', 'w')
  166. for line in ascii:
  167.     file.write('%s\n' % line)
  168. file.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement