Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created November 23, 2023 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncalm/f7884fe128b425755f52daec56cf7b62 to your computer and use it in GitHub Desktop.
Save ncalm/f7884fe128b425755f52daec56cf7b62 to your computer and use it in GitHub Desktop.
Code for dot-matrix Turkey Excel pixel-art
Sub ColorCellsFromHex()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("test") ' Change to your sheet's name
Dim i As Integer, j As Integer
Dim hexColor As String
For i = 1 To 50 ' Assuming 50 rows
For j = 1 To 50 ' Assuming 50 columns
hexColor = ws.Cells(i, j).Value ' Get the hex color code from the cell
If Len(hexColor) = 7 And hexColor <> "#fefefe" Then ' Check if the hex code is the correct length and not white
ws.Cells(i, j).Interior.Color = RGB(Application.WorksheetFunction.Hex2Dec(Mid(hexColor, 2, 2)), _
Application.WorksheetFunction.Hex2Dec(Mid(hexColor, 4, 2)), _
Application.WorksheetFunction.Hex2Dec(Mid(hexColor, 6, 2)))
ws.Cells(i, j).Font.Color = ws.Cells(i, j).Interior.Color ' Set the font color to match the cell color
End If
Next j
Next i
End Sub
import numpy as np
import pandas as pd
def image_to_excel_color_codes(image_path, pixel_width):
# Open the image
img = Image.open(image_path)
# Calculate the new height to maintain aspect ratio
aspect_ratio = img.height / img.width
new_height = int(pixel_width * aspect_ratio)
# Resize the image while maintaining aspect ratio
img_resized = img.resize((pixel_width, new_height))
# Convert image to RGB (if it's not already in that format)
img_rgb = img_resized.convert('RGB')
# Create an array of RGB values
rgb_array = np.array(img_rgb)
# Convert the RGB values to hexadecimal
hex_array = np.vectorize(lambda r, g, b: '#{:02x}{:02x}{:02x}'.format(r, g, b))(rgb_array[:,:,0], rgb_array[:,:,1], rgb_array[:,:,2])
# Convert to a DataFrame
df_hex = pd.DataFrame(hex_array)
# Save the DataFrame to a CSV file
csv_path = image_path.rsplit('.', 1)[0] + '_colors.csv'
df_hex.to_csv(csv_path, index=False, header=False)
return csv_path, img_resized
# Call the function with the provided image and desired pixel width
csv_file_path, processed_img = image_to_excel_color_codes('path/to/image', 50)
# Show the resized image and return the path to the CSV file
processed_img.show(), csv_file_path
https://i.pinimg.com/564x/4f/8b/ae/4f8bae9c94e9dfc8d67e4b11714c531d.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment