jagttt
jagttt

Reputation: 1070

How to convert a Pyglet image to a PIL image?

i want to convert a Pyglet.AbstractImage object to an 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 an image from pyglet to PIL properly?

Upvotes: 2

Views: 2121

Answers (2)

jagttt
jagttt

Reputation: 1070

I think I find the solution

the pitch in Pyglet.AbstractImage instance is not compatible with PIL I found in pyglet 1.1 there is a codec function to encode the Pyglet image to PIL here is the link to the source

so the code above should be modified 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()

I'm using a 461x288 image in this case and find that pic.pitch is -1384

but the new pitch is -1383

Upvotes: 2

TML
TML

Reputation: 12966

This is an open wishlist item:

AbstractImage to/from PIL image.

Upvotes: 0

Related Questions