Cairo: Unterschied zwischen den Versionen
Aus Vosp.info
F (Diskussion | Beiträge) |
F (Diskussion | Beiträge) |
||
Zeile 5: | Zeile 5: | ||
<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: | ||
import cairocffi as cairo | import cairocffi as cairo | ||
Zeile 10: | Zeile 18: | ||
import cairo # pycairo | 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.jpg' | ||
− | + | #imagename = '/home/dev/CairoSVG/img_65_small.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 '- lese Bild ', imagename | print '- lese Bild ', imagename | ||
Zeile 40: | Zeile 39: | ||
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_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) | pattern_pattern = cairo.SurfacePattern(image_surface) | ||
− | |||
− | + | context.set_source(pattern_pattern) | |
+ | context.fill() | ||
+ | |||
+ | |||
+ | |||
cairo_surface.copy_page() | cairo_surface.copy_page() | ||
− | cairo_surface.show_page() | + | #cairo_surface.show_page() |
+ | |||
+ | |||
</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] |
Version vom 26. November 2013, 16:23 Uhr
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