5
votes

I have to render a very big image (>50.000² pixel) with cairo. To do this without running out of memory I render parts of the image (<1.000² pixel) one after another and merge them together later.

  1. Create 1000x1000 Surface
  2. Translate to position of the current part
  3. Draw image (calling the drawing instructions using pycairo)
  4. Render/Save image to file (cairo_surface_write_to_png)
  5. Repeat with next part

Because cairos clipping algorithms are faster than my own, step three draws the whole image, even if only a part of it is visible. Most of the CPU is used in Step 3 (by python). Most of the memory is used in Step 4 (by cairo).

Is there a way to speed things up? Something like this?

  1. Create 1000x1000 Surface
  2. Draw image
  3. Move everything to position of the current part
  4. Render/Save image to file
  5. Repeat 3 with next part

or

  1. Create 50000x50000 Surface
  2. Draw image
  3. Render/Save only the current part of the image to file
  4. Repeat 3 with next part
1

1 Answers

2
votes

First of all, using C or Vala instead of Python will probably speed things up.

As for memory usage, I would try writing to my own stream, rather than a file (see write_to_png_stream). This could allow you to (I didn't try this) control memory usage, assuming Cairo doesn't call your function only once after everything's done.