Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

2D Game Development: From Zero To Hero

A collection of the community's knowledge on game development. · By Penaz

Will this book help me with errors I've been dealing with for scripts like this?

A topic by LonesomePiran7a created 5 days ago Views: 24 Replies: 5
Viewing posts 1 to 3

from PIL import Image


def crop_texture(image_path, texture_rect, texture_rect_offset, output_path):

    """

    Crops a portion of the image based on textureRect and textureRectOffset.


    :param image_path: Path to the input PNG file.

    :param texture_rect: A tuple (x, y, width, height) representing the area to crop.

    :param texture_rect_offset: A tuple (offset_x, offset_y) to apply an offset to the texture.

    :param output_path: Path where the cropped image will be saved.

    """

     # Open the image

    img = Image.open(image_path)


    # Extract textureRect and textureRectOffset values

    x, y, width, height = texture_rect

    offset_x, offset_y = texture_rect_offset


    # Adjust the coordinates by applying the offset

    x += offset_x

    y += offset_y


    # Flip the y-coordinate relative to the image height

    y = img.height - y - height # Flip the y-axis for bottom-left origin


    # Define the box to crop (left, upper, right, lower)

    crop_box = (x, y, x + width, y + height)


    # Crop the image using the adjusted coordinates

    cropped_img = img.crop(crop_box)


    # Save the cropped image to the output path

    cropped_img.save(output_path)

    print(f"Cropped image saved to {output_path}")


Example usage

image_path = "Human_Template.png" # Path to your PNG file

texture_rect = (818.0129, 125.0781, 22.911, 44.8) # Example texture rectangle (x, y, width, height)

texture_rect_offset = (0, 0) # Example offset (offset_x, offset_y)

output_path = "output_image.png" # Path to save the cropped image

This is for python.

Developer

Hello, the book does have some basic concepts about graphics (like how usually game engines have texture origins on the top left corner), but I'm not sure it will be able to give you a surefire solution to your problem.


I noticed that your example is missing the actual call to "crop_texture" at the end:

crop_texture(image_path, texture_rect, texture_rect_offset, output_path)

Did you forget to paste it, by any chance?


If any errors arise from that, I can try and give you some pointers.

(2 edits)

Yes. Thank you! I just started programming so I didnt even know there were pieces of the script missing. I have coordinates I found on Unity Explorer in Drova Forsaken Kin game but I was told it has to be flipped bottom left to locate specific character models in a png. I'm having issue from the very beginning of this script. FileNotFoundError is shown. Do you have discord by any chance? LONE5mpiraña is mine. Or it is lonesomepiran7a.

Developer

FilenotfoudError just means that Python cannot find a file you're trying to open (probably the image file) from where you're executing the script (the script and image file should be in the same folder to work as written).


Considering you're just starting your programming journey I can suggest these books from the great Al Sweigart:

https://inventwithpython.com/invent4thed/

- https://inventwithpython.com/pygame/

They're great material. (The entire collection is a goldmine, free to read online)

For the discord, i humbly apologize but I prefer not give it away. Although I'm more than willing to help to any extent possible either here or on GitHub's discussions.

No worries. Thank you for pointing me in the right directions. #blessed