Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback
Image loading, texture management, and texture atlases.
# Load image
image = pyglet.image.load('file.png')
anim = pyglet.image.load_animation('file.gif')
# Get texture
texture = image.get_texture()
# Transform
flipped = image.get_transform(flip_x=False, flip_y=True, rotate=90)
# Draw
image.blit(x=100, y=100)
# Create from data
image = pyglet.image.ImageData(width, height, 'RGBA', pixel_data, pitch=None)
# Texture atlas
atlas = pyglet.image.atlas.TextureAtlas(1024, 1024)
region = atlas.add(image)def pyglet.image.load(filename: str, file=None, decoder=None) -> AbstractImage
def pyglet.image.load_animation(filename: str, file=None, decoder=None) -> Animation
def pyglet.image.create(width: int, height: int, pattern=None) -> ImageData
def pyglet.image.get_max_texture_size() -> int
def pyglet.image.get_max_array_texture_layers() -> intclass pyglet.image.AbstractImage:
width, height: int
anchor_x, anchor_y: int
def get_image_data() -> ImageData
def get_texture() -> Texture
def get_mipmapped_texture() -> Texture
def get_region(x, y, width, height) -> AbstractImage
def save(filename, file=None, encoder=None)
def get_transform(flip_x=False, flip_y=False, rotate=0) -> AbstractImage
def blit(x, y, z=0)
class pyglet.image.ImageData(AbstractImage):
"""Raw pixel data"""
__init__(width, height, fmt, data, pitch=None)
format: str # 'RGB', 'RGBA', etc.
pitch: int # Bytes per row
def get_data(fmt=None, pitch=None) -> bytes
def set_data(fmt, pitch, data)
def set_mipmap_data(level, data)
class pyglet.image.Texture(AbstractImage):
"""OpenGL texture"""
id: int # GL texture ID
target: int # GL_TEXTURE_2D, etc.
def blit(x, y, z=0, width=None, height=None)class pyglet.image.atlas.TextureAtlas:
"""Pack multiple images into single texture"""
__init__(width=2048, height=2048)
def add(img: AbstractImage) -> TextureRegion
# Returns None if doesn't fit
# Atlas bin (manages multiple atlases)
class pyglet.image.atlas.TextureBin:
"""Manages multiple texture atlases"""
__init__(texture_width=2048, texture_height=2048)
def add(img: AbstractImage) -> TextureRegionclass pyglet.image.Animation:
frames: list # AnimationFrame objects
@classmethod
def from_image_sequence(images, duration, loop=True) -> Animation
def get_duration() -> float
def get_transform(flip_x=False, flip_y=False, rotate=0) -> Animation
class pyglet.image.AnimationFrame:
image: AbstractImage
duration: float # Seconds# Solid color
pattern = pyglet.image.SolidColorImagePattern(color=(255, 255, 255, 255))
image = pyglet.image.create(100, 100, pattern)
# Checkerboard
pattern = pyglet.image.CheckerImagePattern(colors=((255,255,255,255), (0,0,0,255)))
image = pyglet.image.create(100, 100, pattern)image = pyglet.image.load('sprite.png')
@window.event
def on_draw():
window.clear()
image.blit(100, 100)texture = image.get_texture()
# Access texture coordinates for sprite/shader use
tex_coords = texture.tex_coords # (u0, v0, u1, v1, ...)@window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.F12:
pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')# Create atlas
atlas = pyglet.image.atlas.TextureAtlas(1024, 1024)
# Add images
images = [pyglet.image.load(f'sprite{i}.png') for i in range(10)]
regions = [atlas.add(img) for img in images]
# Use with sprites
sprites = [pyglet.sprite.Sprite(region, x=i*50, y=100) for i, region in enumerate(regions)]# From GIF
anim = pyglet.image.load_animation('explosion.gif')
sprite = pyglet.sprite.Sprite(anim, x=100, y=100)
# From image sequence
frames = [pyglet.image.load(f'frame{i:02d}.png') for i in range(10)]
anim = pyglet.image.Animation.from_image_sequence(frames, duration=0.1, loop=True)
sprite = pyglet.sprite.Sprite(anim)# Flip
flipped = image.get_transform(flip_y=True)
# Rotate (0, 90, 180, 270 only)
rotated = image.get_transform(rotate=90)
# Combine
transformed = image.get_transform(flip_x=True, rotate=180)get_transform(flip_y=True)get_max_texture_size()Install with Tessl CLI
npx tessl i tessl/pypi-pyglet@2.1.1