-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
88 lines (76 loc) · 4.32 KB
/
convert.py
File metadata and controls
88 lines (76 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from PIL import Image
import numpy as np
import os
if os.path.exists("output.py"): # If an existing file exists
try:
os.remove("output.py") # Remove it
except:
"Unable to remove file!"
else: # Otheriwise
print("Creating empty file") # We're good!
selected_image = Image.open('image.png') # Could be upgraded to be an input
pix = selected_image.load() # Load the image
width, height = selected_image.size # Get the width and height
output_file = open("output.py", "a") # Set the output file
output_file.write("image_list = [") # Write the first char
resolution = int(input("Set your res (default is 2): ")) # Ask the user for the res
list_of_colors = [[0,0,0],[0,0,255],[165,42,42],[0,255,255],[255,215,0],[128,128,128],[0,128,0],[75,0,130],[255,165,0],[255,192,203],[128,0,128],[255,0,0],[238,130,238],[255,255,255],[250,255,0]]
list_of_names = ["black", "blue", "brown", "cyan", "gold", "gray", "green", "indigo", "orange", "pink", "purple", "red", "violet", "white", "yellow"]
def closest(colors,color):
colors = np.array(colors) # all the colors are now an array
color = np.array(color) # the current color is another array
distances = np.sqrt(np.sum((colors-color)**2,axis=1)) # get all the distances
index_of_smallest = np.where(distances==np.amin(distances)) # find the array with the smallest distance
smallest_distance = colors[index_of_smallest] # Get that distance
return smallest_distance #return the closest color
def get_RGB(m_column, m_row):
try: # Try getting the value at all
try:
red, green, blue, alpha = pix[m_column, m_row] # If the image has transparacy, four values will be passed
except:
red, green, blue = pix[m_column, m_row] # If not, only three (some images)
except:
red, green, blue = [255, 255, 255] # If unable to get any value at all, defaul to white (255,255,255)
rounded_rgb = ((closest(list_of_colors, [red,green,blue])).tolist())[0] # Round the RGB values to the nearest color
index_in_array = 0 # Index through the array of colors
for i in list_of_colors: # For every color in list_of_colors
#print(i) # Debug
if i == rounded_rgb: # if i in the list of colors matches the rounded RGB value
return_color = list_of_names[index_in_array] # Set the color to return to the item in the array with the corrosponding number
index_in_array = index_in_array + 1 # Add a number to the index value so we know what string-color we will select
return return_color # Finally return the color as a string
for row in range(0, height, resolution): # For every row
for column in range(0, width, resolution): # For every column
output_color = get_RGB(column, row) # Get the output color
print("Current Color: " + str(output_color) + ", Current line: " + str(row) + "/" + str(height) + " ", end="\r") # Inform the user
if(output_color != "white"): # If the color is white dont bother printing it
output_file.write("\"" + str(column) + " " + str(row) + " " + output_color + "\", ") # Save the list data as text
output_file.write("""]
# This code autogenerated by joe
import random
shuffleDraw = False # Change this to true to enable shuffled draw
if shuffleDraw:
random.shuffle(image_list)
speed(0) # Go at max speed
resolution = """ + str(resolution) + """ # Set to res of image
current_progress = 0.0
last_progress = 0
print("Starting...")
for current_pixel in image_list: # For every pixel
split_pixel = current_pixel.split() # split into values on " "
x = split_pixel[0] # The first value is x
y = 400 - int(split_pixel[1]) # The second value is y
selectColor = split_pixel[2] # The third value is color
current_progress = current_progress - 1 # Calculate Percentage
percentage = (100 * float(current_progress)/float(len(image_list))) * -1
if(last_progress != int(percentage)):
print("Progress: " + str(int(percentage)) + "%")
last_progress = int(percentage)
penup() # Bring up the cursor
setposition(int(x) - 200, int(y) - 200) # Go to the pixel location (tranformed by 200)
pendown() # Begin drawing
color(selectColor) # Set the color
pensize(resolution * 2) # Set the pen size
forward(1) # Go forward
print("Done") # Done!
""") # Print the entire interpeter code