Cairo
Aus Vosp.info
Version vom 26. November 2013, 16:23 Uhr von F (Diskussion | Beiträge)
Beispiele
Cairo - erstelle ein pdf in python
#!/usr/bin/env python
#import os
#import sys
#import optparse
#import io
#import base64
#import gzip
try:
import cairocffi as cairo
except ImportError:
import cairo # pycairo
pdf_output = 'test.pdf'
pdf_width = 596.590909091
pdf_height = 843.75
imagename = '/home/dev/CairoSVG/img_65.jpg'
#imagename = '/home/dev/CairoSVG/img_65_small.jpg'
image_x = 0
image_y = 0
print '- erstelle pdf', pdf_output, 'w:', pdf_width, 'h:', pdf_height
cairo_surface = cairo.PDFSurface(pdf_output, pdf_width, pdf_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)
image_width_original = image_surface.get_width()
image_height_original = image_surface.get_height()
image_scale_x = pdf_width / image_width_original
image_scale_y = pdf_height / image_height_original
print '- image original: ', image_width_original, image_height_original, image_scale_x, image_scale_y
context = cairo.Context(cairo_surface)
context.rectangle(image_x, image_y, image_width_original, image_height_original)
context.scale(image_scale_y,image_scale_y)
pattern_pattern = cairo.SurfacePattern(image_surface)
context.set_source(pattern_pattern)
context.fill()
cairo_surface.copy_page()
#cairo_surface.show_page()
- Quellen