Skip to content

Joes fancy image converter #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bb332c3
This program converts a whole image to a string, pretty nifty eh?
KenwoodFox Nov 5, 2019
3f77663
This stuff works bby
KenwoodFox Nov 5, 2019
54c1ff0
it works here!
KenwoodFox Nov 5, 2019
d0d4ceb
rendered mcc
KenwoodFox Nov 5, 2019
f989f92
doot
KenwoodFox Nov 5, 2019
7d99dd6
Here aidan, this will convert a bitmap for u <3
KenwoodFox Nov 6, 2019
ddbd68d
rock and roll and other lateral transforms
KenwoodFox Nov 7, 2019
5b394ad
Add files via upload
boinary Nov 12, 2019
46f1d1f
Image render now does the WHOLE THING
KenwoodFox Nov 13, 2019
f31b1ef
Update as release!
KenwoodFox Nov 13, 2019
50ab252
Added all compatable codehs colors!
KenwoodFox Nov 13, 2019
544e7de
Add ability to shuffle list
KenwoodFox Nov 13, 2019
73e8e63
Deprciate shuffle_list.py
KenwoodFox Nov 13, 2019
f91e374
Add default image as CE logo
KenwoodFox Nov 13, 2019
2947787
Add default code to repo
KenwoodFox Nov 13, 2019
efb7b0c
Remove old images
KenwoodFox Nov 13, 2019
4aad69a
Add docs
KenwoodFox Nov 13, 2019
7e874ec
Update readme.md
KenwoodFox Nov 13, 2019
4b9b081
Create requirements.txt for easy instalation
KenwoodFox Nov 14, 2019
50aaa07
Add option to remove existing file
KenwoodFox Nov 14, 2019
b8502f6
Add loading bar
KenwoodFox Nov 14, 2019
fd16477
Hotfix the file removal
KenwoodFox Nov 14, 2019
fbb0ee6
Update output.py
KenwoodFox Nov 14, 2019
c8333c0
Now it outputs the progres yay and cool
KenwoodFox Nov 14, 2019
92f0e5c
Only go forward one px instead of `res`
KenwoodFox Nov 14, 2019
a367364
Rearrange something
KenwoodFox Nov 15, 2019
c93311c
Make program production Ready
KenwoodFox Nov 15, 2019
80ccf9d
Tune color values
KenwoodFox Nov 17, 2019
2e083c8
Fix Syntax
KenwoodFox Nov 24, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Image-Dict/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Binary file added Image-Dict/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions Image-Dict/output.py

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Image-Dict/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#You need `numpy` and `PIL` to use this software.

To use: Replace the default image.png with your own image.

Scale your image to a width of 400 (height can be other)

Delete an existing output.py if it exists

Run convert.py

wait for the program to finish, you should see a lot of scrolling text.

Once completed, copy and paste the contents of output.py into codehs

press run

tada!
2 changes: 2 additions & 0 deletions Image-Dict/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.17.2
Pillow==6.2.0