Skip to content

Commit 0036041

Browse files
committed
refactored insert_image
1 parent 92addc1 commit 0036041

File tree

7 files changed

+358
-363
lines changed

7 files changed

+358
-363
lines changed

changes.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Changes in Version 1.18.13
1010
* **Added** documentation for handling transparent image insertions, :meth:`Page.insert_image`.
1111
* **Added** :meth:`Page.get_image_rects`, an improved version of :meth:`Page.get_image_bbox`.
1212
* **Changed** :meth:`Page.insert_image` to also accept the xref of an existing image in the file. This allows "copying" images between pages, and extremely fast mutiple insertions.
13-
* **Changed** :meth:`Pixmap.setAlpha` to support new parameters for pre-multiplying colors with their alpha values and setting a specific color to fully transparent (e.g. white).
13+
* **Changed** :meth:`Pixmap.set_alpha` to support new parameters for pre-multiplying colors with their alpha values and setting a specific color to fully transparent (e.g. white).
1414
* **Changed** :meth:`Document.embfile_add` to automatically set creation and modification date-time. Correspondingly, :meth:`Document.embfile_upd` automatically maintains modification date-time (``/ModDate`` PDF key), and :meth:`Document.embfile_info` correspondingly reports these data. In addition, the embedded file's associated "collection item" is included via its :data:`xref`. This supports the development of PDF portfolio applications.
1515

1616
Changes in Version 1.18.11 / 1.18.12
@@ -194,7 +194,7 @@ This is the first PyMuPDF version supporting MuPDF v1.18. The focus here is on e
194194
Changes in Version 1.17.7
195195
---------------------------
196196
* **Fixed** issue `#651 <https://github.com/pymupdf/PyMuPDF/issues/651>`_. An upstream bug causing interpreter crashes in corner case redaction processings was fixed by backporting MuPDF changes from their development repo.
197-
* **Fixed** issue `#645 <https://github.com/pymupdf/PyMuPDF/issues/645>`_. Pixmap top-left coordinates can be set (again) by their own method, :meth:`Pixmap.setOrigin`.
197+
* **Fixed** issue `#645 <https://github.com/pymupdf/PyMuPDF/issues/645>`_. Pixmap top-left coordinates can be set (again) by their own method, :meth:`Pixmap.set_origin`.
198198
* **Fixed** issue `#622 <https://github.com/pymupdf/PyMuPDF/issues/622>`_. :meth:`Page.insertImage` again accepts a :data:`rect_like` parameter.
199199
* **Added** severeal new methods to improve and speed-up table of contents (TOC) handling. Among other things, TOC items can now changed or deleted individually -- without always replacing the complete TOC. Furthermore, access to some PDF page attributes is now possible without first **loading** the page. This has a very significant impact on the performance of TOC manipulation.
200200
* **Added** an option to :meth:`Document.insert_pdf` which allows displaying progress messages. Adresses `#640 <https://github.com/pymupdf/PyMuPDF/issues/640>`_.
@@ -299,7 +299,7 @@ Changes in Version 1.16.17
299299
---------------------------
300300

301301
* **Fixed** issue `#479 <https://github.com/pymupdf/PyMuPDF/issues/479>`_. PyMuPDF should now more correctly report image resolutions. This applies to both, images (either from images files or extracted from PDF documents) and pixmaps created from images.
302-
* **Added** :meth:`Pixmap.setResolution` which sets the image resolution in x and y directions.
302+
* **Added** :meth:`Pixmap.set_dpi` which sets the image resolution in x and y directions.
303303

304304
Changes in Version 1.16.16
305305
---------------------------
@@ -354,7 +354,7 @@ Changes in Version 1.16.11
354354

355355
Changes in Version 1.16.10
356356
---------------------------
357-
* **Fixed** issue #421 ("annot.setRect(rect) has no effect on text Annotation")
357+
* **Fixed** issue #421 ("annot.set_rect(rect) has no effect on text Annotation")
358358
* **Fixed** issue #417 ("Strange behavior for page.deleteAnnot on 1.16.9 compare to 1.13.20")
359359
* **Fixed** issue #415 ("Annot.setOpacity throws mupdf warnings")
360360
* **Changed** all "add annotation / widget" methods to store a unique name in the */NM* PDF key.
@@ -542,27 +542,27 @@ Changes in Version 1.14.9
542542

