Cairo
Aus Vosp.info
Version vom 26. November 2013, 02:16 Uhr von F (Diskussion | Beiträge)
Beispiele
Cairo - erstelle ein pdf in python
#!/usr/bin/env python
try:
import cairocffi as cairo
except ImportError:
import cairo # pycairo
UNITS = {
"mm": 1 / 25.4,
"cm": 1 / 2.54,
"in": 1,
"pt": 1 / 72.,
"pc": 1 / 6.,
"px": None}
dpi = 300
output = 'test.pdf'
width = int(1000) * (1 / (dpi * UNITS["pt"]))
height = int(1000) * (1 / (dpi * UNITS["pt"]))
imagename = '/home/dev/CairoSVG/img_65.jpg'
print 'dpi: ', dpi, 'w:', width, 'h: ', height, 'output:', output
print '- erstelle pdf'
surface_class = cairo.PDFSurface
cairo_surface = surface_class(output, width, height)
print '- lese Bild ', imagename
input_ = open(imagename, 'rb') # filename
image_bytes = input_.read()
from io import BytesIO
png_file = BytesIO()
from PIL import Image
Image.open(BytesIO(image_bytes)).save(png_file, 'PNG')
png_file.seek(0)
image_surface = cairo.ImageSurface.create_from_png(png_file)
pattern_pattern = cairo.SurfacePattern(image_surface)
##.context.save()
print '- weitere Seite'
cairo_surface.copy_page()
cairo_surface.show_page()
- Quellen