How to convert a pygame Surface to a PIL Image?

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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.

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) 

What am I doing wrong?

回答1:

As I noted in a comment, pygame documentation for pygame.image.fromstring(string, size, format, flipped=False)

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!