使用 Reportlab 将旋转图像居中

问题描述 投票:0回答:1

我正在尝试将旋转图像在 Reportlab 上居中,但在使用正确的放置计算时遇到问题。

这是当前代码:

from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from PIL import Image as PILImage

import requests
import math


def main(rotation):
    # create a new PDF with Reportlab
    a4 = (595.275590551181, 841.8897637795275)
    c = canvas.Canvas('output.pdf', pagesize=a4)
    c.saveState()

    # loading the image:
    img = requests.get('https://i.stack.imgur.com/dI5Rj.png', stream=True)

    img = PILImage.open(img.raw)
    width, height = img.size

    # We calculate the bouding box of a rotated rectangle
    angle_radians = rotation * (math.pi / 180)
    bounding_height = abs(width * math.sin(angle_radians)) + abs(height * math.cos(angle_radians))
    bounding_width = abs(width * math.cos(angle_radians)) + abs(height * math.sin(angle_radians))

    a4_pixels = [x * (100 / 75) for x in a4]

    offset_x = (a4_pixels[0] / 2) - (bounding_width / 2)
    offset_y = (a4_pixels[1] / 2) - (bounding_height / 2)
    c.translate(offset_x, offset_y)

    c.rotate(rotation)
    c.drawImage(ImageReader(img), 0, 0, width, height, 'auto')

    c.restoreState()
    c.save()


if __name__ == '__main__':
    main(45)

到目前为止,这就是我所做的:

  1. 计算旋转矩形的边界(因为它会更大)
  2. 使用这些来计算图像中心的位置(尺寸/2 - 图像/2)的宽度和高度。

出现两个问题我无法解释:

  1. “a4”变量以点为单位,其他所有内容均以像素为单位。如果我将它们更改为像素来计算位置(这是合乎逻辑的,使用
    a4_pixels = [x * (100 / 75) for x in a4]
    )。对于 0 度旋转,放置不正确。如果我将 a4 保留为点,它会起作用......?
  2. 如果我改变旋转方向,它会破裂得更多。

所以我的最后一个问题:如何计算

offset_x
offset_y
值以确保无论旋转如何它始终居中?

python-3.x pdf reportlab
1个回答
3
投票

当您平移画布时,您实际上是在移动原点 (0,0),所有绘制操作都将与其相关。

所以在下面的代码中,我将原点移到了页面的中间。 然后我旋转“页面”并在“页面”上绘制图像。无需旋转图像,因为其画布轴已旋转。

from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from reportlab.lib.pagesizes import A4
from PIL import Image as PILImage
import requests

def main(rotation):
    c = canvas.Canvas('output.pdf', pagesize=A4)
    c.saveState()

    # loading the image:
    img = requests.get('https://i.stack.imgur.com/dI5Rj.png', stream=True)
    img = PILImage.open(img.raw)
    # The image dimensions in cm
    width, height = img.size

    # now move the canvas origin to the middle of the page
    c.translate(A4[0] / 2, A4[1] / 2)
    # and rotate it
    c.rotate(rotation)
    # now draw the image relative to the origin
    c.drawImage(ImageReader(img), -width/2, -height/2, width, height, 'auto')

    c.restoreState()
    c.save()

if __name__ == '__main__':
    main(45)
© www.soinside.com 2019 - 2024. All rights reserved.