How to convert Pyglet image to PIL image?
I want to convert Pyglet.AbstractImage object to PIL image for further manipulation here are my codes
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
data = pic.get_data('RGB', pic.pitch)
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
but the image shown went wrong. so how to convert image from piglet to PIL correctly?
+1
a source to share
2 answers
I think I found a solution
step in Pyglet.AbstractImage instance is incompatible with PIL I found in pyglet 1.1, there is a codec function to encode Pyglet image for PIL here link to source
so the above code should be changed to this
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
pitch = -(pic.width * len('RGB'))
data = pic.get_data('RGB', pitch) # using the new pitch
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
In this case, I use a 461x288 image and find that pic.pitch is -1384
but a new step is -1383
+2
a source to share