Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python script for mass image renaming and clk conversion #61

Open
hallard opened this issue Jan 18, 2024 · 2 comments
Open

Python script for mass image renaming and clk conversion #61

hallard opened this issue Jan 18, 2024 · 2 comments

Comments

@hallard
Copy link

hallard commented Jan 18, 2024

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")
@MikeyC3040
Copy link

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

@hallard
Copy link
Author

hallard commented Aug 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants