python - reportlab中旋转图片的简单方法

标签 python reportlab

我们如何使用 reportlab 轻松旋转图像?我还没有找到一个简单的方法。找到的唯一方法来自 http://dods.ipsl.jussieu.fr/orchidee/SANORCHIDEE/TEMP/TEMP_LOCAL/cdat_portable/lib_new_wrong_gcc/python2.4/site-packages/reportlab/test/test_graphics_images.py使用例如:

>>> from reportlab.graphics.shapes import Image, Drawing
>>> from reportlab.platypus import SimpleDocTemplate
>>> from reportlab.lib.pagesizes import A4, portrait
>>> from reportlab.lib.units import mm
>>> img = Image(-202/25.4, -125/25.4, 210/25.4, 138/25.4, 'uneBelleImage.png') # (x, y [from lower left corner], width, height, path, **kw)
>>> d = Drawing(0, 0) # (width, height, *nodes, **keywords)
>>> d.add(img)
>>> d.scale(100,100) #(%width, %height)
>>> d.rotate(90)
>>> report=[]
>>> report.append(d)
>>> page = SimpleDocTemplate('toto.pdf', pagesize = portrait(A4), rightMargin=20*mm, leftMargin=20*mm, topMargin=10*mm, bottomMargin = 10*mm)
>>> page.build(report)

这是可行的,但它是用大锤来敲螺母。 是否有更直接的方法,例如使用经典的 reportlab.platypus.Image?

最佳答案

要修改现有的可流动对象,您应该创建一个派生类并覆盖您需要更改的方法以获得所需的行为。 因此,要创建旋转图像,您需要重写现有 Image 类的 wrap 和 draw 方法,如下所示:

from reportlab.platypus.flowables import Image

class RotatedImage(Image):

    def wrap(self,availWidth,availHeight):
        h, w = Image.wrap(self,availHeight,availWidth)
        return w, h
    def draw(self):
        self.canv.rotate(90)
        Image.draw(self)

I = RotatedImage('../images/somelogo.gif')

关于python - reportlab中旋转图片的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29848988/

相关文章:

ReportLab 段落中的字母间距

python - 带有日期和时间的 reportlab LinePlot 轴

python - 如何在 ReportLab 中将酒吧聊天中的每个单独的酒吧设置为不同的颜色?

python - 切片 Pandas 数据帧非单调索引

python - 在python中重新排列嵌套字典的级别

python - 卡尔曼滤波器 - 两个相同传感器的融合

python - 打印为 PDF Django 和 ReportLab

python - 如何在 reportlab (python) 中使用 drawString 方法在一行中添加粗体和普通文本

python - 如何在Python中从字符串中就地删除重复项?

python - 如何设置 Atom 的 'styles.less' 文件以突出显示 Python 中的函数和方法调用?