Looking for help in inserting a rotated HTML box #4448
Unanswered
XHJ-TS9527
asked this question in
Looking for help
Replies: 1 comment
-
The problem can be solved if I initialize a new A temporary solution: import fitz
from PIL import Image
import numpy as np
# Step 1: Open the image and turn it into a PDF file
image = fitz.open(imagePath)
imageBytes = image.convert_to_pdf()
document = fitz.open(stream=documentBytes)
image.close()
imagePage = document[0]
fontArchive = fitz.Archive(fontPath) # fontPath refers to the directory containing font files
# Step 2: Put each text clip on the image
for textClip in textClips:
text = textClip['text'] # textClip['text'] is the HTML string
textCss = textClip['textCss'] # textClip['textCss'] is the CSS string
textBox = fitz.Rect(*textClip['box']) # textClip['box'] is a tuple with four numbers, with (0, 0) as the left-up point
draftDocument = fitz.Document()
draftDocument.insert_page(len(draftDocument), width=textBox.width, height=textBox.height)
draftPage = draftDocument[-1]
draftPage.insert_htmlbox(textBox, text, css=textCss, archive=fontArchive)
# Step 3: Calculate the zone to paste the rotated draft page
rotationAngle = textClip['angle'] # rotation angle in degrees, and the rotation direction is counterclockwise
textCenterPoint = np.array(textClip['centerPoint']) # textClip['centerPoint'] is a tuple with two numbers indicating the exact location of the center point of the text box
roationAngleRad = np.deg2rad(rotationAngle)
rotationMatrix = np.array([[np.cos(rotationAngleRad), -np.sin(rotationAngleRad)],
[np.sin(rotationAngleRad), np.cos(rotationAngleRad)]])
originalPoints = np.array(textClip['box']) - textCenterPoint # originalPoints will be a 4×2 ndarray
rotatedPoints = (rotationMatrix @ originalPoints.T).T + textCenterPoint # rotatedPoints will indicate the outmost boundary of the text box
rotatedPointXs = rotatedPoints[:, 0].tolist()
rotatedPointYs = rotatedPoints[:, 1].tolist()
leftBoundary = min(rotatedPointXs)
upBoundary = min(rotatedPointYs)
rightBoundary = max(rotatedPointXs)
downBoundary = max(rotatedPointYs)
pasteZoneRect = fitz.Rect(leftBoundary, upBoundary, rightBoundary, downBoundary)
# Step 4: Copy the page from the draft document to the image document with rotation
imagePage.show_pdf_page(pasteZoneRect, draftDocument, len(draftDocument) - 1, rotate=rotationAngle)
draftDocument.close()
# Step 5: Turn the page into an image and save it
image = Image.open(imagePath)
image = imagePage.get_pixmap(dpi=max(image.info['dpi']))
image.save(imagePath) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm currently trying to put some text on an image. The text includes characters in different languages, so I have to use different fonts to display them correctly. It seems that the
insert_htmlbox
function can meet my requirements, so I'm using this function to put the text on the image. However, the document says that the rotation angle should be a multiple of 90°, which is very inflexible. Therefore, I try to use the following solution to fulfill my requirements in putting text on an image with an arbitrary rotation angle:I think this approach should work. But when I run the program, I get an exception thrown by
pymupdf.mupdf.FzErrorArgument
. The exception information iscode=4: source object number out of range
. I don't know how this exception occurs, as I have put text on draftPage using the providedinsert_htmlbox
function. Can anyone give me some suggestions? Thanks in advance.Beta Was this translation helpful? Give feedback.
All reactions