Cairo: Unterschied zwischen den Versionen

Aus Vosp.info
Wechseln zu:Navigation, Suche
Zeile 1: Zeile 1:
 +
* http://www.tortall.net/mu/wiki/CairoTutorial
 +
 +
= Beispiele =
 
== Cairo - erstelle ein pdf in python ==
 
== Cairo - erstelle ein pdf in python ==
 
<source lang="python">
 
<source lang="python">
Zeile 45: Zeile 48:
 
</source>
 
</source>
 
* Quellen
 
* Quellen
**  
+
** [http://cairosvg.org CairoSVG]
 
** [https://github.com/Kozea/CairoSVG github.com/Kozea/CairoSVG]
 
** [https://github.com/Kozea/CairoSVG github.com/Kozea/CairoSVG]

Version vom 26. November 2013, 02:16 Uhr

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()