### Install Tesseract Language Packs (Ubuntu) Source: https://pymupdf.readthedocs.io/en/latest/_sources/pymupdf4llm/index.rst Example command to install Tesseract OCR language packs on Ubuntu. Ensure the correct language codes are used. ```bash sudo apt install tesseract-ocr-deu tesseract-ocr-fra ``` -------------------------------- ### Python Virtual Environment Setup (Linux/macOS) Source: https://pymupdf.readthedocs.io/en/latest/_sources/installation.rst Commands to create and activate a Python virtual environment on Linux and macOS systems, preparing for package installations. ```bash python -m venv pymupdf-venv . pymupdf-venv/bin/activate python -m pip install --upgrade pip ``` -------------------------------- ### Install PyMuPDF in Docker Source: https://pymupdf.readthedocs.io/en/latest/faq/index.html This Dockerfile example shows how to install PyMuPDF and Tesseract OCR with English language data in a Python 3.11 slim container. Standard pip installation is used. ```dockerfile FROM python:3.11-slim RUN apt-get update && apt-get install -y tesseract-ocr tesseract-ocr-eng RUN pip install pymupdf ``` -------------------------------- ### Python Virtual Environment Setup (Windows) Source: https://pymupdf.readthedocs.io/en/latest/_sources/installation.rst Commands to create and activate a Python virtual environment on Windows, ensuring a clean environment for package installations. ```bash py -m venv pymupdf-venv .\pymupdf-venv\Scripts\activate python -m pip install --upgrade pip ``` -------------------------------- ### Install Tesseract Language Packs on Fedora/RHEL Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Use `dnf search` to find available language packs, `dnf install` to install a specific language (e.g., German: `tesseract-langpack-deu`), or `dnf install tesseract-langpack-*` to install all language packs. ```bash # Search for available language packs dnf search tesseract # Install a specific language (e.g. German) sudo dnf install tesseract-langpack-deu # Install all language packs sudo dnf install tesseract-langpack-* ``` -------------------------------- ### Install Tesseract Language Packs on Ubuntu/Debian Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Use `apt-cache search` to find available language packs, `apt install` to install a specific language (e.g., German: `tesseract-ocr-deu`), or `apt install tesseract-ocr-all` to install all available languages. ```bash # List all available language packs apt-cache search tesseract-ocr # Install a specific language (e.g. German) sudo apt install tesseract-ocr-deu # Install all available languages at once sudo apt install tesseract-ocr-all ``` -------------------------------- ### Run Full Build, Install, and Test with sysinstall.py Source: https://pymupdf.readthedocs.io/en/latest/packaging.html Execute the `sysinstall.py` script to perform a complete build, installation, and testing process for both a local fake root and the system root. ```bash ./PyMuPDF/scripts/sysinstall.py ./PyMuPDF/scripts/sysinstall.py --root / ``` -------------------------------- ### Install PyMuPDF from source (Windows) Source: https://pymupdf.readthedocs.io/en/latest/_sources/installation.rst Instructions for installing PyMuPDF from source on Windows, which requires C/C++ development tools like Visual Studio 2019. Environmental variables may need to be set. ```bash # On Windows: # Install Visual Studio 2019. # If not installed in a standard location, set environmental variable PYMUPDF_SETUP_DEVENV to the location of the devenv.com binary. ``` -------------------------------- ### Install PyMuPDF4LLM Source: https://pymupdf.readthedocs.io/en/latest/_sources/pymupdf4llm/index.rst Install the package via pip. ```bash pip install pymupdf4llm ``` -------------------------------- ### Install PyMuPDF Source: https://pymupdf.readthedocs.io/en/latest/faq/index.html Use this command to install the PyMuPDF package. Ensure you are installing 'pymupdf' and not similar package names. ```bash pip install pymupdf ``` -------------------------------- ### Install PyMuPDF Pro Source: https://pymupdf.readthedocs.io/en/latest/_sources/pymupdf-pro/index.rst Install PyMuPDF Pro using pip. This command is used to add the commercial extension to your PyMuPDF installation. ```bash pip install pymupdfpro ``` -------------------------------- ### PyMuPDF Cache Size Management Example Source: https://pymupdf.readthedocs.io/en/latest/_sources/tools.rst Demonstrates how to interact with PyMuPDF's cache using `store_maxsize`, `store_size`, and `store_shrink`. The example shows initial cache sizes, how they change after loading a document and creating a pixmap, and how to reduce cache usage. ```python >>> import pymupdf # print the maximum and current cache sizes >>> pymupdf.TOOLS.store_maxsize 268435456 >>> pymupdf.TOOLS.store_size 0 >>> doc = pymupdf.open("demo1.pdf") # pixmap creation puts lots of object in cache (text, images, fonts), # apart from the pixmap itself >>> pix = doc[0].get_pixmap(alpha=False) >>> pymupdf.TOOLS.store_size 454519 # release (at least) 50% of the storage >>> pymupdf.TOOLS.store_shrink(50) 13471 >>> pymupdf.TOOLS.store_size 13471 # close document and see how how much cache is still in use >>> doc.close() >>> pymupdf.TOOLS.store_size 0 ``` -------------------------------- ### Install PyMuPDF using pip Source: https://pymupdf.readthedocs.io/en/latest/_sources/installation.rst The standard command to install or upgrade the PyMuPDF package using pip. This will install from a wheel if available for the platform. ```bash pip install --upgrade pymupdf ``` -------------------------------- ### Install Tesseract Language Packs on Arch Linux Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Use `pacman -Ss` to search for available language packs and `pacman -S` to install a specific language (e.g., German: `tesseract-data-deu`). ```bash # Search for available language packs pacman -Ss tesseract-data # Install a specific language (e.g. German) sudo pacman -S tesseract-data-deu ``` -------------------------------- ### Build and Install PyMuPDF with Local MuPDF Source Source: https://pymupdf.readthedocs.io/en/latest/installation.html Install PyMuPDF using a local MuPDF source tree by setting the PYMUPDF_SETUP_MUPDF_BUILD environment variable. ```bash cd PyMuPDF && PYMUPDF_SETUP_MUPDF_BUILD=../mupdf pip install . ``` -------------------------------- ### Verify Tesseract Language Installation (Windows) Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Run this command in Command Prompt or PowerShell to list installed Tesseract languages and verify new installations. ```bash tesseract --list-langs ``` -------------------------------- ### Install All Tesseract Languages (macOS Homebrew) Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Use this Homebrew command to install Tesseract with all available language packs, which uses more disk space. ```bash brew install tesseract-lang ``` -------------------------------- ### Build and Install PyMuPDF (Default MuPDF) Source: https://pymupdf.readthedocs.io/en/latest/installation.html Navigate to the PyMuPDF source directory and install using pip. This method automatically downloads and builds the default MuPDF version. ```bash cd PyMuPDF && pip install . ``` -------------------------------- ### Install Tesseract Engine Only (macOS Homebrew) Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Install only the Tesseract OCR engine using Homebrew. Language packs must be downloaded separately. ```bash # Install Tesseract engine only brew install tesseract ``` -------------------------------- ### Example Session with PyMuPDF Tools Source: https://pymupdf.readthedocs.io/en/latest/tools.html Demonstrates how to interact with PyMuPDF's tools for cache management and ID generation. Shows initial cache sizes, how they change after opening a document and creating a pixmap, shrinking the cache, generating unique IDs, and observing cache size after closing the document. ```python >>> import pymupdf # print the maximum and current cache sizes >>> pymupdf.TOOLS.store_maxsize 268435456 >>> pymupdf.TOOLS.store_size 0 >>> doc = pymupdf.open("demo1.pdf") # pixmap creation puts lots of object in cache (text, images, fonts), # apart from the pixmap itself >>> pix = doc[0].get_pixmap(alpha=False) >>> pymupdf.TOOLS.store_size 454519 # release (at least) 50% of the storage >>> pymupdf.TOOLS.store_shrink(50) 13471 >>> pymupdf.TOOLS.store_size 13471 # get a few unique numbers >>> pymupdf.TOOLS.gen_id() 1 >>> pymupdf.TOOLS.gen_id() 2 >>> pymupdf.TOOLS.gen_id() 3 # close document and see how much cache is still in use >>> doc.close() >>> pymupdf.TOOLS.store_size 0 >>> ``` -------------------------------- ### Custom Page Labeling Example Source: https://pymupdf.readthedocs.io/en/latest/document.html Defines rules for generating custom page labels based on page number, prefix, style, and starting page number. This example shows how to create sequential labels with prefixes and reset numbering. ```python [{'startpage': 6, 'prefix': 'A-', 'style': 'D', 'firstpagenum': 10}, {'startpage': 10, 'prefix': '', 'style': 'D', 'firstpagenum': 1}] ``` -------------------------------- ### Document Viewer UI Setup Source: https://pymupdf.readthedocs.io/en/latest/recipes-multiprocessing.html Sets up the main user interface for the document viewer, including buttons for opening, playing, and stopping the document, as well as labels for displaying page information and the document content. ```Python def initUI(self): vbox = QtWidgets.QVBoxLayout() self.setLayout(vbox) hbox = QtWidgets.QHBoxLayout() self.btnOpen = QtWidgets.QPushButton("OpenDocument", self) self.btnOpen.clicked.connect(self.openDoc) hbox.addWidget(self.btnOpen) self.btnPlay = QtWidgets.QPushButton("PlayDocument", self) self.btnPlay.clicked.connect(self.playDoc) hbox.addWidget(self.btnPlay) self.btnStop = QtWidgets.QPushButton("Stop", self) self.btnStop.clicked.connect(self.stopPlay) hbox.addWidget(self.btnStop) self.label = QtWidgets.QLabel("0/0", self) self.label.setFont(QtGui.QFont("Verdana", 20)) hbox.addWidget(self.label) vbox.addLayout(hbox) self.labelImg = QtWidgets.QLabel("Document", self) sizePolicy = QtWidgets.QSizePolicy( QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Expanding ) self.labelImg.setSizePolicy(sizePolicy) vbox.addWidget(self.labelImg) self.setGeometry(100, 100, 400, 600) self.setWindowTitle("PyMuPDF Document Player") self.show() ``` -------------------------------- ### Initialize PyMuPDF Pro with a Trial Key Source: https://pymupdf.readthedocs.io/en/latest/_sources/pymupdf-pro/index.rst Initialize PyMuPDF Pro using a trial key to unlock full functionality for evaluation. This allows testing the product's capabilities beyond the restricted trial version. ```python import pymupdf.pro pymupdf.pro.unlock(my_key) # PyMuPDF has now been extended with PyMuPDF Pro features. ``` -------------------------------- ### Image Profile Example Source: https://pymupdf.readthedocs.io/en/latest/functions.html Demonstrates how to get properties of an image using image_profile. This function can process image data from memory or files and returns image details like dimensions, orientation, and color space. It's useful for analyzing images without external libraries. ```python pymupdf.image_profile(open("nur-ruhig.jpg", "rb").read()) ``` -------------------------------- ### Colorspace Constructor Example Source: https://pymupdf.readthedocs.io/en/latest/colorspace.html Demonstrates how to create a Colorspace object using predefined integer constants. Use this to initialize a colorspace for a Pixmap. ```python cs = pymupdf.Colorspace(pymupdf.CS_RGB) ``` -------------------------------- ### Create Mutable Identity Matrix in PyMuPDF Source: https://pymupdf.readthedocs.io/en/latest/_sources/identity.rst Demonstrates how to create a mutable Identity matrix in PyMuPDF. This is useful when you need a starting point for transformations. The examples show creating a matrix by specifying values, using scaling, rotation, or by copying the immutable Identity matrix. ```python >>> m = pymupdf.Matrix(1, 0, 0, 1, 0, 0) # specify the values >>> m = pymupdf.Matrix(1, 1) # use scaling by factor 1 >>> m = pymupdf.Matrix(0) # use rotation by zero degrees >>> m = pymupdf.Matrix(pymupdf.Identity) # make a copy of Identity ``` -------------------------------- ### Perform OCR and Save as PDF with PyMuPDF Source: https://pymupdf.readthedocs.io/en/latest/_sources/pixmap.rst Recognizes text in an image using Tesseract and saves it as a 1-page PDF with an OCR text layer. Supports compression, language selection, and Tesseract data path configuration. Requires Tesseract installation and proper environment setup. ```python import pymupdf import os # Example usage: # doc = pymupdf.open() # for imgfile in os.listdir(folder): # pix = pymupdf.Pixmap(imgfile) # imgpdf = pymupdf.open("pdf", pix.pdfocr_tobytes()) # doc.insert_pdf(imgpdf) # pix = None # imgpdf.close() # doc.save("ocr-images.pdf") ``` -------------------------------- ### Database Setup and Data Insertion Source: https://pymupdf.readthedocs.io/en/latest/recipes-stories.html Initializes an in-memory SQLite database, creates a 'capitals' table, and inserts data from a string into the table. This prepares data for HTML table generation. ```python dbfilename = ":memory:" # the SQLITE database file name database = sqlite3.connect(dbfilename) # open database cursor = database.cursor() # multi-purpose database cursor # Define and fill the SQLITE database cursor.execute( """CREATE TABLE capitals (Country text, Capital text, Population text, Percent text, Year text)""" ) for value in table_data.splitlines(): cursor.execute("INSERT INTO capitals VALUES (?,?,?,?,?)", value.split(";")) # select statement for the rows - let SQL also sort it for us select = "SELECT * FROM capitals ORDER BY \"Country\" " ``` -------------------------------- ### Insert Text Lines into PDF using PyMuPDF Source: https://pymupdf.readthedocs.io/en/latest/_sources/recipes-text.rst This example demonstrates how to insert text lines onto a new or existing PDF page using PyMuPDF. It shows how to specify the starting point for the text and provides flexibility by allowing text to be represented as a single string with newline characters or as a list of strings. ```python import pymupdf doc = pymupdf.open(...) # new or existing PDF page = doc.new_page() # new or existing page via doc[n] p = pymupdf.Point(50, 72) # start point of 1st line text = "Some text,\nspread across\nseveral lines." # the same result is achievable by # text = ["Some text", "spread across", "several lines."] # Example of inserting the text (requires page object and point) # page.insert_text(p, text) ``` -------------------------------- ### Initialize Archive from File with Path Source: https://pymupdf.readthedocs.io/en/latest/_sources/archive-class.rst Creates a new archive from a local file, treating its binary content as a single-member archive. The 'path' parameter is mandatory and specifies the member name. ```python archive = fitz.Archive(content=b"binary data", path="member_name") ``` -------------------------------- ### Initialize Archive from Folder Source: https://pymupdf.readthedocs.io/en/latest/_sources/archive-class.rst Creates a new archive by converting a local folder. The folder's contents will be accessible as sub-members. ```python archive = fitz.Archive(content="/path/to/folder") ``` -------------------------------- ### PyMuPDF Journaling Example Session Source: https://pymupdf.readthedocs.io/en/latest/recipes-journalling.html This snippet demonstrates enabling journaling, performing operations like adding pages and text, and then using undo/redo to navigate through these changes. It also shows how to check the journal's current position and capabilities. ```python >>> import pymupdf >>> doc=pymupdf.open() >>> doc.journal_enable() >>> # try update without an operation: >>> page = doc.new_page() mupdf: No journalling operation started ... omitted lines RuntimeError: No journalling operation started >>> doc.journal_start_op("op1") >>> page = doc.new_page() >>> doc.journal_stop_op() >>> doc.journal_start_op("op2") >>> page.insert_text((100,100), "Line 1") >>> doc.journal_stop_op() >>> doc.journal_start_op("op3") >>> page.insert_text((100,120), "Line 2") >>> doc.journal_stop_op() >>> doc.journal_start_op("op4") >>> page.insert_text((100,140), "Line 3") >>> doc.journal_stop_op() >>> # show position in journal >>> doc.journal_position() (4, 4) >>> # 4 operations recorded - positioned at bottom >>> # what can we do? >>> doc.journal_can_do() {'undo': True, 'redo': False} >>> # currently only undos are possible. Print page content: >>> print(page.get_text()) Line 1 Line 2 Line 3 >>> # undo last insert: >>> doc.journal_undo() >>> # show combined status again: >>> doc.journal_position();doc.journal_can_do() (3, 4) {'undo': True, 'redo': True} >>> print(page.get_text()) Line 1 Line 2 >>> # our position is now second to last >>> # last text insertion was reverted >>> # but we can redo / move forward as well: >>> doc.journal_redo() >>> # our combined status: >>> doc.journal_position();doc.journal_can_do() (4, 4) {'undo': True, 'redo': False} >>> print(page.get_text()) Line 1 Line 2 Line 3 >>> # line 3 has appeared again! ``` -------------------------------- ### Install PyMuPDF Package Source: https://pymupdf.readthedocs.io/en/latest/_sources/faq/index.rst This command installs the PyMuPDF library using pip. Ensure you are installing 'pymupdf' and not similar-sounding packages like 'pymypdf' or 'mupdf'. ```bash pip install pymupdf ``` -------------------------------- ### PyMuPDF Rect Initialization Examples Source: https://pymupdf.readthedocs.io/en/latest/_sources/rect.rst Demonstrates various ways to initialize a PyMuPDF Rect object, including using coordinates, other Rect objects, or sequences. ```python import pymupdf # Initialize with coordinates rect1 = pymupdf.Rect(0, 0, 100, 200) # Initialize with top-left and bottom-right points point1 = (10, 10) point2 = (50, 60) rect2 = pymupdf.Rect(point1, point2) # Initialize with another Rect object rect3 = pymupdf.Rect(rect1) # Initialize with a sequence of numbers rect4 = pymupdf.Rect([0, 0, 100, 200]) # Initialize an empty rectangle empty_rect = pymupdf.Rect() # Initialize an infinite rectangle infinite_rect = pymupdf.Rect(-float('inf'), -float('inf'), float('inf'), float('inf')) ``` -------------------------------- ### Install PyMuPDF for Specific Python Version (Windows) Source: https://pymupdf.readthedocs.io/en/latest/installation.html On Windows, use 'py - -m pip install .' to install PyMuPDF for a particular Python version from the source tree. ```bash cd PyMuPDF && py -3.10 -m pip install . ``` -------------------------------- ### Opening a Document Source: https://pymupdf.readthedocs.io/en/latest/_sources/tutorial.rst Shows how to initialize a Document object from a file path. The filename can be a string or a pathlib.Path object. ```python doc = pymupdf.open(filename) # or pymupdf.Document(filename) ``` -------------------------------- ### Install Tesseract Language Packs on Ubuntu Source: https://pymupdf.readthedocs.io/en/latest/pymupdf4llm/index.html This command installs the German and French language data packs for the Tesseract OCR engine on Ubuntu systems. Ensure Tesseract is installed first. ```bash sudo apt install tesseract-ocr-deu tesseract-ocr-fra ``` -------------------------------- ### Get Link URI Source: https://pymupdf.readthedocs.io/en/latest/link.html Access the uri property to get the destination URI of the link. ```python uri = link.uri ``` -------------------------------- ### Initialize Archive with Path as Mount Point Source: https://pymupdf.readthedocs.io/en/latest/_sources/archive-class.rst Creates an archive with a specified 'path' parameter, which acts as a mount point or folder name for the sub-archive's elements. ```python archive = fitz.Archive(content=(b"data", "name"), path="mount/point") ``` -------------------------------- ### Install PyMuPDF for Specific Python Version (Windows, 32-bit) Source: https://pymupdf.readthedocs.io/en/latest/installation.html On Windows, use 'py --32 -m pip install .' to install a 32-bit PyMuPDF for a particular Python version from the source tree. ```bash cd PyMuPDF && py -3.10-32 -m pip install . ``` -------------------------------- ### Setting and Inspecting CropBox Source: https://pymupdf.readthedocs.io/en/latest/_sources/page.rst Demonstrates how to set the crop box of a page and observe the effect on the page's rectangle. Also shows how mediabox remains unaffected. ```python page = doc.new_page() page.rect # pymupdf.Rect(0.0, 0.0, 595.0, 842.0) page.cropbox # cropbox and mediabox still equal # pymupdf.Rect(0.0, 0.0, 595.0, 842.0) # now set cropbox to a part of the page page.set_cropbox(pymupdf.Rect(100, 100, 400, 400)) # this will also change the "rect" property: page.rect # pymupdf.Rect(0.0, 0.0, 300.0, 300.0) # but mediabox remains unaffected page.mediabox # pymupdf.Rect(0.0, 0.0, 595.0, 842.0) # revert CropBox change # either set it to MediaBox page.set_cropbox(page.mediabox) # or 'refresh' MediaBox: will remove all other rectangles page.set_mediabox(page.mediabox) ``` -------------------------------- ### Get Next Link Source: https://pymupdf.readthedocs.io/en/latest/link.html Access the next property to get the next link in the chain of links on the page. ```python next_link = link.next ``` -------------------------------- ### Get Link Destination Details Source: https://pymupdf.readthedocs.io/en/latest/link.html Access the dest property to get details about the link's destination. ```python dest = link.dest ``` -------------------------------- ### Importing PyMuPDF and Checking Version Source: https://pymupdf.readthedocs.io/en/latest/_sources/tutorial.rst Demonstrates how to import the pymupdf library and display version information using the docstring. ```python import pymupdf print(pymupdf.__doc__) ``` -------------------------------- ### Output Database Content with Story Templates Source: https://pymupdf.readthedocs.io/en/latest/_sources/recipes-stories.rst Demonstrates reporting SQL database content using an HTML template with the Story class. Requires a pre-existing SQL database file. ```python import pathlib import pymupdf # This script requires the filmfestival-sql.db database file. # It demonstrates how to report SQL database content using an HTML template. # Create a Story object from an HTML template and a CSS file. # The HTML template should reference the database content. # Example usage: # story = pymupdf.Story(html=template_html, user_css=template_css) # story.run() # story.save("output.pdf") # The actual script 'filmfestival-sql.py' would contain the logic to # query the database and populate the HTML template. # Placeholder for the actual script content as it's too long to include here. # The provided snippet in the source is a literal include of the python file. # For brevity, a conceptual representation is used here. print("This is a placeholder for the filmfestival-sql.py script.") print("The actual script queries a SQL database and uses an HTML template to generate a PDF.") ``` -------------------------------- ### Get Link Clickable Area Source: https://pymupdf.readthedocs.io/en/latest/link.html Access the rect property to get the clickable area of the link in untransformed coordinates. ```python rect = link.rect ``` -------------------------------- ### Get Link XREF Number Source: https://pymupdf.readthedocs.io/en/latest/link.html Access the xref property to get the xref number of the link's entry in the PDF. ```python xref = link.xref ``` -------------------------------- ### Creating Qt Image from Pixmap Pointer Source: https://pymupdf.readthedocs.io/en/latest/pixmap.html Demonstrates how to create a Qt QImage directly from a Pixmap's memory pointer for significant performance gains by avoiding data copying. ```python img = QtGui.QImage(pix.samples, pix.width, pix.height, format) # (1) img = QtGui.QImage(pix.samples_ptr, pix.width, pix.height, format) # (2) ``` -------------------------------- ### Manually Install Tesseract Language Pack Source: https://pymupdf.readthedocs.io/en/latest/ocr/tesseract-language-packs.html Download the `.traineddata` file using `curl` and copy it to the Tesseract tessdata directory using `sudo cp`. This method is useful if a language pack is not available through your distribution's package manager. ```bash # Download language pack (e.g. French) curl -L https://github.com/tesseract-ocr/tessdata/raw/main/fra.traineddata \ -o fra.traineddata # Copy to tessdata directory (path varies by distro) sudo cp fra.traineddata /usr/share/tesseract-ocr/4.00/tessdata/ # or sudo cp fra.traineddata /usr/share/tessdata/ ``` -------------------------------- ### Ligature Mapping Example Source: https://pymupdf.readthedocs.io/en/latest/functions.html This example shows a mapping of common ligatures to their corresponding Unicode values, useful for text processing. ```python "ff" -> 0xFB00 "fi" -> 0xFB01 "fl" -> 0xFB02 "ffi" -> 0xFB03 "ffl" -> 0xFB04 "ft" -> 0xFB05 "st" -> 0xFB06 ``` -------------------------------- ### Extract and Analyze Image Transformation Matrix Source: https://pymupdf.readthedocs.io/en/latest/app3.html This example shows how to retrieve an image's bounding box and its transformation matrix from a page. It then uses these to verify the bbox calculation and inspect the combined shrink and transformation matrix to understand image rotation and scaling. ```python >>> imginfo = page.get_images()[0] # get an image item on a page >>> imginfo (5, 0, 439, 501, 8, 'DeviceRGB', '', 'fzImg0', 'DCTDecode') >>> #------------------------------------------------ >>> # define image shrink matrix and rectangle >>> #------------------------------------------------ >>> shrink = pymupdf.Matrix(1 / 439, 0, 0, 1 / 501, 0, 0) >>> imgrect = pymupdf.Rect(0, 0, 439, 501) >>> #------------------------------------------------ >>> # determine image bbox and transformation matrix: >>> #------------------------------------------------ >>> bbox, transform = page.get_image_bbox("fzImg0", transform=True) >>> #------------------------------------------------ >>> # confirm equality - permitting rounding errors >>> #------------------------------------------------ >>> bbox Rect(100.0, 112.37525939941406, 300.0, 287.624755859375) >>> imgrect * shrink * transform Rect(100.0, 112.375244140625, 300.0, 287.6247253417969) >>> #------------------------------------------------ >>> shrink * transform Matrix(0.0, -0.39920157194137573, 0.3992016017436981, 0.0, 100.0, 287.6247253417969) >>> #------------------------------------------------ >>> # the above shows: >>> # image sides are scaled by same factor ~0.4, >>> # and the image is rotated by 90 degrees clockwise >>> # compare this with pymupdf.Matrix(-90) * 0.4 >>> #------------------------------------------------ ``` -------------------------------- ### DisplayList.__init__ Source: https://pymupdf.readthedocs.io/en/latest/displaylist.html Creates a new DisplayList instance. It requires a mediabox (Rect) representing the page's rectangle. ```APIDOC ## DisplayList.__init__ ### Description Creates a new display list. ### Parameters * **mediabox** (Rect) – The page’s rectangle. ### Return type `DisplayList` ``` -------------------------------- ### Verify PyMuPDF Installation Source: https://pymupdf.readthedocs.io/en/latest/_sources/faq/index.rst This Python command verifies if PyMuPDF is installed correctly and accessible in the current Python environment. It's useful for troubleshooting import errors. ```python python -c "import pymupdf; print(pymupdf.__doc__)" ``` -------------------------------- ### Advanced Multiprocessing with GUI and IPC Source: https://pymupdf.readthedocs.io/en/latest/recipes-multiprocessing.html This example demonstrates a more advanced use of multiprocessing, involving a main process with a GUI (Qt) and a child process that accesses a PyMuPDF document. It highlights establishing two-way communication between these processes. ```Python """ Created on 2019-05-01 @author: yinkaisheng@live.com @copyright: 2019 yinkaisheng@live.com @license: GNU AFFERO GPL 3.0 Demonstrate the use of multiprocessing with PyMuPDF ----------------------------------------------------- This example shows some more advanced use of multiprocessing. The main process show a Qt GUI and establishes a 2-way communication with another process, which accesses a supported document. """ import os import sys import time import multiprocessing as mp import queue import pymupdf ''' PyQt and PySide namespace unifier shim https://www.pythonguis.com/faq/pyqt6-vs-pyside6/ simple "if 'PyQt6' in sys.modules:" test fails for me, so the more complex pkgutil use overkill for most people who might have one or the other, why both? ''' from pkgutil import iter_modules def module_exists(module_name): return module_name in (name for loader, name, ispkg in iter_modules()) ``` -------------------------------- ### Get Pixmap with Custom Matrix Source: https://pymupdf.readthedocs.io/en/latest/_sources/page.rst Demonstrates how to get a Pixmap of a page with a custom matrix for controlling resolution and transformations. This is useful for scaling and rotating the output image. ```python import pymupdf doc=pymupdf.open("demo1.pdf") page=doc[0] rotation = page.rotation cropbox = page.cropbox page.set_cropbox(page.mediabox) page.set_rotation(0) pix = page.get_pixmap() page.set_cropbox(cropbox) if rotation != 0: page.set_rotation(rotation) ```