Bild-Seitenverhältnis mit Reportlab Python
Möchte ich ein Bild einfügen in einem Rahmen. Ich fand zwei Möglichkeiten, dies zu tun:
- drawImage(selbst -, Bild -, x -, y -, width=None, height=None, mask=None, preserveAspectRatio=False, anchor='c')
- Image(filename, width=None, height=None)
Meine Frage ist: wie kann ich ein Bild in einem Rahmen unter Beibehaltung des Seitenverhältnisses?
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Frame, Image
c = Canvas('mydoc.pdf')
frame = Frame(1*cm, 1*cm, 19*cm, 10*cm, showBoundary=1)
"""
If I have a rectangular image, I will get a square image (aspect ration
will change to 8x8 cm). The advantage here is that I use coordinates relative
to the frame.
"""
story = []
story.append(Image('myimage.png', width=8*cm, height=8*cm))
frame.addFromList(story, c)
"""
Aspect ration is preserved, but I can't use the frame's coordinates anymore.
"""
c.drawImage('myimage.png', 1*cm, 1*cm, width=8*cm, preserveAspectRatio=True)
c.save()
InformationsquelleAutor citn | 2011-03-16
Du musst angemeldet sein, um einen Kommentar abzugeben.
Können Sie das original-Bild ist eine Größe zur Berechnung des Seitenverhältnisses, dann verwenden, um skalieren Sie Ihr Ziel, Breite, Höhe. Sie können wickeln Sie diese in eine Funktion zu machen, wiederverwendbar:
Beispiel mit einem 248 x 70 pixel-stack.png:
Dies ist die beste Antwort auf diese Frage. Es gibt ähnliche Fragen sollten wir Zusammenführen in diese ein.
InformationsquelleAutor samplebias
ich hatte ein ähnliches problem und ich denke, das funktioniert:
Ich hoffe, das hilft!
InformationsquelleAutor Vlad