Cairo: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
F (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
F (Diskussion | Beiträge) |
||
| (Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
| 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 | ||
try: | try: | ||
import cairocffi as cairo | import cairocffi as cairo | ||
| Zeile 10: | Zeile 18: | ||
import cairo # pycairo | 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' | imagename = '/home/dev/CairoSVG/img_65.jpg' | ||
image_x = 0 | |||
image_y = 0 | |||
print '- erstelle pdf' | 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 ', imagename | print '- lese Bild von Datei ', imagename | ||
input_ = open(imagename, 'rb') | input_ = open(imagename, 'rb') | ||
image_bytes = input_.read() | image_bytes = input_.read() | ||
from io import BytesIO | from io import BytesIO | ||
| Zeile 38: | 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_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) | 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() | |||
</source> | </source> | ||
* Quellen | * Quellen | ||
** [http://cairosvg.org CairoSVG] | ** [http://cairosvg.org CairoSVG] | ||
** [https://github.com/Kozea/CairoSVG github.com/Kozea/CairoSVG] | ** [https://github.com/Kozea/CairoSVG github.com/Kozea/CairoSVG] | ||
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()
- Quellen