Cairo: Unterschied zwischen den Versionen

Aus Vosp.info
Wechseln zu:Navigation, Suche
(Cairo - erstelle ein pdf in python)
 
Zeile 3: Zeile 3:
 
= Beispiele =
 
= Beispiele =
 
== Cairo - erstelle ein pdf in python ==
 
== Cairo - erstelle ein pdf in python ==
 +
 +
folgender Code
 +
* erstellt eine pdf Datei ''pdf_output'',
 +
* liest ein Bild ''imagename'' ein
 +
* positioniert dieses auf der ersten Seite
 +
* nimmt das eingelesene Bild und positioniert es als Referenz auf ''pdf_pages'' Seiten
 +
 
<source lang="python">
 
<source lang="python">
 
#!/usr/bin/env python
 
#!/usr/bin/env python
#import os
 
#import sys
 
#import optparse
 
#import io
 
#import base64
 
#import gzip
 
 
  
 
try:
 
try:
Zeile 19: Zeile 19:
  
 
pdf_output = 'test.pdf'
 
pdf_output = 'test.pdf'
 +
pdf_pages =430
 
pdf_width = 596.590909091
 
pdf_width = 596.590909091
 
pdf_height = 843.75
 
pdf_height = 843.75
 
imagename = '/home/dev/CairoSVG/img_65.jpg'
 
imagename = '/home/dev/CairoSVG/img_65.jpg'
#imagename = '/home/dev/CairoSVG/img_65_small.jpg'
 
 
image_x = 0
 
image_x = 0
 
image_y = 0
 
image_y = 0
Zeile 28: Zeile 28:
 
print '- erstelle pdf', pdf_output, 'w:', pdf_width, 'h:', pdf_height
 
print '- erstelle pdf', pdf_output, 'w:', pdf_width, 'h:', pdf_height
 
cairo_surface = cairo.PDFSurface(pdf_output, pdf_width, pdf_height)
 
cairo_surface = cairo.PDFSurface(pdf_output, pdf_width, pdf_height)
 +
print '  ... ', cairo_surface
  
print '- lese Bild ', imagename
+
print '- lese Bild von Datei ', imagename
input_ = open(imagename, 'rb')  # filename
+
input_ = open(imagename, 'rb')   
 
image_bytes = input_.read()
 
image_bytes = input_.read()
 
from io import BytesIO
 
from io import BytesIO
Zeile 37: Zeile 38:
 
Image.open(BytesIO(image_bytes)).save(png_file, 'PNG')
 
Image.open(BytesIO(image_bytes)).save(png_file, 'PNG')
 
png_file.seek(0)
 
png_file.seek(0)
 +
print '  ... ', png_file
  
 +
print '- lade Bild in Cairobiliothek'
 
image_surface = cairo.ImageSurface.create_from_png(png_file)
 
image_surface = cairo.ImageSurface.create_from_png(png_file)
image_width_original = image_surface.get_width()
+
image_width_original, image_height_original = image_surface.get_width(), image_surface.get_height()
image_height_original = image_surface.get_height()
+
image_scale_x, image_scale_y = pdf_width/image_width_original, pdf_height/image_height_original
image_scale_x = pdf_width / image_width_original
+
print ' ... image original: ', image_width_original, image_height_original, image_scale_x, image_scale_y
image_scale_y = pdf_height / image_height_original
 
print '- image original: ', image_width_original, image_height_original, image_scale_x, image_scale_y
 
  
 +
print '- erstelle Bild Context und zeige Bild darauf'
 
context = cairo.Context(cairo_surface)
 
context = cairo.Context(cairo_surface)
 
context.rectangle(image_x, image_y, image_width_original, image_height_original)
 
context.rectangle(image_x, image_y, image_width_original, image_height_original)
 
context.scale(image_scale_y,image_scale_y)
 
context.scale(image_scale_y,image_scale_y)
 
pattern_pattern = cairo.SurfacePattern(image_surface)
 
pattern_pattern = cairo.SurfacePattern(image_surface)
 
 
context.set_source(pattern_pattern)
 
context.set_source(pattern_pattern)
 
context.fill()
 
context.fill()
 +
print '  ... ', context
  
 +
print '- for'
 +
for i in range(pdf_pages):
 +
print'  - erstelle Seite', i+2
 +
cairo_surface.copy_page()
 +
#cairo_surface.show_page()
  
 
+
context = cairo.Context(cairo_surface)
cairo_surface.copy_page()
+
context.rectangle(image_x, image_y, image_width_original, image_height_original)
#cairo_surface.show_page()
+
context.scale(image_scale_y,image_scale_y)
 
+
pattern_pattern = cairo.SurfacePattern(image_surface)
 +
context.set_source(pattern_pattern)
 +
context.fill()
  
 
</source>
 
</source>

Aktuelle Version vom 26. November 2013, 17:22 Uhr

Beispiele

Cairo - erstelle ein pdf in python

folgender Code

  • erstellt eine pdf Datei pdf_output,
  • liest ein Bild imagename ein
  • positioniert dieses auf der ersten Seite
  • nimmt das eingelesene Bild und positioniert es als Referenz auf pdf_pages Seiten
#!/usr/bin/env python

try:
    import cairocffi as cairo
except ImportError:
    import cairo  # pycairo

pdf_output = 'test.pdf'
pdf_pages =430
pdf_width = 596.590909091
pdf_height = 843.75
imagename = '/home/dev/CairoSVG/img_65.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 '  ... ', cairo_surface

print '- lese Bild von Datei ', imagename
input_ = open(imagename, 'rb')  
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)
print '  ... ', png_file

print '- lade Bild in Cairobiliothek'
image_surface = cairo.ImageSurface.create_from_png(png_file)
image_width_original, image_height_original = image_surface.get_width(), image_surface.get_height()
image_scale_x, image_scale_y = pdf_width/image_width_original, pdf_height/image_height_original
print '  ... image original: ', image_width_original, image_height_original, image_scale_x, image_scale_y

print '- erstelle Bild Context und zeige Bild darauf'
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()
print '  ... ', context

print '- for'
for i in range(pdf_pages):
	print'   - erstelle Seite', i+2
	cairo_surface.copy_page()
	#cairo_surface.show_page()

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