SmittyHalibut/EleksTubeHAX

Python script for mass image renaming and clk conversion

Opened this issue · 2 comments

During lunch time I asked chatgpt to convert pascal code to generate .clk file (because I'm on mac and file converter need windows platform) and here the result; I'll take some time later to see if it's working, but looks good.

from PIL import Image
import os

def convert_image(input_file_path, output_file_name):
    # Load the image
    img = Image.open(input_file_path)
    W, H = img.size

    # Construct the output file path
    output_file_path = os.path.join(os.path.dirname(input_file_path), output_file_name + '.clk')

    # Prepare the header
    header = bytearray([ord('C'), ord('K'), W & 0xFF, (W >> 8) & 0xFF, H & 0xFF, (H >> 8) & 0xFF])

    # Create a memory stream (byte array) and write the header
    memory_stream = bytearray()
    memory_stream.extend(header)

    # Process and write pixel data
    for y in range(H):
        for x in range(W):
            R, G, B = img.getpixel((x, y))
            OutPixel = ((R & 0xF8) << 8) | ((G & 0xFC) << 3) | (B >> 3)
            memory_stream.extend(OutPixel.to_bytes(2, byteorder='little'))

    # Save to file
    with open(output_file_path, 'wb') as file:
        file.write(memory_stream)

# Example, loop through the image files named RETRO0.jpg to RETRO9.jpg
# convert them to bmp renaming them from 40 to 49 and finally convert the
# bmp to clk file
for i in range(10):
    # Open each jpg image file
    img = Image.open(f"RETRO{i}.jpg")

    # Crop the image to the specified coordinates (left, upper, right, lower)
    #cropped_img = img.crop((12, 22, 120, 220))

    # Save the cropped image as a bmp file named 40.bmp to 49.bmp
    #cropped_img.save(f"4{i}.bmp")
    img.save(f"4{i}.bmp")

    # Convert the bmp to clk
    convert_image(f"4{i}.bmp", f"4{i}.clk")

I took your idea a step further, and since you are using PIL created a PIL plugin for CLK files and an example prepare script (with option to view CLK files)
https://github.com/MikeyC3040/EleksTubeHAX/blob/main/Prepare_images/CLKImagePlugin.py
https://github.com/MikeyC3040/EleksTubeHAX/blob/main/Prepare_images/CLKprepare.py

Nice, what will your PR2 contains, new awesome features?