Lecture_8_Pycairo_Images_Surfaces---.pptx

kamalsmail1 0 views 9 slides Oct 08, 2025
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Lecture 8


Slide Content

Lecture 8: Images and Surfaces in Pycairo Understanding surfaces, image handling, and exporting results.

What are Surfaces? - A Surface is the target for all drawing operations - Types: • ImageSurface (PNG raster images) • PDFSurface (PDF export) • SVGSurface (vector graphics) • Win32/XlibSurface (platform-specific)

Creating an Image Surface import cairo surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 300) context = cairo.Context(surface) context.set_source_rgb(1, 1, 1) context.paint() surface.write_to_png('output.png')

Drawing Shapes on Image Surfaces context.set_source_rgb(1, 0, 0) context.rectangle(50, 50, 200, 100) context.fill() surface.write_to_png('shapes.png')

Loading and Drawing External Images img_surface = cairo.ImageSurface.create_from_png('logo.png') context.set_source_surface(img_surface, 100, 100) context.paint() surface.write_to_png('combined.png')

Scaling Images context.save() context.translate(50, 50) context.scale(0.5, 0.5) context.set_source_surface(img_surface, 0, 0) context.paint() context.restore()

Using Multiple Surfaces temp_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200) temp_ctx = cairo.Context(temp_surface) temp_ctx.set_source_rgb(0, 0, 1) temp_ctx.arc(100, 100, 80, 0, 6.28) temp_ctx.fill() context.set_source_surface(temp_surface, 150, 50) context.paint() surface.write_to_png('multi_surface.png')

Saving to Different Formats pdf_surface = cairo.PDFSurface('output.pdf', 400, 300) pdf_ctx = cairo.Context(pdf_surface) pdf_ctx.set_source_rgb(0, 0, 0) pdf_ctx.move_to(50, 50) pdf_ctx.show_text('Hello PDF!') pdf_surface.finish()

Summary - Surfaces are the target for all drawing - ImageSurface is most common for PNG output - You can load, scale, and combine images - Export drawings to PNG, PDF, or SVG
Tags