如何将pygame曲面转换为PIL图像? [英] How to convert a pygame Surface to a PIL Image?

查看:115
本文介绍了如何将pygame曲面转换为PIL图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PIL透视变换屏幕的一部分. 原始图像数据是pygame曲面,需要将其转换为PIL图像.

I'm using PIL to transform a portion of the screen perspectively. The original image-data is a pygame Surface which needs to be converted to a PIL Image.

因此,我找到了为此目的而存在的pygame的tostring函数.

Therefore I found the tostring-function of pygame which exists for that purpose.

但是结果看起来很奇怪(请参阅所附的屏幕截图).这段代码出了什么问题?

However the result looks pretty odd (see attached screenshot). What is going wrong with this code:

rImage = pygame.Surface((1024,768))

#draw something to the Surface
sprite = pygame.sprite.RenderPlain((playboard,))
sprite.draw(rImage)

pil_string_image = pygame.image.tostring(rImage, "RGBA",False)
pil_image = Image.fromstring("RGBA",(660,660),pil_string_image)

我在做什么错了?

推荐答案

正如我在评论中指出的,

As I noted in a comment, pygame documentation for pygame.image.fromstring(string, size, format, flipped=False) says "The size and format image must compute the exact same size as the passed string buffer. Otherwise an exception will be raised". Thus, using (1024,768) in place of (660,660), or vice versa – in general, the same dimensions for the two calls – is more likely to work. (I say "more likely to work" instead of "will work" because of I didn't test any cases.)

怀疑这样的问题的原因:图像部分的奇怪外观类似于显示屏,该显示屏设置为无法同步的光栅速率;也就是说,图像的线条开始显示在左边缘以外的其他位置;在这种情况下,因为图像线的长度比显示线的长度长.我假设雪花是精灵,与扭曲的图像分开生成.

The reason for suspecting a problem like this: The strange look of part of the image resembles a display screen which is set to a raster rate it can't synchronize; ie, lines of the image start displaying at points other than at the left margin; in this case because of image line lengths being longer than display line lengths. I'm assuming the snowflakes are sprites, generated separately from the distorted image.

这篇关于如何将pygame曲面转换为PIL图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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