Skip to content
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 as PNG color data stream and loaded as-is
  • ImageObject - if the first item to ImageObject is 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 to ImageObject is 'None, and both widthandheightare set, thenImageObjectwill attempt to generate and load a blankPNGof sizeWidth x Height(with optionalcolor` parameter).
  • None - If the first parameter to ImageObject is None, but this time width/height is not supplied (or _no_backdrop=True is set). This means we do not want a background to be generated or rendered for this object. In this case, a dummy texture object will be used as a placeholder in self._texture to satisfy pyglet'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.

Clone this wiki locally