如何读取位图文件并在之后进行裁剪? [英] How to read a bitmap file and crop it afterwards?

查看:48
本文介绍了如何读取位图文件并在之后进行裁剪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个表示精灵的位图,并裁剪一部分.
这是图像的示例.

I want to read a bitmap that represents a sprite, and crop a part of it.
Here is an example of an image.

在Matlab中,我会做类似的事情:

In Matlab I would have done somethings like:

i = imread('myfile.png')
x1 = i(1:64,1:64,:)

如何在python中做同样的事情?

How can I do the same in python?

推荐答案

PIL 可以做到这一点,特别是 crop 函数.

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')

我不确定,好像您的图片使用的是调色板吗?我在RGBA调用中添加了 convert ,因此输出文件具有透明度.没有 convert 的文件在透明像素上具有绿色(为什么是绿色?).

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.

这篇关于如何读取位图文件并在之后进行裁剪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