543543
Changes in Version 1.14.8
544544
---------------------------
545-
* **Added** :meth:`Pixmap.setRect` to change the pixel values in a rectangle. This is also an alternative to setting the color of a complete pixmap (:meth:`Pixmap.clearWith`).
545+
* **Added** :meth:`Pixmap.set_rect` to change the pixel values in a rectangle. This is also an alternative to setting the color of a complete pixmap (:meth:`Pixmap.clear_with`).
546546
* **Fixed** an image extraction issue with JBIG2 (monochrome) encoded PDF images. The issue occurred in :meth:`Page.getText` (parameters "dict" and "rawdict") and in :meth:`Document.extractImage` methods.
547-
* **Fixed** an issue with not correctly clearing a non-alpha :ref:`Pixmap` (:meth:`Pixmap.clearWith`).
548-
* **Fixed** an issue with not correctly inverting colors of a non-alpha :ref:`Pixmap` (:meth:`Pixmap.invertIRect`).
547+
* **Fixed** an issue with not correctly clearing a non-alpha :ref:`Pixmap` (:meth:`Pixmap.clear_with`).
548+
* **Fixed** an issue with not correctly inverting colors of a non-alpha :ref:`Pixmap` (:meth:`Pixmap.invert_irect`).
549549

550550
Changes in Version 1.14.7
551551
---------------------------
552-
* **Added** :meth:`Pixmap.setPixel` to change one pixel value.
552+
* **Added** :meth:`Pixmap.set_pixel` to change one pixel value.
553553
* **Added** documentation for image conversion in the :ref:`FAQ`.
554554
* **Added** new function :meth:`getTextlength` to determine the string length for a given font.
555-
* **Added** Postscript image output (changed :meth:`Pixmap.writeImage` and :meth:`Pixmap.getImageData`).
556-
* **Changed** :meth:`Pixmap.writeImage` and :meth:`Pixmap.getImageData` to ensure valid combinations of colorspace, alpha and output format.
557-
* **Changed** :meth:`Pixmap.writeImage`: the desired format is now inferred from the filename.
555+
* **Added** Postscript image output (changed :meth:`Pixmap.save` and :meth:`Pixmap.tobytes`).
556+
* **Changed** :meth:`Pixmap.save` and :meth:`Pixmap.tobytes` to ensure valid combinations of colorspace, alpha and output format.
557+
* **Changed** :meth:`Pixmap.save`: the desired format is now inferred from the filename.
558558
* **Changed** FreeText annotations can now have a transparent background - see :meth:`Annot.update`.
559559

560560
Changes in Version 1.14.5
561561
---------------------------
562562
* **Changed:** :ref:`Shape` methods now strictly use the transformation matrix of the :ref:`Page` -- instead of "manually" calculating locations.
563563
* **Added** method :meth:`Pixmap.pixel` which returns the pixel value (a list) for given pixel coordinates.
564-
* **Added** method :meth:`Pixmap.getImageData` which returns a bytes object representing the pixmap in a variety of formats. Previously, this could be done for PNG outputs only (:meth:`Pixmap.getPNGData`).
565-
* **Changed:** output of methods :meth:`Pixmap.writeImage` and (the new) :meth:`Pixmap.getImageData` may now also be PSD (Adobe Photoshop Document).
564+
* **Added** method :meth:`Pixmap.tobytes` which returns a bytes object representing the pixmap in a variety of formats. Previously, this could be done for PNG outputs only (:meth:`Pixmap.tobytes`).
565+
* **Changed:** output of methods :meth:`Pixmap.save` and (the new) :meth:`Pixmap.tobytes` may now also be PSD (Adobe Photoshop Document).
566566
* **Added** method :meth:`Shape.drawQuad` which draws a :ref:`Quad`. This actually is a shorthand for a :meth:`Shape.drawPolyline` with the edges of the quad.
567567
* **Changed** method :meth:`Shape.drawOval`: the argument can now be **either** a rectangle (:data:`rect_like`) **or** a quadrilateral (:data:`quad_like`).
568568

@@ -760,7 +760,7 @@ Changes in Version 1.12.3
760760
--------------------------
761761
This is an extension of 1.12.2.
762762

763-
* Many functions now return *None* instead of *0*, if the result has no other meaning than just indicating successful execution (:meth:`Document.close`, :meth:`Document.save`, :meth:`Document.select`, :meth:`Pixmap.writePNG` and many others).
763+
* Many functions now return *None* instead of *0*, if the result has no other meaning than just indicating successful execution (:meth:`Document.close`, :meth:`Document.save`, :meth:`Document.select`, :meth:`Pixmap.save` and many others).
764764

