简体   繁体   English

在Pygame中使用Pillow / PIL

[英]Using Pillow/PIL with Pygame

I'm currently trying to use a .png of a Tic Tac Toe board in Pygame so that the white part is transparent. 我目前正在尝试在Pygame中使用Tic Tac Toe板的.png文件,以使白色部分透明。 However, I'm getting "libpng warning: sBIT: invalid" and the white is showing when I blit the board onto the background. 但是,我收到“ libpng警告:sBIT:无效”,并且当我将电路板放到背景上时显示白色。 The code is as follows: 代码如下:

background = pygame.image.load("blah.bmp")
board_surface = pygame.image.load("board.png")
board_surface.convert_alpha()

I read on the Pygame docs that only .bmp is guaranteed to work, and since image.get_extended() is returning a 1, I'm assuming that I simply don't have enough image processing support. 我在Pygame文档上读到,保证只有.bmp可以工作,并且由于image.get_extended()返回1,所以我假设我只是没有足够的图像处理支持。

I've installed Pillows and imported the module, but I have no idea how to use this with Pygame. 我已经安装了Pillows并导入了模块,但是我不知道如何在Pygame中使用它。 The tutorial shows how to load images as Pillow's Image class and so forth, but I need to represent images as Pygame's Surfaces. 本教程显示了如何将图像作为Pillow的Image类加载,等等,但是我需要将图像表示为Pygame的Surfaces。

I would appreciate any help with integrating these two things, or any other solutions on how to get my .png to work! 对于将这两件事集成在一起的任何帮助,或者任何其他有关如何使我的.png工作的解决方案,我将不胜感激! For reference, I'm using Python3.3 and Pygame1.9.2 供参考,我使用的是Python3.3和Pygame1.9.2

I have encountered this problem too. 我也遇到过这个问题。 I solved it by switching to .tif (only with one f !). 我通过切换到.tif(仅使用一个f!)解决了它。 If you have Gimp installed use that to turn the white spaces into alpha=0 spaces (if you have problems doing that I can help with a more detailed explanation). 如果您安装了Gimp,请使用它将空白转换为alpha = 0的空格(如果这样做有问题,我可以为您提供更详细的解释)。 After that import the image just as normal and use the .convert_alpha() method. 之后,照常导入图像并使用.convert_alpha()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM