Using PyX, how can I set the size of the canvas?

388 Views Asked by At

Beginner's question using PyX in Python 3.8.1.

I'm creating an EPS file, and would like the canvas size to be 70 mm x 70 mm. How can I do this?

from pyx import *
# build and size canvas
c = canvas.canvas()
c.fill(path.circle(2.5, 1.5, 0.5))
c.writeEPSfile("my-output")
print("Done!")

The .eps file that I get is the size of the element that I've added, but I would like it to be a 70 mm square. Any help appreciated!

2

There are 2 best solutions below

0
wobsta On

The bounding box is a property of the page (see document.page). In the writeEPSfile method page parameters can be set by the page_ prefix (see writeEPSfile documentation), hence the following code creates the requested output:

from pyx import *
from pyx import bbox
# build and size canvas
c = canvas.canvas()
c.fill(path.circle(2.5, 1.5, 0.5))
c.writeEPSfile("my-output", page_bbox=bbox.bbox(0, 0, 7, 7))
print("Done!")

Note that the bbox module is not part of the default module names imported by *, thus a second import line is required.

0
To Ch On

Thank you, this worked well! My image is in different orientations, so I ended up with:

if blnFlip == True:
        MasterCanvas.items[i].writeEPSfile("my_output", page_bbox=bbox.bbox(llx_pt=-CanvasSizeCm, lly_pt=-CanvasSizeCm, urx_pt=0, ury_pt=0))
else:
        MasterCanvas.items[i].writeEPSfile("my_output", page_bbox=bbox.bbox(llx_pt=0, lly_pt=-CanvasSizeCm, urx_pt=CanvasSizeCm, ury_pt=0))