本文介绍了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使用例如:

>>>从 reportlab.graphics.shapes 导入图像,绘图>>>从 reportlab.platypus 导入 SimpleDocTemplate>>>从 reportlab.lib.pagesizes 导入 A4,纵向>>>从 reportlab.lib.units 导入 mm>>>img = Image(-202/25.4, -125/25.4, 210/25.4, 138/25.4, 'uneBelleImage.png') # (x, y [从左下角], width, height, path, **kw)>>>d = Drawing(0, 0) # (width, height, *nodes, **keywords)>>>d.add(img)>>>d.scale(100,100) #(%width, %height)>>>d.旋转(90)>>>报告=[]>>>报告.附加(d)>>>page = SimpleDocTemplate('toto.pdf', pagesize = Portrait(A4), rightMargin=20*mm, leftMargin=20*mm, topMargin=10*mm, bottomMargin = 10*mm)>>>page.build(报告)

这行得通,但这就像用大锤敲碎坚果?有没有更直接的方法,例如使用经典的 reportlab.platypus.Image ?提前致谢!

解决方案

要修改现有的 flowable,您应该创建一个派生类并覆盖您需要更改的方法以获得所需的行为.因此,要创建旋转图像,您需要像这样覆盖现有 Image 类的 wrap 和 draw 方法:

from reportlab.platypus.flowables import Image类 RotatedImage(Image):def wrap(self,availWidth,availHeight):h, w = Image.wrap(self,availHeight,availWidth)返回 w, h定义绘制(自我):self.canv.rotate(90)Image.draw(自我)I = RotatedImage('../images/somelogo.gif')

How can we rotate easily an image using reportlab ? I looked for and I do not found an easy method. The only way found comes from 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 using for example:

>>> 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)

This is working, but it is like using a sledgehammer to crack a nut ?Is there a more direct method, using for example the classical reportlab.platypus.Image ?Thanks by advance !

解决方案

to modify an existing flowable, you should create a derived class and override the methods you need to change to get the desired behaviour.So, to create a rotated image you need to override the wrap and draw methods of the existing Image class like this :

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')

这篇关于reportlab中旋转图片的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 11:49