Train image question

#21
by Edwardelric - opened

Hello,
I tried to train my own model with stardrew valley characters, but found that images are too tiny, could u tell how to preprocess your training data?

yeah sure! I lost most of my work on this project in an unfortunate accident while upgrading my storage setup. but here is a basic script that was pretty close to what I had from what I can remember. Also I didn't use stardew valley official files as these are not licensed for use in this way. I used various stardew valley mods which are free for reuse. that being said I cannot control how you choose to apply these technologies. so keep in mind the specific values for height and width will need to be adjusted to fit the input data. I also just whipped this up quickly and probably made mistakes. I haven't tested what I'm sending and I do not have the time right now to recollect my data to test it; so don't be surprised if you have to do some fiddling.

make sure you install Pillow

I'm pretty sure the pip is

pip install Pillow

import os
from PIL import Image


def process_image(input_path, output_path, image_number):
    # Load the input image
    image = Image.open(input_path)

    # Create a plain grey background of 512x512 pixels
    background = Image.new("RGBA", (512, 512), (128, 128, 128, 255))

    # Define the dimensions of the extracted area (32x64 pixels)
    width, height = 32, 64

    # Calculate the resized dimensions to fit the 512x512 background
    resized_width, resized_height = 512 // 4, 512 // 1

    # Iterate through 4 rows of the image ( if you want to do the first 4 views. I found with most of my data I could just do the first 3 and mirror one of the side views. play around and find what works for you. )
    for row in range(4):
        # Calculate the top left corner coordinates of the current row
        left, top = 0, row * height

        # Define the area to be cropped from the input image
        area = (left, top, left + width, top + height)

        # Crop and resize the extracted area. Use nearest neighbor. I think this is the right call to resize but if it doesn't work just check pillows docs and it should be in there.
        extracted = image.crop(area)
        resized = extracted.resize((resized_width, resized_height), Image.NEAREST)

        # Create a plain grey background of 512x512 pixels. It doesn't have to be this specific grey or grey at all but make sure whatever you choose has enough contrast from any of the colors in your data palette.
        background = Image.new("RGBA", (512, 512), (128, 128, 128, 255))

        # Paste the resized area onto the background
        background.paste(resized, (row * resized_width, 0))

        # Determine the output filename based on the row number. i cant remember the order of the views in the file but this is working from top to bottom so name them however you want so you can call specific views
        # durring inference (image generation).
        view = ""
        if row == 0:
            view = "front"
        elif row == 1:
            view = "back"
        elif row == 2:
            view = "left"
        elif row == 3:
            view = "right"

        output_filename = f"{view}{image_number}.png"
        output_path = os.path.join(output_path, output_filename)

        # Save the final image to the output path
        background.save(output_path, "PNG")
        print(f"Processed: {input_path} -> {output_path}")


def main():
    input_folder = "path/to/your/input_folder"
    output_folder = "path/to/your/output_folder"

    # this just ensures your output folder exists if not it will create one
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Initialize the image number counter variable will keep track of how many images you've iterated through and use this to add (x) where x is the iteration count. this allows you to have the views have the same name
    # in the training data without overwriting.
    image_number = 0

    # Iterate through the files in the input folder
    for filename in os.listdir(input_folder):
        if filename.endswith(".png"):
            input_path = os.path.join(input_folder, filename)
            process_image(input_path, output_folder, image_number)
            image_number += 1


if __name__ == "__main__":
    main()

Sign up or log in to comment