如何使用Pygame裁剪图像? [英] How can I crop an image with Pygame?

查看:299
本文介绍了如何使用Pygame裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 pygame 并想要一个具有三种状态的按钮的图形:正常,悬停和按下。我有一个像这样的图像...

I am learning pygame and want a graphic for a button with the three states: normal, hover, and pressed. I have an image like this one ...

...我想要一个新的 Surface 使用一部分它。

... and I want to get a new Surface using a portion of it.

我正在使用以下代码加载图片:

I'm loading the image with this code:

 buttonStates = pygame.image.load(os.path.join('image','button.png'))

如何只使用该图形的一部分制作新曲面?

How can I make a new surface using just a portion of that graphic?

推荐答案

cropped = pygame.Surface((80, 80))
cropped.blit(buttonStates, (0, 0), (30, 30, 80, 80))

表面上的blit方法粘贴另一个表面。 blit的第一个参数是源表面。第二个是要粘贴的位置(在这种情况下,左上角)。第三个(可选)参数是要粘贴的源图像的区域 - 在这种情况下,从顶部开始是80x80平方30px,从左边开始是30px。

The blit method on a surface 'pastes' another surface on to it. The first argument to blit is the source surface. The second is the location to paste to (in this case, the top left corner). The third (optional) argument is the area of the source image to paste from -- in this case an 80x80 square 30px from the top and 30px from the left.

这篇关于如何使用Pygame裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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