Skip to content Skip to sidebar Skip to footer

How To Read A Bitmap File And Crop It Afterwards?

I want to read a bitmap that represents a sprite, and crop a part of it. Here is an example of an image. In Matlab I would have done somethings like: i = imread('myfile.png') x1 =

Solution 1:

PIL could do that, crop function in particular.

from PIL import Image

img = Image.open(r'yRc2a.png')
img = img.convert('RGBA')
img = img.crop((0, 0, 82, 82))
img.save(r'out.png')

I'm not sure, seems like your image uses palette? I added convert to RGBA call so output file has transparency. Without convert file has green (why green?) color on transparent pixels.

Post a Comment for "How To Read A Bitmap File And Crop It Afterwards?"