Class ImageIO
pygame.image, named ImageIO so it does not clash with ImageSharp's Image in code that uses
both. The part Gymnasium's envs use: Load(string), for the PNG sprites and tiles of the
toy_text envs and Pendulum's torque arrow, and FromBytes(byte[], int, int, string), which builds the same kind of surface from
raw pixels.
public static class ImageIO
- Inheritance
-
ImageIO
- Inherited Members
Remarks
PNG is lossless, so the pixels are the file's own; the decoder is ImageSharp (decision D18 keeps it), and the parity suite proves its pixels equal pygame-ce 2.5.8's (SDL_image) for every asset the envs load. Reference: src_c/imageext.c (pygame-ce 2.5.8).
Methods
FromBytes(byte[], int, int, string)
pygame.image.frombytes(bytes, (width, height), format) for "RGBA" and "RGB": a new surface
from raw pixel bytes, row by row from the top, with per-pixel alpha for "RGBA" (the format a loaded PNG
has) and without it for "RGB". The bytes are copied.
public static Surface FromBytes(byte[] bytes, int width, int height, string format)
Parameters
bytesbyte[]The pixels, 4 (RGBA) or 3 (RGB) bytes each.
widthintThe width, positive.
heightintThe height, positive.
formatstring"RGBA"or"RGB".
Returns
- Surface
The surface; the caller owns it.
Exceptions
- ArgumentNullException
Thrown when
bytesorformatis null.- ValueError
Thrown with pygame's messages for a size that is not positive (
Resolution must be nonzero positive values), a byte count that does not match (Bytes length does not equal format and resolution size), or a format pygame does not know (Unrecognized type of format).- NotSupportedException
Thrown for a format pygame knows but this method does not model (
"ARGB","P", ...).
Load(Stream, string)
pygame.image.load(file_object) for an 8-bit PNG read from a stream: what Gym.NET's envs use for the
assets they embed (DR-19), with the same pixels and the same refusals as Load(string).
public static Surface Load(Stream stream, string name = "stream")
Parameters
streamStreamThe PNG bytes, read from the current position; the stream stays open and the caller's.
namestringWhat to call the image in error messages (the asset's name).
Returns
- Surface
The surface; the caller owns it.
Exceptions
- ArgumentNullException
Thrown when
streamis null.- NotSupportedException
Thrown for an image that is not an 8-bit RGB, RGBA or grey PNG.
- InvalidImageContentException
Thrown for corrupt bytes.
- UnknownImageFormatException
Thrown for bytes that are no image format ImageSharp knows.
Load(string)
pygame.image.load(path) for an 8-bit PNG: a new surface with the image's pixels, with per-pixel alpha when
the PNG has an alpha channel (as every Gymnasium asset does), without it otherwise. A grey PNG loads as grey RGB.
public static Surface Load(string path)
Parameters
pathstringThe file.
Returns
- Surface
The surface; the caller owns it.
Exceptions
- ArgumentException
Thrown when
pathis null or empty.- FileNotFoundException
Thrown when the file does not exist (pygame raises
FileNotFoundError).- NotSupportedException
Thrown for a file that is not a PNG, a palette PNG, or a bit depth other than 8: pygame loads those too, but through conversions (palette surfaces, 16-bit stripping) no env needs, so they are refused rather than guessed.
- InvalidImageContentException
Thrown for a corrupt file.