开发者

Printing Graphics in Python

I need to print "Wheel Tags" from python. Wheel tags will have images, lines, and text.

The Python tutorial has two paragraphs about creating postscript files with the image lib. After readi开发者_如何学编程ng it I still do not know how to lay out the data. I was hoping some one might have samples of how to layout the images, text and lines?

Thanks for any help.


See http://effbot.org/imagingbook/psdraw.htm

Note that:

  1. the PSDraw module does not appear to have been actively maintained since 2005; I would guess that most of the effort has been redirected into supporting the PDF format instead. You might be happier using pypdf instead;

  2. it has comments like '# FIXME: incomplete' and 'NOT YET IMPLEMENTED' in the source

  3. it does not appear to have any method of setting the page size - which as I recall means it defaults to A4 (8.26 x 11.69 inches)

  4. all measurements are in points, at 72 points per inch.

You will need to do something like:

import Image
import PSDraw

# fns for measurement conversion    
PTS = lambda x:  1.00 * x    # points
INS = lambda x: 72.00 * x    # inches-to-points
CMS = lambda x: 28.35 * x    # centimeters-to-points

outputFile = 'myfilename.ps'
outputFileTitle = 'Wheel Tag 36147'

myf = open(outputFile,'w')
ps = PSDraw.PSDraw(myf)
ps.begin_document(outputFileTitle)

ps is now a PSDraw object which will write PostScript to the specified file, and the document header has been written - you are ready to start drawing stuff.

To add an image:

im = Image.open("myimage.jpg")
box = (        # bounding-box for positioning on page
    INS(1),    # left
    INS(1),    # top
    INS(3),    # right
    INS(3)     # bottom
)
dpi = 300      # desired on-page resolution
ps.image(box, im, dpi)

To add text:

ps.setfont("Helvetica", PTS(12))  # PostScript fonts only -
                                  # must be one which your printer has available
loc = (        # where to put the text?
    INS(1),    # horizontal value - I do not know whether it is left- or middle-aligned
    INS(3.25)  # vertical value   - I do not know whether it is top- or bottom-aligned
)
ps.text(loc, "Here is some text")

To add a line:

lineFrom = ( INS(4), INS(1) )
lineTo   = ( INS(4), INS(9) )
ps.line( lineFrom, lineTo )

... and I don't see any options for changing stroke weight.

When you are finished, you have to close the file off like:

ps.end_document()
myf.close()

Edit: I was doing a bit of reading on setting stroke weights, and I ran across a different module, psfile: http://seehuhn.de/pages/psfile#sec:2.0.0 The module itself looks pretty minimal - he's writing a lot of raw postscript - but it should give you a better idea of what's going on behind the scenes.


I would recommend the open source library Reportlab for this sort of task.

It is very simple to use and outputs directly to PDF format.

A very simple example from the official documentation:

from reportlab.pdfgen import canvas
def hello(c):
    c.drawString(100,100,"Hello World")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

As long as PIL is installed, it is also very easy to add images to your page:

canvas.drawImage(self, image, x,y, width=None,height=None,mask=None)

where "image" is either a PIL Image object, or the filename of the image you wish to use.

Plenty of examples in the documentation also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