PyMuPDF 1.24.4 中文文档(十一)(3)https://developer.aliyun.com/article/1559463
一些示例
使用数字进行操作
对于通常的算术操作,数字始终允许作为第二个操作数。此外,您可以制定 "x in OBJ"
,其中 x 是一个数字。它实现为 "x in tuple(OBJ)"
:
>>> pymupdf.Rect(1, 2, 3, 4) + 5 pymupdf.Rect(6.0, 7.0, 8.0, 9.0) >>> 3 in pymupdf.Rect(1, 2, 3, 4) True >>>
创建文档页面矩形的左上角四分之一:
>>> page.rect Rect(0.0, 0.0, 595.0, 842.0) >>> page.rect / 2 Rect(0.0, 0.0, 297.5, 421.0) >>>
以下将提供连接两点 p1 和 p2 的线的中点:
>>> p1 = pymupdf.Point(1, 2) >>> p2 = pymupdf.Point(4711, 3141) >>> mp = (p1 + p2) / 2 >>> mp Point(2356.0, 1571.5) >>>
使用“类似”对象进行操作
二元操作的第二个操作数始终可以“类似”于左操作数。在此上下文中,“类似”表示“具有相同长度的数字序列”。使用上述示例:
>>> p1 + p2 Point(4712.0, 3143.0) >>> p1 + (4711, 3141) Point(4712.0, 3143.0) >>> p1 += (4711, 3141) >>> p1 Point(4712.0, 3143.0) >>>
要将矩形向右移动 5 个像素,请执行以下操作:
>>> pymupdf.Rect(100, 100, 200, 200) + (5, 0, 5, 0) # add 5 to the x coordinates Rect(105.0, 100.0, 205.0, 200.0) >>>
点、矩形和矩阵可以使用矩阵进行变换。在 PyMuPDF 中,我们将其视为**“乘法”(或“除法”**),其中第二个操作数可以“类似”于矩阵。在此上下文中,“除法”表示“与倒置矩阵的乘法”:
>>> m = pymupdf.Matrix(1, 2, 3, 4, 5, 6) >>> n = pymupdf.Matrix(6, 5, 4, 3, 2, 1) >>> p = pymupdf.Point(1, 2) >>> p * m Point(12.0, 16.0) >>> p * (1, 2, 3, 4, 5, 6) Point(12.0, 16.0) >>> p / m Point(2.0, -2.0) >>> p / (1, 2, 3, 4, 5, 6) Point(2.0, -2.0) >>> >>> m * n # matrix multiplication Matrix(14.0, 11.0, 34.0, 27.0, 56.0, 44.0) >>> m / n # matrix division Matrix(2.5, -3.5, 3.5, -4.5, 5.5, -7.5) >>> >>> m / m # result is equal to the Identity matrix Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) >>> >>> # look at this non-invertible matrix: >>> m = pymupdf.Matrix(1, 0, 1, 0, 1, 0) >>> ~m Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) >>> # we try dividing by it in two ways: >>> p = pymupdf.Point(1, 2) >>> p * ~m # this delivers point (0, 0): Point(0.0, 0.0) >>> p / m # but this is an exception: Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> p / m File "... /site-packages/fitz/pymupdf.py", line 869, in __truediv__ raise ZeroDivisionError("matrix not invertible") ZeroDivisionError: matrix not invertible >>>
作为特殊情况,矩形支持附加的二元操作:
- 交集 – 矩形类的公共区域,操作符 “&”
- 包含 – 扩展以包含点状或矩形状,操作符 “|”
- 包含检查 – 点状或矩形状是否在内部
这是一个创建包围给定点的最小矩形的示例:
>>> # first define some point-likes >>> points = [] >>> for i in range(10): for j in range(10): points.append((i, j)) >>> >>> # now create a rectangle containing all these 100 points >>> # start with an empty rectangle >>> r = pymupdf.Rect(points[0], points[0]) >>> for p in points[1:]: # and include remaining points one by one r |= p >>> r # here is the to be expected result: Rect(0.0, 0.0, 9.0, 9.0) >>> (4, 5) in r # this point-like lies inside the rectangle True >>> # and this rect-like is also inside >>> (4, 4, 5, 5) in r True >>>
您对本页有何反馈?
本软件按“原样”提供,不提供任何明示或暗示的担保。本软件在许可下分发,并且除非在该许可条款明确授权下,否则不得复制、修改或分发。请参阅 artifex.com 上的许可信息或联系美国加利福尼亚州旧金山 Mesa 街 39 号 108A 室的 Artifex Software Inc. 获取更多信息。
此文档涵盖了所有版本,直至 1.24.4。
使用数字进行操作
对于通常的算术操作,数字始终允许作为第二个操作数。此外,您可以制定 "x in OBJ"
,其中 x 是一个数字。它实现为 "x in tuple(OBJ)"
:
>>> pymupdf.Rect(1, 2, 3, 4) + 5 pymupdf.Rect(6.0, 7.0, 8.0, 9.0) >>> 3 in pymupdf.Rect(1, 2, 3, 4) True >>>
创建文档页面矩形的左上角四分之一:
>>> page.rect Rect(0.0, 0.0, 595.0, 842.0) >>> page.rect / 2 Rect(0.0, 0.0, 297.5, 421.0) >>>
以下将提供连接两点 p1 和 p2 的线的中点:
>>> p1 = pymupdf.Point(1, 2) >>> p2 = pymupdf.Point(4711, 3141) >>> mp = (p1 + p2) / 2 >>> mp Point(2356.0, 1571.5) >>>
使用“类似”对象进行操作
二元操作的第二个操作数始终可以“类似”于左操作数。在此上下文中,“类似”表示“具有相同长度的数字序列”。使用上述示例:
>>> p1 + p2 Point(4712.0, 3143.0) >>> p1 + (4711, 3141) Point(4712.0, 3143.0) >>> p1 += (4711, 3141) >>> p1 Point(4712.0, 3143.0) >>>
要将矩形向右移动 5 个像素,请执行以下操作:
>>> pymupdf.Rect(100, 100, 200, 200) + (5, 0, 5, 0) # add 5 to the x coordinates Rect(105.0, 100.0, 205.0, 200.0) >>>
点、矩形和矩阵可以通过矩阵进行转换。在 PyMuPDF 中,我们将其视为**“乘法”(或“除法”**),其中第二个操作数可能“类似”于一个矩阵。在这种情况下,“除法”意味着“乘以倒置矩阵”:
>>> m = pymupdf.Matrix(1, 2, 3, 4, 5, 6) >>> n = pymupdf.Matrix(6, 5, 4, 3, 2, 1) >>> p = pymupdf.Point(1, 2) >>> p * m Point(12.0, 16.0) >>> p * (1, 2, 3, 4, 5, 6) Point(12.0, 16.0) >>> p / m Point(2.0, -2.0) >>> p / (1, 2, 3, 4, 5, 6) Point(2.0, -2.0) >>> >>> m * n # matrix multiplication Matrix(14.0, 11.0, 34.0, 27.0, 56.0, 44.0) >>> m / n # matrix division Matrix(2.5, -3.5, 3.5, -4.5, 5.5, -7.5) >>> >>> m / m # result is equal to the Identity matrix Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) >>> >>> # look at this non-invertible matrix: >>> m = pymupdf.Matrix(1, 0, 1, 0, 1, 0) >>> ~m Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) >>> # we try dividing by it in two ways: >>> p = pymupdf.Point(1, 2) >>> p * ~m # this delivers point (0, 0): Point(0.0, 0.0) >>> p / m # but this is an exception: Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> p / m File "... /site-packages/fitz/pymupdf.py", line 869, in __truediv__ raise ZeroDivisionError("matrix not invertible") ZeroDivisionError: matrix not invertible >>>
作为特殊功能,矩形支持额外的二元操作:
- 交集 – 矩形类的公共区域,操作符 “&”
- 包含 – 扩展以包含一个点或矩形,操作符 “|”
- 包含性检查 – 检查一个点或矩形是否在内部
这里是创建包围给定点的最小矩形的示例:
>>> # first define some point-likes >>> points = [] >>> for i in range(10): for j in range(10): points.append((i, j)) >>> >>> # now create a rectangle containing all these 100 points >>> # start with an empty rectangle >>> r = pymupdf.Rect(points[0], points[0]) >>> for p in points[1:]: # and include remaining points one by one r |= p >>> r # here is the to be expected result: Rect(0.0, 0.0, 9.0, 9.0) >>> (4, 5) in r # this point-like lies inside the rectangle True >>> # and this rect-like is also inside >>> (4, 4, 5, 5) in r True >>>
对本页有任何反馈吗?
此软件按原样提供,没有任何明示或暗示的保证。此软件根据许可分发,并且未经明确授权不得复制、修改或分发。请参阅许可信息,位于 artifex.com 或联系美国加利福尼亚州旧金山 Mesa 街 39 号 108A 室的 Artifex Software Inc. 获取更多信息。
此文档涵盖了所有版本直到 1.24.4。
低级功能和类
包含多个供有经验用户使用的功能和类。用于特殊需求或性能要求。
- 功能
- 设备
- 共同工作:DisplayList 和 TextPage
你对本页面有任何反馈吗?
此软件按原样提供,不提供任何明示或暗示的保证。此软件在许可下分发,未经许可明确授权,不得复制、修改或分发。请参阅artifex.com上的许可信息或联系美国旧金山 CA 94129 Mesa Street 39 号 108A 套房的 Artifex Software Inc.以获取更多信息。
此文档涵盖了所有版本直至 1.24.4。
PyMuPDF 1.24.4 中文文档(十一)(5)https://developer.aliyun.com/article/1559465