|
12 | 12 | import os
|
13 | 13 | import random
|
14 | 14 | import string
|
| 15 | +import tempfile |
15 | 16 | import typing
|
16 | 17 | import warnings
|
17 |
| -import tempfile |
18 | 18 |
|
19 | 19 | from fitz import *
|
20 | 20 |
|
21 |
| - |
22 | 21 | TESSDATA_PREFIX = os.environ.get("TESSDATA_PREFIX")
|
23 | 22 | point_like = "point_like"
|
24 | 23 | rect_like = "rect_like"
|
@@ -236,6 +235,51 @@ def calc_matrix(sr, tr, keep=True, rotate=0):
|
236 | 235 | return xref
|
237 | 236 |
|
238 | 237 |
|
| 238 | +def replace_image(page: Page, xref: int, *, filename=None, pixmap=None, stream=None): |
| 239 | + """Replace the image referred to by xref. |
| 240 | +
|
| 241 | + Replace the image by changing the object definition stored under xref. This |
| 242 | + will leave the pages appearance instructions intact, so the new image is |
| 243 | + being displayed with the same bbox, rotation etc. |
| 244 | + By providing a small fully transparent image, an effect as if the image had |
| 245 | + been deleted can be achieved. |
| 246 | + A typical use may include replacing large images by a smaller version, |
| 247 | + e.g. with a lower resolution or graylevel instead of colored. |
| 248 | +
|
| 249 | + Args: |
| 250 | + xref: the xref of the image to replace. |
| 251 | + filename, pixmap, stream: exactly one of these must be provided. The |
| 252 | + meaning being the same as in Page.insert_image. |
| 253 | + """ |
| 254 | + doc = page.parent # the owning document |
| 255 | + if not doc.is_image(xref): |
| 256 | + raise ValueError("xref not an image") # insert new image anywhere in page |
| 257 | + if bool(filename) + bool(stream) + bool(pixmap) != 1: |
| 258 | + raise ValueError("Exactly one of filename/stream/pixmap must be given") |
| 259 | + new_xref = page.insert_image( |
| 260 | + page.rect, filename=filename, stream=stream, pixmap=pixmap |
| 261 | + ) |
| 262 | + doc.xref_copy(new_xref, xref) # copy over new to old |
| 263 | + last_contents_xref = page.get_contents()[-1] |
| 264 | + # new image insertion has created a new /Contents source, |
| 265 | + # which we will set to spaces now |
| 266 | + doc.update_stream(last_contents_xref, b" ") |
| 267 | + |
| 268 | + |
| 269 | +def delete_image(page: Page, xref: int): |
| 270 | + """Delete the image referred to by xef. |
| 271 | +
|
| 272 | + Actually replaces by a small transparent Pixmap using method Page.replace_image. |
| 273 | +
|
| 274 | + Args: |
| 275 | + xref: xref of the image to delete. |
| 276 | + """ |
| 277 | + # make a small 100% transparent pixmap (of just any dimension) |
| 278 | + pix = fitz.Pixmap(fitz.csGRAY, (0, 0, 1, 1), 1) |
| 279 | + pix.clear_with() # clear all samples bytes to 0x00 |
| 280 | + page.replace_image(xref, pixmap=pix) |
| 281 | + |
| 282 | + |
239 | 283 | def insert_image(page, rect, **kwargs):
|
240 | 284 | """Insert an image for display in a rectangle.
|
241 | 285 |
|
@@ -810,15 +854,15 @@ def get_page_text(
|
810 | 854 |
|
811 | 855 |
|
812 | 856 | def get_pixmap(
|
813 |
| - page: Page, |
814 |
| - *, |
815 |
| - matrix: matrix_like=Identity, |
816 |
| - dpi=None, |
817 |
| - colorspace: Colorspace=csRGB, |
818 |
| - clip: rect_like=None, |
819 |
| - alpha: bool=False, |
820 |
| - annots: bool=True, |
821 |
| - ) -> Pixmap: |
| 857 | + page: Page, |
| 858 | + *, |
| 859 | + matrix: matrix_like = Identity, |
| 860 | + dpi=None, |
| 861 | + colorspace: Colorspace = csRGB, |
| 862 | + clip: rect_like = None, |
| 863 | + alpha: bool = False, |
| 864 | + annots: bool = True, |
| 865 | +) -> Pixmap: |
822 | 866 | """Create pixmap of page.
|
823 | 867 |
|
824 | 868 | Keyword args:
|
@@ -876,7 +920,12 @@ def get_page_pixmap(
|
876 | 920 | annots: (bool) also render annotations
|
877 | 921 | """
|
878 | 922 | return doc[pno].get_pixmap(
|
879 |
| - matrix=matrix, dpi=dpi, colorspace=colorspace, clip=clip, alpha=alpha, annots=annots |
| 923 | + matrix=matrix, |
| 924 | + dpi=dpi, |
| 925 | + colorspace=colorspace, |
| 926 | + clip=clip, |
| 927 | + alpha=alpha, |
| 928 | + annots=annots, |
880 | 929 | )
|
881 | 930 |
|
882 | 931 |
|
|
0 commit comments