A simple method for rotating images in reportlab

2024/11/12 23:53:41

How can we easily rotate an image using reportlab? I have 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 using a sledgehammer to crack a nut. Is there a more direct method, using for example the classical reportlab.platypus.Image?

Answer

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 Imageclass RotatedImage(Image):def wrap(self,availWidth,availHeight):h, w = Image.wrap(self,availHeight,availWidth)return w, hdef draw(self):self.canv.rotate(90)Image.draw(self)I = RotatedImage('../images/somelogo.gif')
https://en.xdnf.cn/q/72075.html

Related Q&A

XML header getting removed after processing with elementtree

i have an xml file and i used Elementtree to add a new tag to the xml file.My xml file before processing is as follows <?xml version="1.0" encoding="utf-8"?><PackageInfo …

How to ntp server time down to millisecond precision using Python ntplib?

I am creating a python module that will output the time from a selection of NTP Pool servers to millisecond precision as an exercise in showing how server timestamps vary. Thus far I have been able to …

Control 2 separate Excel instances by COM independently... can it be done?

Ive got a legacy application which is implemented in a number of Excel workbooks. Its not something that I have the authority to re-implement, however another application that I do maintain does need t…

nested Python numpy arrays dimension confusion

Suppose I have a numpy array c constructed as follows:a = np.zeros((2,4)) b = np.zeros((2,8)) c = np.array([a,b])I would have expected c.shape to be (2,1) or (2,) but instead it is (2,2). Additionally,…

how to reuse tests written using unittest.testcase

Ive written some tests using unittest as below and I want to reuse them in another class where Im stuck and need help.. Code snippets are as below.MyTestClass.pyClass MyTestClass(unittest.TestCase): …

Randomized stratified k-fold cross-validation in scikit-learn?

Is there any built-in way to get scikit-learn to perform shuffled stratified k-fold cross-validation? This is one of the most common CV methods, and I am surprised I couldnt find a built-in method to …

Find all paths through a tree (nested dicts) from top to bottom

EDIT: See below for a suggested answer and how its not quite right yet.There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. Im a programming novice, so pl…

How to show process state (blocking, non-blocking) in Linux

Is there a way to query the state of processes in a Linux process table to be able to demonstrate if a process is running or blocked at the time the query is executed? My goal is to do this from outsi…

Removing quotation marks from list items

I am running this program:f = open( "animals.txt", "r") g = f.read() g1 = g.split(,) #turning the file into list print g1And I want this to come out:[ELDEN, DORSEY, DARELL, BRODERIC…

Handle multiple questions for Telegram bot in python

Im programming a telegram bot in Python using the Telegram bot API. Im facing the problem of managing questions that need an answer of the user. The problem arises when the program is waiting for an an…