本文整理匯總了Python中reportlab.platypus.flowables.Image類的典型用法代碼示例。如果您正苦於以下問題:Python Image類的具體用法?Python Image怎麽用?Python Image使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Image類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Figure
class Figure(Flowable):
def __init__(self, imgFile, captionTxt, captionStyle, imgWidth=None, imgHeight=None, margin=(0, 0, 0, 0),
padding=(0, 0, 0, 0), align=None, borderColor=(0.75, 0.75, 0.75), no_mask=False, url=None):
imgFile = imgFile
self.imgPath = imgFile
# workaround for http://code.pediapress.com/wiki/ticket/324
# see http://two.pairlist.net/pipermail/reportlab-users/2008-October/007526.html
if no_mask:
self.i = Image(imgFile, width=imgWidth, height=imgHeight, mask=None)
else:
self.i = Image(imgFile, width=imgWidth, height=imgHeight)
self.imgWidth = imgWidth
self.imgHeight = imgHeight
self.c = Paragraph(captionTxt, style=captionStyle)
self.margin = margin # 4-tuple. margins in order: top, right, bottom, left
self.padding = padding # same as above
self.borderColor = borderColor
self.align = align
self.cs = captionStyle
self.captionTxt = captionTxt
self.availWidth = None
self.availHeight = None
self.url = url
def draw(self):
canv = self.canv
if self.align == "center":
canv.translate((self.availWidth - self.width) / 2, 0)
canv.saveState()
canv.setStrokeColor(Color(self.borderColor[0], self.borderColor[1], self.borderColor[2]))
canv.rect(self.margin[3], self.margin[2], self.boxWidth, self.boxHeight)
canv.restoreState()
canv.translate(self.margin[3] + self.padding[3], self.margin[2] + self.padding[2] - 2)
self.c.canv = canv
self.c.draw()
canv.translate((self.boxWidth - self.padding[1] - self.padding[3] - self.i.drawWidth) / 2, self.captionHeight + 2)
self.i.canv = canv
self.i.draw()
if self.url:
frags = urlparse.urlsplit(self.url.encode('utf-8'))
clean_url = urlparse.urlunsplit((frags.scheme,
frags.netloc,
urllib.quote(frags.path, safe='/'),
urllib.quote(frags.query, safe='=&'),
frags.fragment,)).decode('utf-8')
canv.linkURL(clean_url, (0, 0, self.imgWidth, self.imgHeight), relative=1, thickness=0)
def wrap(self, availWidth, availHeight):
self.availWidth = availWidth
self.availHeight = availHeight
contentWidth = max(self.i.drawWidth, self.c.wrap(self.i.drawWidth, availHeight)[0])
self.boxWidth = contentWidth + self.padding[1] + self.padding[3]
(self.captionWidth, self.captionHeight) = self.c.wrap(contentWidth, availHeight)
self.captionHeight += self.cs.spaceBefore + self.cs.spaceAfter
self.boxHeight = self.i.drawHeight + self.captionHeight + self.padding[0] + self.padding[2]
self.width = self.boxWidth + self.margin[1] + self.margin[3]
self.height = self.boxHeight + self.margin[0] + self.margin[2]
return (self.width, self.height)
示例2: draw
def draw(self):
canvas = self.canv
img = Image(self.barcode)
width, height = img.wrapOn(canvas, 0, 0)
width = width * 72. / 300
height = height * 72. / 300
canvas.drawImage(
self.barcode,
self.x * mm,
(self.y * mm * -1) - height,
width,
height,
)
示例3: _pageheader
def _pageheader(self):
if self.preview:
t = self.canvas.beginText()
t.setTextOrigin(6*cm, 4*cm)
t.setFont("Times-Italic", 70)
t.setFillColorRGB(0.9,0.9,0.9)
t.textLines("PREVIEW PREVIEW")
self.canvas.rotate(45)
self.canvas.drawText(t)
self.canvas.rotate(-45)
im = Image("%s/PostgreSQL_logo.1color_blue.300x300.png" % self.imagedir, width=3*cm, height=3*cm)
im.drawOn(self.canvas, 2*cm, 25*cm)
t = self.canvas.beginText()
t.setFillColorRGB(0,0,0,0)
t.setFont("Times-Roman", 10)
t.setTextOrigin(6*cm, 27.5*cm)
t.textLines("""PostgreSQL Europe
Carpeaux Diem
13, rue du Square Carpeaux
75018 PARIS
France
""")
self.canvas.drawText(t)
t = self.canvas.beginText()
t.setTextOrigin(2*cm, 23*cm)
t.setFont("Times-Roman", 10)
t.textLine("")
t.textLines("""
Your contact: Guillaume Lelarge
Function: PostgreSQL Europe Treasurer
E-mail: [email protected]
""")
self.canvas.drawText(t)
t = self.canvas.beginText()
t.setTextOrigin(11*cm, 23*cm)
t.setFont("Times-Italic", 11)
t.textLine("To:")
t.setFont("Times-Roman", 11)
t.textLines(self.recipient)
self.canvas.drawText(t)
p = self.canvas.beginPath()
p.moveTo(2*cm, 18.9*cm)
p.lineTo(19*cm, 18.9*cm)
self.canvas.drawPath(p)
示例4: _draw_image
def _draw_image(self, img_path, width=None, height=None, **kwargs):
"""Draw an image"""
canvas = self.canv
img = Image(img_path)
_width, _height = img.wrapOn(canvas, 0, 0)
_width = _width * 72. / 300
_height = _height * 72. / 300
if width is not None:
_width = width
if height is not None:
_height = height
canvas.drawImage(
img_path,
self.cursor.x * self.unit,
self.cursor.y * self.unit - _height,
_width,
_height,
)
示例5: __init__
def __init__(self,imgFile, captionTxt, captionStyle, imgWidth=None, imgHeight=None, margin=(0,0,0,0), padding=(0,0,0,0), align=None, borderColor=(0.75,0.75,0.75), no_mask=False, url=None):
imgFile = imgFile
self.imgPath = imgFile
# workaround for http://code.pediapress.com/wiki/ticket/324
# see http://two.pairlist.net/pipermail/reportlab-users/2008-October/007526.html
if no_mask:
self.i = Image(imgFile, width=imgWidth, height=imgHeight, mask=None)
else:
self.i = Image(imgFile, width=imgWidth, height=imgHeight)
self.imgWidth = imgWidth
self.imgHeight = imgHeight
self.c = Paragraph(captionTxt, style=captionStyle)
self.margin = margin # 4-tuple. margins in order: top, right, bottom, left
self.padding = padding # same as above
self.borderColor = borderColor
self.align = align
self.cs = captionStyle
self.captionTxt = captionTxt
self.availWidth = None
self.availHeight = None
self.url = url
示例6: wrap
def wrap(self, availWidth, availHeight):
#print 123, self.drawWidth, self.drawHeight, self.pisaZoom
#self.drawWidth *= self.pisaZoom
#self.drawHeight *= self.pisaZoom
# print 456, self.drawWidth, self.drawHeight
width = min(self.drawWidth, availWidth)
# print 999, width, self.drawWidth, availWidth
factor = float(width) / self.drawWidth
# print 123, factor
self.drawHeight = self.drawHeight * factor
self.drawWidth = width
return Image.wrap(self, availWidth, availHeight)
示例7: __init__
def __init__( self, attrs ):
# PIL is required to draw images
try:
import PIL
except ImportError:
Error("""
PIL (Python Imaging Library) is required to use images in
your documents.
You should download and install it.
http://www.pythonware.com/products/pil/
""")
Properties.__init__(self)
self.graphic( attrs )
if self.properties['src']:
self.filename = self.properties['src']
else:
Error('No source defined for external-graphic element.')
Image.__init__( self, self.filename )
示例8: add_image
def add_image(self, src, width, height, align=CENTER, caption=None):
if src.split(".")[-1] in ["png", "PNG"]:
try:
f = open(src, 'rb')
data = StringIO(f.read())
except:
return
else:
img = Image(data, width, height)
f.close()
else:
img = Image(src, width, height)
img.hAlign = align
if caption:
caption_p = Paragraph(caption, self.theme.paragraph_centered)
image_table = Table([[img], [caption_p]], width)
image_table.setStyle(TableStyle([('ALIGN',(-1,-1),(-1,-1),
'CENTER')]))
self.add(image_table)
else:
self.add(img)
示例9: fill_sender
def fill_sender(self):
"""Fills sender identity"""
from reportlab.platypus.flowables import Image
from core.pdf.utils import Paragraph
# Sender identity
sender_paragraphs = []
if self.invoice_base.current_revision.sender:
sender_paragraphs.append(Paragraph(self.invoice_base.current_revision.sender, self.style['Small']))
sender_paragraphs.append(Paragraph(self.invoice_base.tenant.name, self.style['Small']))
if self.invoice_base.current_revision.sender_address:
sender_paragraphs.append(Paragraph(u'\n'.join(self.invoice_base.current_revision.sender_address.get_formatted()), self.style['Small']))
# Add layout table if logo or paragraphs
if self.invoice_base.tenant.logo_cache:
logo = Image(self.invoice_base.tenant.logo_cache)
logo_width, logo_height = logo._restrictSize(50*mm, 20*mm)
self.table(
[[logo, sender_paragraphs]],
(logo_width + 4*mm, None),
self.style['LayoutTable'],
rowHeights=(20*mm,)
)
else:
for paragraph in sender_paragraphs:
self.append(paragraph)
示例10: __init__
def __init__(self, title, enterprise, interval, logo):
self.title = title
self.enterprise = enterprise
self.interval = interval
self.logo = logo
self.width, self.height = A4
self.buf = StringIO()
self.canvas = Canvas(self.buf, pagesize=A4)
self.page_title = ''
self.page_rows = []
self.page_frags = 0
self.page_num = 1
# Build story.
self.canvas.saveState()
self.canvas.setStrokeColor(colors.RED)
self.canvas.setLineWidth(2)
self.canvas.roundRect(self.margin, self.edenwall_height + self.margin, self.width, self.height, 20, stroke=1, fill=0)
self.canvas.setFillColor(colors.GREEN2)
self.canvas.setStrokeColor(colors.GREEN1)
self.canvas.roundRect(- self.margin, - self.margin, self.width - self.margin, self.edenwall_height + self.margin,
20, stroke=1, fill=1)
# TODO do not hardcode this values.
img = Image('/var/lib/ufwi_rpcd/edenwall.png', 1209 / (300/(self.edenwall_height-self.margin/2)), self.edenwall_height-self.margin/2)
img.drawOn(self.canvas, self.margin, self.margin/4)
self.canvas.restoreState()
if self.logo:
img = Image(StringIO(self.logo))
img._setup_inner()
img.drawOn(self.canvas, (self.width - self.margin)/2 - img.drawWidth/2, 2*self.height/3)
offset = 40
self.canvas.setFillColor(black)
self.canvas.setFont("Helvetica-Bold", self.big_title_height)
self.canvas.drawCentredString((self.width-self.margin)/2, self.height/3, title)
self.canvas.setFont("Helvetica-Bold", self.frag_title_height)
self.canvas.drawString(offset, self.height - offset, enterprise)
示例11: image
def image(self, name, width, height, halign='CENTER'):
im = Image(name, width=width, height=height)
im.hAlign = halign
self._store_flowable(im)
示例12: Dessine_texte
def Dessine_texte(self, texte="", nom_categorie=None, y=0, hauteur=0):
""" Dessine le texte de la case """
if texte == None :
texte = ""
# Dessine le nom de la catégorie
if nom_categorie != None :
self.canvas.saveState()
self.canvas.setStrokeColor(ColorWxToPdf(self.parent.dictDonnees["case_titre_texte_couleur"], alpha=1))
self.canvas.setFillColor(ColorWxToPdf(self.parent.dictDonnees["case_titre_texte_couleur"], alpha=1))
self.canvas.setLineWidth(0.5)
self.canvas.setDash(0.5, 4)
self.canvas.line(0, self.hauteur_case - y +1, self.largeur_case, self.hauteur_case - y +1)
self.canvas.setFont(self.parent.dictDonnees["case_titre_nom_police"], size=self.parent.dictDonnees["case_titre_taille_police"]-2)
self.canvas.drawString(4, self.hauteur_case - y - 10, nom_categorie)
self.canvas.restoreState()
# Propriétés
self.canvas.setFillColor(ColorWxToPdf(self.parent.dictDonnees["case_texte_couleur"], alpha=1))
# Création des paragraphes
taille_police = self.parent.dictDonnees["case_taille_police"]
espace_vertical = self.parent.dictDonnees["case_espace_vertical"]
liste_paragraphes, hauteur_paragraphes = self.GetParagraphes(texte, taille_police)
ratio_depassement = (hauteur_paragraphes + (len(liste_paragraphes) - 1) * espace_vertical) / hauteur
# Vérifie si le texte ne dépasse pas de la case
if ratio_depassement > 1 :
taille_police = taille_police / ratio_depassement
liste_paragraphes, hauteur_paragraphes = self.GetParagraphes(texte, taille_police)
# Calcule l'espace vertical et la marge supérieure
if self.parent.dictDonnees["case_repartition_verticale"] == True :
# marge_haut = self.parent.dictDonnees["case_marge_haut"]
# espace_vertical = (hauteur - hauteur_paragraphes - marge_haut * 2) / (len(liste_paragraphes) - 1)
bordure = 4
espace_vertical = (hauteur - hauteur_paragraphes - bordure*2) / (len(liste_paragraphes) - 1 + 2)
marge_haut = espace_vertical + bordure
else :
espace_vertical = self.parent.dictDonnees["case_espace_vertical"]
marge_haut = (hauteur - (hauteur_paragraphes + (len(liste_paragraphes) - 1) * espace_vertical)) / 2.0
# Préparation des images
if self.parent.dictDonnees["case_separateur_type"] == "image" and self.parent.dictDonnees["case_separateur_image"] != "aucune":
img = wx.Image(Chemins.GetStaticPath("Images/Menus/%s" % self.parent.dictDonnees["case_separateur_image"]), wx.BITMAP_TYPE_ANY)
ratio_image = 1.0 * img.GetWidth() / img.GetHeight()
largeur_image = self.largeur_case / 1.5
hauteur_image = largeur_image / ratio_image
separateur_image = Image(Chemins.GetStaticPath("Images/Menus/%s" % self.parent.dictDonnees["case_separateur_image"]), width=largeur_image, height=hauteur_image)
# Dessine les lignes
y_paragraphe = self.hauteur_case - y - marge_haut
index = 0
for hauteur_paragraphe, paragraphe in liste_paragraphes:
y_paragraphe -= hauteur_paragraphe
paragraphe.drawOn(self.canvas, 0, y_paragraphe)
# Dessine l'image de séparation
if self.parent.dictDonnees["case_separateur_type"] != "aucun" and index < len(liste_paragraphes) - 1:
if self.parent.dictDonnees["case_separateur_type"] == "image" :
separateur_image.drawOn(self.canvas, self.largeur_case / 2.0 - separateur_image._width / 2.0, y_paragraphe - espace_vertical / 2.0 - separateur_image._height / 2.0)
elif self.parent.dictDonnees["case_separateur_type"] == "ligne":
largeur_separateur = self.largeur_case / 3.5
x_separateur = (self.largeur_case - largeur_separateur) / 2.0
self.canvas.setStrokeColor(ColorWxToPdf(wx.WHITE, alpha=0.2))
self.canvas.setLineWidth(0.25)
self.canvas.line(x_separateur, y_paragraphe - espace_vertical / 2.0, x_separateur + largeur_separateur, y_paragraphe - espace_vertical / 2.0)
y_paragraphe -= espace_vertical
index += 1
示例13: fill
def fill(self):
from django.template.defaultfilters import date as format_date, floatformat
from reportlab.platypus.flowables import Image
from core.pdf.utils import Paragraph
from invoicing import currency_format
# Sender frame
# Sender identity
sender_paragraphs = []
if self.invoice_base.current_revision.sender:
sender_paragraphs.append(Paragraph(self.invoice_base.current_revision.sender, self.style['Small']))
sender_paragraphs.append(Paragraph(self.invoice_base.tenant.name, self.style['Small']))
if self.invoice_base.current_revision.sender_address:
sender_paragraphs.append(Paragraph(u'\n'.join(self.invoice_base.current_revision.sender_address.get_formatted()), self.style['Small']))
# Add layout table if logo or paragraphs
if self.invoice_base.tenant.logo_cache:
logo = Image(self.invoice_base.tenant.logo_cache)
logo_width, logo_height = logo._restrictSize(50 * mm, 20 * mm)
self.table(
[[logo, sender_paragraphs]],
(logo_width + 4 * mm, None),
self.style['LayoutTable'],
rowHeights=(20 * mm,)
)
else:
for paragraph in sender_paragraphs:
self.append(paragraph)
# Billing address frame
self.next_frame()
if self.invoice_base.current_revision.contact:
self.p(self.invoice_base.current_revision.contact.get_full_name(upper_name=True), style=self.style['Address'])
if self.invoice_base.current_revision.organization:
self.p(self.invoice_base.current_revision.organization.corporate_name, style=self.style['Address'])
if self.invoice_base.current_revision.billing_address:
self.p(u'\n'.join(self.invoice_base.current_revision.billing_address.get_formatted()), style=self.style['Address'])
# Delivery address frame
self.next_frame()
if self.invoice_base.current_revision.contact:
self.p(self.invoice_base.current_revision.contact.get_full_name(upper_name=True), style=self.style['Address'])
if self.invoice_base.current_revision.organization:
self.p(self.invoice_base.current_revision.organization.corporate_name, style=self.style['Address'])
if self.invoice_base.current_revision.delivery_address:
self.p(u'\n'.join(self.invoice_base.current_revision.delivery_address.get_formatted()), style=self.style['Address'])
# Rest of the report
self.next_frame()
invoice_reference = pgettext('date', 'Undefined') if getattr(self.invoice_base, 'has_temporary_reference', None) else self.invoice_base.reference
self.table([[
' '.join([unicode(self.invoice_base.RECORD_NAME).upper(), invoice_reference]),
format_date(self.invoice_base.current_revision.invoicing_date, 'DATE_FORMAT')
]], (12 * cm, 5 * cm), style=self.style['InvoiceBaseReferencesTable'])
self.spacer()
rows = [[
pgettext('table-headers', 'Description'),
pgettext('table-headers', 'Qty'),
pgettext('table-headers', 'Unit price (excl. tax)'),
pgettext('table-headers', 'Tax'),
pgettext('table-headers', 'Total (excl. tax)')
]]
for item in self.invoice_base.current_revision.line_items:
rows.append([
item.description,
floatformat(item.quantity, -2),
currency_format(item.unit_price),
'{0:.2%}'.format(item.tax.rate),
currency_format(item.total_price, self.invoice_base.current_revision.currency.symbol)
])
col_widths = (85 * mm, 20 * mm, 20 * mm, 20 * mm, 25 * mm)
self.table(rows, col_widths, repeatRows=1, style=self.style['InvoiceBaseItemsTable'])
self.spacer()
rows = [[
_('TOTAL (excl. tax)'),
currency_format(self.invoice_base.sub_total, self.invoice_base.current_revision.currency.symbol)
]]
for tax in self.invoice_base.taxes_amounts:
rows.append([
'%(tax_name)s (%(tax_rate)s)' % {
'tax_name': tax.get('name'),
'tax_rate': '{0:.2%}'.format(tax.get('rate'))
},
currency_format(tax.get('amount'), self.invoice_base.current_revision.currency.symbol)
])
rows.append([
_('TOTAL (incl. tax)'),
currency_format(self.invoice_base.amount, self.invoice_base.current_revision.currency.symbol)
])
col_widths = (None, 25 * mm)
self.start_keeptogether()
self.table(rows, col_widths, hAlign='RIGHT', style=self.style['InvoiceBaseSummaryTable'])
self.end_keeptogether()
# Legal notices
self.spacer()
self.start_keeptogether()
if self.invoice_base.is_quotation():
self.p(_("Valid until %(quotation_validity)s") % {
#.........這裏部分代碼省略.........
示例14: draw
def draw(self):
self.canv.rotate(90)
Image.draw(self)
示例15: insertImage
def insertImage(self, imagePath, height, width):
img = Image(imagePath)
img.drawHeight = height
img.drawWidth = width
self.Story.append(img)