-
Hello! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
You should be able to compute mm by using these equalities: 72 points = 1 inch, 1 inch = 25.4 mm. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
import fitz
file = '56204820.pdf'
regnum = 'regnum.png'
# Coordinates of the top-left corner: x=24mm, y=88mm
# Width of the image: 77mm and height image: 7mm.
coords = [24, 88, 77, 7]
def func_coords(*args):
print(args)
point_coords = []
for c in args[0][:2]:
point_coords.append(c / 25.4 * 72)
for c in args[0][2:]:
points = c / 25.4 * 72
if c == args[0][2]:
result = 595 - points - args[0][0]
else:
result = 842 - points - args[0][1]
point_coords.append(result)
return tuple(point_coords)
def main():
pdf = fitz.open(file)
page = pdf[0]
point_coords = func_coords(coords)
print(point_coords)
rect = fitz.Rect(point_coords)
img = open(regnum, "rb").read() # an image file
page.wrapContents()
page.insertImage(rect, stream=img)
pdf.save('result.pdf')
if __name__ == "__main__":
main() This is my script, maybe there is a problem in the code? String page.wrapContents() it didn't help me, image is much lower for Y coordinate than it should be. |
Beta Was this translation helpful? Give feedback.
-
A rectangle must be given as >>> rect = fitz.Rect(24, 88, 24+77, 88+7)
>>> rect = rect / 25.4 * 72
>>> rect
Rect(68.03149606299213, 249.4488188976378, 286.2992125984252, 269.29133858267716)
>>> |
Beta Was this translation helpful? Give feedback.
-
Excellent!! It works! |
Beta Was this translation helpful? Give feedback.
A rectangle must be given as
rect = fitz.Rect(x0, y0, x1, y1)
where (x0, y0) is the top-left point and (x1, y1) the bottom-right one. If you have the the top-left only plus the width / height, the calculation is thereforerect = fitz.Rect(x0, y0, x0 + width, y0 + height)
.So in your case
rect = fitz.Rect(24, 88, 24+77, 88+7)
. Then simply do this to convert the whole thing to pixels: