11from PIL import Image
2+ import numpy as np
23
34selected_image = Image .open ('image.png' )
45pix = selected_image .load ()
56width , height = selected_image .size
6- output_file = open ("output.txt" , "a" )
7- output_file .write ("[" )
8- resolution = 2
7+ output_file = open ("output.py" , "a" )
8+ output_file .write ("image_list = [" )
9+ resolution = int (input ("Set your res (default is 2): " ))
10+
11+ list_of_colors = [[255 ,0 ,0 ],[0 ,255 ,0 ],[0 ,0 ,255 ],[255 ,255 ,255 ],[0 ,0 ,0 ],[255 ,255 ,0 ],[0 ,255 ,255 ],[255 ,0 ,255 ],[100 ,100 ,100 ]]
12+ list_of_names = ["Red" , "Green" , "Blue" , "White" , "Black" ,"Yellow" ,"Cyan" ,"Purple" ,"Grey" ]
13+
14+ def closest (colors ,color ):
15+ colors = np .array (colors ) # all the colors are now an array
16+ color = np .array (color ) # the current color is another array
17+ distances = np .sqrt (np .sum ((colors - color )** 2 ,axis = 1 )) # get all the distances
18+ index_of_smallest = np .where (distances == np .amin (distances )) # find the array with the smallest distance
19+ smallest_distance = colors [index_of_smallest ] # Get that distance
20+ return smallest_distance #return the closest color
921
1022for row in range (0 , height , resolution ):
1123 for column in range (0 , width , resolution ):
1224 try :
1325 red , green , blue , alpha = pix [column , row ]
1426 except :
1527 red , green , blue = pix [column , row ]
16- print (str (red ) + " " + str (green ) + " " + str (blue ))
28+ # print (str(red) + " " + str(green) + " " + str(blue))
1729
18- #if blue > 0 and green < blue and red < blue:
19- if blue < 100 and green > 30 and red < 100 :
20- output_color = "Green"
21- output_file .write ("\" " + str (column ) + " " + str (row ) + " " + output_color + "\" , " )
22- elif blue < 30 and green < 30 and red < 30 :
23- output_color = "Black"
24- output_file .write ("\" " + str (column ) + " " + str (row ) + " " + output_color + "\" , " )
25- elif blue < 30 and green > 30 and red > 30 :
26- output_color = "Yellow"
30+ #print([red, green, blue])
31+ closest_color = ((closest (list_of_colors , [red ,green ,blue ])).tolist ())[0 ]
32+ #print(closest_color)
33+
34+ index_in_array = 0
35+ for i in list_of_colors :
36+ #print(i)
37+ if i == closest_color :
38+ output_color = list_of_names [index_in_array ]
39+ index_in_array = index_in_array + 1
40+
41+ print (output_color )
42+ if (output_color != "White" ):
2743 output_file .write ("\" " + str (column ) + " " + str (row ) + " " + output_color + "\" , " )
28- output_file .write ("]" )
44+ output_file .write ("""]
45+
46+ # This code autogenerated by joe
47+ speed(0)
48+ resolution = """ + str (resolution ) + """
49+ for current_pixel in image_list:
50+ split_pixel = current_pixel.split()
51+ x = split_pixel[0]
52+ y = 400 - int(split_pixel[1])
53+ selectColor = split_pixel[2]
54+
55+ penup()
56+ setposition(int(x) - 200, int(y) - 200)
57+ pendown()
58+ color(selectColor)
59+ pensize(resolution * 2)
60+ forward(resolution)
61+ print("Done")""" )
0 commit comments