Python PIL Image.tostring() [英] Python PIL Image.tostring()

查看:264
本文介绍了Python PIL Image.tostring()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和PIL的新手.我正在尝试遵循代码示例,了解如何通过PIL将图像加载到Python,然后使用openGL绘制其像素.这是一些代码行:

I'm new to Python and PIL. I am trying to follow code samples on how to load an image into to Python through PIL and then draw its pixels using openGL. Here are some line of the code:

from Image import *
im = open("gloves200.bmp") 
pBits = im.convert('RGBA').tostring()

.....

glDrawPixels(200, 200, GL_RGBA, GL_UNSIGNED_BYTE, pBits)

这将在画布上绘制200 x 200像素像素.但是,它不是预期的图像,看起来像是从随机存储器中提取像素.即使当我尝试绘制完全不同的图像时,我也会得到相同的图案,这支持了随机记忆假说.有人可以帮助我吗?我在Windows XP上使用的是Python 2.7和pyopenGL和PIL的2.7版本.

This will draw a 200 x 200 patch of pixels on the canvas. However, it is not the intended image-- it looks like it is drawing pixels from random memory. The random memory hypothesis is supported by the fact that I get the same pattern even when I attempt to draw entirely different images.Can someone help me? I'm using Python 2.7 and the 2.7 version of pyopenGL and PIL on Windows XP.

推荐答案

我认为您很亲密.试试:

I think you were close. Try:

pBits = im.convert("RGBA").tostring("raw", "RGBA")

首先必须将图像转换为RGBA模式,以使RGBA原始模式打包程序可用(请参阅

The image first has to be converted to RGBA mode in order for the RGBA rawmode packer to be available (see Pack.c in libimaging). You can check that len(pBits) == im.size[0]*im.size[1]*4, which is 200x200x4 = 160,000 bytes for your gloves200 image.

这篇关于Python PIL Image.tostring()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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