Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:25 05 Sep 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : pyhton program that coverts a image dir

Author Message
tenij000
Regular Member

Joined: 30/05/2025
Location: Netherlands
Posts: 51
Posted: 05:16pm 31 Aug 2025
Copy link to clipboard 
Print this post

 
tenij000
Regular Member

Joined: 30/05/2025
Location: Netherlands
Posts: 51
Posted: 05:19pm 31 Aug 2025
Copy link to clipboard 
Print this post

converts hole dir whit image to new output dir whit image that fit picomite ili display

from PIL import Image
import os
import tkinter as tk
from tkinter import filedialog

def convert_image_to_256color_bmp(img, target_width=320, target_height=240):
   """Converteer naar 256 kleuren BMP op 320x240 resolutie."""
   img = img.convert("RGB").resize((target_width, target_height), Image.LANCZOS)
   quantized = img.quantize(colors=256, method=Image.Quantize.MEDIANCUT)
   return quantized

def batch_convert_images(input_folder, output_folder):
   """Converteer alle images in map naar 256 kleuren BMP's met nummering."""
   if not os.path.exists(output_folder):
       os.makedirs(output_folder)

   supported_ext = (".jpg", ".jpeg", ".png", ".bmp", ".gif")
   files = [f for f in os.listdir(input_folder) if f.lower().endswith(supported_ext)]
   files.sort()

   for i, filename in enumerate(files, start=1):
       try:
           path = os.path.join(input_folder, filename)
           img = Image.open(path)
           bmp_img = convert_image_to_256color_bmp(img)
           out_name = f"image_{i:03d}.bmp"
           out_path = os.path.join(output_folder, out_name)
           bmp_img.save(out_path, format="BMP")
           print(f"[{i}/{len(files)}] Converted {filename} -> {out_name}")
       except Exception as e:
           print(f"Error converting {filename}: {e}")

def select_folder_with_gui(title="Select folder"):
   root = tk.Tk()
   root.withdraw()
   folder = filedialog.askdirectory(title=title)
   root.destroy()
   return folder

if __name__ == "__main__":
   print("Select input folder with images...")
   input_folder = select_folder_with_gui("Select input folder with images")
   if input_folder:
       print("Select output folder...")
       output_folder = select_folder_with_gui("Select output folder for BMPs")
       if output_folder:
           batch_convert_images(input_folder, output_folder)
           print("Done!")
       else:
           print("No output folder selected.")
   else:
       print("No input folder selected.")
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025