Some colors are messed up, please let me know if you see a problem in the code takes a sec to load
code: """ generate a 360x480 array of hex codes from an image """ from PIL import Image import os import numpy as np # Counter from collections import Counter codes = [i for i in "ՎՏՐՑՒՓՔՕՖՙ՚՛՜՝՞՟ՠաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆևֈ։֊֍֎֏ְֱֲֳִֵֶַָֹֺֻּֽ֑֖֛֢֣֤֥֦֧֪֚֭֮֒֓֔֕֗֘֙֜֝֞֟֠֡֨֩֫֬֯־ֿ׀ׁׂ׃ׅׄ׆ׇאבגדהוזחטיךכלםמןנסעףפץצקרשתׯװױײ׳״؆؇؈؉؊؋،؍؎؏ؘؙؚؐؑؒؓؔؕؖؗ؛؝؞؟ؠءآأؤإئابةتثجحخدذرزسشصضطظعغػؼؽؾؿـفقكلمنهوىيًٌٍَُِّْٕٖٜٟٓٔٗ٘ٙٚٛٝٞ٠١٢٣٤٥٦٧٨٩٪٫٬٭ٮٯٰ"] print(codes) def get_hex_codes(image_path, resolution=1): global colors assert 480 % resolution == 0, "Res must be a factor of 480 and 360" assert 360 % resolution == 0, "Res must be a factor of 480 and 360" """ Retrieves an array of hexadecimal color codes from an image. Args: image_path: Path to the image file. Returns: A list of lists, where each inner list represents a row of pixels and contains the hexadecimal color codes for that pixel. """ img = Image.open(image_path) img = img.convert("RGB").resize((int(480/resolution), int(360/resolution))) # Ensure the image is in RGB format width, height = img.size hex_codes = [] for y in range(int(height)): row_codes = [] for x in range(int(width)): r, g, b = img.getpixel((x, y)) hex_code = codes[r] + codes[g] + codes[b] row_codes.append(hex_code) hex_codes.append(row_codes) return hex_codes def save(resolution, x, colors): with open(f"hexes_res_{resolution}.txt", "w", encoding="utf-8") as f: for arr in x: for row in arr: for color in row: f.write(color) f.write(" ") f.write("!") f.write("\n") with open(f"hexes_res_{resolution}_cs.txt", "w", encoding="utf-8") as f: for color in colors: f.write(color) f.write("\n") if __name__ == "__main__": resolution = 10 image_folder = os.path.dirname(__file__) + "/images/" hex_arrays = [] colors = [] print(list(os.walk(image_folder))[0]) for file in list(os.walk(image_folder))[0][2]: print(file) """if not file.endswith((".jpg", ".jfif", ".png", ".JPG", ".jpeg")): continue""" hex_array = get_hex_codes(image_folder + file, resolution=resolution) hex_arrays.append(hex_array) hex_arrays = np.array(hex_arrays) # Convert to numpy array for easier manipulation # Now replace each array with the same array, but if the color from the last frame is the same as the current frame, replace it with 0 tmp = hex_arrays.copy() print(tmp.shape) def close_enough(a, b): return a == b for i in range(len(tmp)): for j in range(len(tmp[i])): for k in range(len(tmp[i][j])): if i > 0 and close_enough(hex_arrays[i][j][k], hex_arrays[i-1][j][k]): tmp[i][j][k] = "." else: tmp[i][j][k] = hex_arrays[i][j][k] save(resolution, tmp, colors) print("Hex codes and colors saved successfully.")