765765
Changes in Version 1.12.2
766766
--------------------------
@@ -958,7 +958,7 @@ Changes in version 1.9.1 compared to version 1.8.0 are the following:
958958
* The new document method *select(list)* removes all pages from a document that are not contained in the list. Pages can also be duplicated and re-arranged.
959959
* Various improvements and new members in our demo and examples collections. Perhaps most prominently: *PDF_display* now supports scrolling with the mouse wheel, and there is a new example program *wxTableExtract* which allows to graphically identify and extract table data in documents.
960960
* *fitz.open()* is now an alias of *fitz.Document()*.
961-
* New pixmap method *getPNGData()* which will return a bytearray formatted as a PNG image of the pixmap.
961+
* New pixmap method *tobytes()* which will return a bytearray formatted as a PNG image of the pixmap.
962962
* New pixmap method *samplesRGB()* providing a *samples* version with alpha bytes stripped off (RGB colorspaces only).
963963
* New pixmap method *samplesAlpha()* providing the alpha bytes only of the *samples* area.
964964
* New iterator *fitz.Pages(doc)* over a document's set of pages.

fitz/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
fitz.Page.get_text_selection = fitz.utils.getTextSelection
9090
fitz.Page.get_text_words = fitz.utils.getTextWords
9191
fitz.Page.get_textbox = fitz.utils.getTextbox
92-
fitz.Page.insert_image = fitz.utils.insertImage
92+
fitz.Page.insert_image = fitz.utils.insert_image
9393
fitz.Page.insert_link = fitz.utils.insertLink
9494
fitz.Page.insert_text = fitz.utils.insert_text
9595
fitz.Page.insert_textbox = fitz.utils.insert_textbox
@@ -277,6 +277,24 @@ def restore_aliases():
277277
fitz.DisplayList.getPixmap = fitz.DisplayList.get_pixmap
278278
fitz.DisplayList.getTextPage = fitz.DisplayList.get_textpage
279279

280+
# deprecated Pixmap aliases
281+
fitz.Pixmap.setAlpha = fitz.Pixmap.set_alpha
282+
fitz.Pixmap.gammaWith = fitz.Pixmap.gamma_with
283+
fitz.Pixmap.tintWith = fitz.Pixmap.tint_with
284+
fitz.Pixmap.clearWith = fitz.Pixmap.clear_with
285+
fitz.Pixmap.copyPixmap = fitz.Pixmap.copy
286+
fitz.Pixmap.getImageData = fitz.Pixmap.tobytes
287+
fitz.Pixmap.getPNGData = fitz.Pixmap.tobytes
288+
fitz.Pixmap.writeImage = fitz.Pixmap.save
289+
fitz.Pixmap.writePNG = fitz.Pixmap.save
290+
fitz.Pixmap.pillowWrite = fitz.Pixmap.pil_save
291+
fitz.Pixmap.pillowData = fitz.Pixmap.pil_tobytes
292+
fitz.Pixmap.invertIRect = fitz.Pixmap.invert_irect
293+
fitz.Pixmap.setPixel = fitz.Pixmap.set_pixel
294+
fitz.Pixmap.setOrigin = fitz.Pixmap.set_origin
295+
fitz.Pixmap.setRect = fitz.Pixmap.set_rect
296+
fitz.Pixmap.setResolution = fitz.Pixmap.set_dpi
297+
280298

281299
fitz.__doc__ = """
282300
PyMuPDF %s: Python bindings for the MuPDF %s library.

fitz/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def getimage(pix):
3737
return getimage(pix1) # return the pixmap as is
3838

3939
pix = fitz.Pixmap(pix1) # copy of pix1, with an alpha channel added
40-
pix.setAlpha(pix2.samples) # treat pix2.samples as the alpha values
40+
pix.set_alpha(pix2.samples) # treat pix2.samples as the alpha values
4141
pix1 = pix2 = None # free temp pixmaps
4242

4343
# we may need to adjust something for CMYK pixmaps here:
@@ -525,7 +525,7 @@ def extract_objects(args):
525525
if pix.colorspace.n < 4
526526
else fitz.Pixmap(fitz.csRGB, pix)
527527
)
528-
pix2.writeImage(outname)
528+
pix2.save(outname)
529529

530530
if args.fonts:
531531
print("saved %i fonts to '%s'" % (len(font_xrefs), out_dir))

0 commit comments

Comments
 (0)