- Notifications
You must be signed in to change notification settings - Fork 1
ImageObject
Anton Hvornum edited this page Oct 26, 2019 · 4 revisions
ImageObject is a soft wrap around pyglet.image.load but acts as a a wrapper for helper functions related to image data.
The first parameter passed to ImageObject can be of varying types,
the common thing is that they all get converted into a pyglet.texture/resource.
All objects are converted as follows: <object> -> pyglet.image.resource
Below follow a few types that ImageObject supports:
-
str- will be parsed as a file location -
bytes- will be parsed asPNGcolor data stream and loaded as-is -
ImageObject- if the first item toImageObjectis of the same type as itself, it will migrate the passed data into it's own object structure. Aka, soft inherited. -
None- If the first parameter toImageObjectis 'None, and bothwidthandheightare set, thenImageObjectwill attempt to generate and load a blankPNGof sizeWidth x Height(with optionalcolor` parameter). -
None- If the first parameter toImageObjectisNone, but this timewidth/heightis not supplied (or_no_backdrop=Trueis set). This means we do not want a background to be generated or rendered for this object. In this case, a dummytextureobject will be used as a placeholder inself._textureto satisfypyglet's internals when used in junction with pyglet.sprite.Sprite for instance.
One example would be:
with urllib.request.urlopen(url) as response: data = response.read() image = ImageObject(io.BytesIO(data), path=url) sprite = genericSprite( image, *args, **kwargs )This would transform the bytes-stream from a web-request, into a image object that can be used and passed as a texture to genericSprite() which is a soft extension of pyglet.sprite.Sprite.