CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyglet

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback

Overview
Eval results
Files

images-textures.mddocs/

Images and Textures

Image loading, texture management, and texture atlases.

Quick Reference

# 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)

Loading Functions

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() -> int

Image Classes

class 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)

Texture Atlas

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) -> TextureRegion

Animations

class 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

Image Patterns

# 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)

Examples

Load and Display

image = pyglet.image.load('sprite.png')

@window.event
def on_draw():
    window.clear()
    image.blit(100, 100)

Texture Coordinates

texture = image.get_texture()
# Access texture coordinates for sprite/shader use
tex_coords = texture.tex_coords  # (u0, v0, u1, v1, ...)

Save Screenshot

@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')

Texture Atlas Usage

# 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)]

Animated Sprite

# 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)

Image Transformations

# 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)

Performance Tips

  1. Use texture atlases: Reduces texture switches
  2. Power-of-2 textures: Better GPU performance (not required on modern GPUs)
  3. Mipmapping: Use for scaled textures
  4. Minimize blits: Use sprites with batching instead
  5. Preload images: Load during init, not per-frame

Common Issues

  1. Image upside down: Use get_transform(flip_y=True)
  2. Max texture size: Check with get_max_texture_size()
  3. Atlas full: Returns None, need new atlas or larger size
  4. Anchor point: Default (0,0) is bottom-left
  5. Pixel formats: 'RGB', 'RGBA', 'BGRA', 'BGR', 'ALPHA', 'LUMINANCE'

Install with Tessl CLI

npx tessl i tessl/pypi-pyglet

docs

3d-models.md

app-clock.md

audio-video.md

graphics-rendering.md

gui.md

images-textures.md

index.md

input-devices.md

math.md

opengl.md

resource-management.md

sprites-shapes.md

text-rendering.md

windowing.md

tile.json