tenij000 Regular Member
 Joined: 30/05/2025 Location: NetherlandsPosts: 46 |
Posted: 11:18am 22 Aug 2025 |
Copy link to clipboard |
 Print this post |
|
python program turns image into a picomite vga picture 64x64
from PIL import Image import os import tkinter as tk from tkinter import filedialog
def image_to_16_color_pixel_commands(image_path, output_filename="pixel_commands.txt", target_width=64, target_height=64): """ Reads a JPG image, resizes it to 64x64, quantizes it to a 16-color palette, and generates a text file with PIXEL x,y,RGB(r,g,b) commands for each pixel.
Args: image_path (str): The path to the input JPG image. output_filename (str): The name of the output text file. target_width (int): The desired width for the processed image. target_height (int): The desired height for the processed image. """ try: # Open the image img = Image.open(image_path) print(f"Successfully opened image: {image_path}") print(f"Original image size: {img.width}x{img.height}")
# Resize the image to the target dimensions # Use Image.LANCZOS for high-quality downsampling img = img.resize((target_width, target_height), Image.LANCZOS) print(f"Resized image to: {img.width}x{img.height}")
# Convert the image to a 16-color adaptive palette # 'P' mode uses a palette. Using Image.Quantize.MEDIANCUT as it's a more # generally available method when LIBIMAGEQUANT might not be compiled in. quantized_img = img.quantize(colors=16, method=Image.Quantize.MEDIANCUT) print(f"Quantized image to 16 colors using MEDIANCUT method.")
# Ensure the image is in RGB mode for consistent color output # (quantize can sometimes leave it in 'P' mode, getrgb() handles this, # but converting explicitly ensures we get R,G,B values for output) quantized_img_rgb = quantized_img.convert('RGB') print(f"Converted quantized image to RGB mode for pixel value extraction.")
# Open the output file for writing with open(output_filename, 'w') as f: f.write(f"' --- Pixel data for {os.path.basename(image_path)} ({target_width}x{target_height}, 16 colors) ---\n") # Iterate through each pixel and write its command for y in range(quantized_img_rgb.height): for x in range(quantized_img_rgb.width): r, g, b = quantized_img_rgb.getpixel((x, y)) f.write(f"PIXEL {x},{y},RGB({r},{g},{b})\n") print(f"Successfully generated pixel commands to {output_filename}")
except FileNotFoundError: print(f"Error: Image file not found at {image_path}") except Exception as e: print(f"An error occurred: {e}")
def select_image_with_gui(): """ Opens a file dialog to allow the user to select an image file. Returns the selected file path or None if no file is selected. """ root = tk.Tk() root.withdraw() # Hide the main window file_path = filedialog.askopenfilename( title="Select an Image File", filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.gif"), ("All files", "*.*")] ) root.destroy() # Destroy the hidden window after selection return file_path
# --- Example Usage --- if __name__ == "__main__": print("Please select an image file using the pop-up window.") selected_image_path = select_image_with_gui()
if selected_image_path: print(f"Selected image: {selected_image_path}") image_to_16_color_pixel_commands(selected_image_path) else: print("No image file selected. Exiting.") |