sdl2.sdlimage - Python bindings for SDL_image

py-sdl2 provides bindings for SDL2_image, a library designed for use with SDL2 that adds support for loading a wide range of different common (and uncommon) image formats for easy use with SDL2. In addition, SDL2_image includes functions for saving SDL_Surface objects to PNG and/or JPEG images.


If using an alternative rendering system that doesn’t use SDL surfaces as input (e.g. PyOpenGL), the Pillow imaging library may be a better fit for your project.

Initialization and library information functions


Returns the most recently encountered SDL2 error message, if any.

This function is a simple wrapper around SDL_GetError().

Retuns:A UTF-8 encoded string describing the most recent SDL2 error.
Return type:bytes

Sets the most recent SDL2 error message to a given string.

This function is a simple wrapper around SDL_SetError().

Parameters:fmt (bytes) – A UTF-8 encoded string containing the error message to set.
Retuns:Always returns -1.
Return type:int

Image format-checking functions

These functions are used to check whether an SDL file object (SDL_RWops) is a valid image file of a given format. Note that all of these functions will return 0 if SDL2_image was not built with support for that format, even if it is a valid image of that type, so be cautious when using these for formats like WEBP, JPEG, PNG, and TIFF, which are optional when building SDL2_image.

Module constants


Latest SDL2_image library major number supported by PySDL2.


Latest SDL2_image library minor number supported by PySDL2.


Latest SDL2_image library patch level number supported by PySDL2.


IMG_Init() flag to enable support for the JPEG image format.


IMG_Init() flag to enable support for the PNG image format.


IMG_Init() flag to enable support for the TIFF image format.


IMG_Init() flag to enable support for the WEBP image format.