MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / rewrite_images

Method rewrite_images

src/__init__.py:6213–6298  ·  view source on GitHub ↗

Rewrite images in a PDF document. The typical use case is to reduce the size of the PDF by recompressing images. Default parameters will convert all images to JPEG where possible, using the specified resolutions and quality. Exclude undesired images by setting parame

(
        self,
        dpi_threshold=None,
        dpi_target=0,
        quality=0,
        lossy=True,
        lossless=True,
        bitonal=True,
        color=True,
        gray=True,
        set_to_gray=False,
        options=None,
    )

Source from the content-addressed store, hash-verified

6211 return pno, xp, yp
6212
6213 def rewrite_images(
6214 self,
6215 dpi_threshold=None,
6216 dpi_target=0,
6217 quality=0,
6218 lossy=True,
6219 lossless=True,
6220 bitonal=True,
6221 color=True,
6222 gray=True,
6223 set_to_gray=False,
6224 options=None,
6225 ):
6226 """Rewrite images in a PDF document.
6227
6228 The typical use case is to reduce the size of the PDF by recompressing
6229 images. Default parameters will convert all images to JPEG where
6230 possible, using the specified resolutions and quality. Exclude
6231 undesired images by setting parameters to False.
6232 Args:
6233 dpi_threshold: look at images with a larger DPI only.
6234 dpi_target: change eligible images to this DPI.
6235 quality: Quality of the recompressed images (0-100).
6236 lossy: process lossy image types (e.g. JPEG).
6237 lossless: process lossless image types (e.g. PNG).
6238 bitonal: process black-and-white images (e.g. FAX)
6239 color: process colored images.
6240 gray: process gray images.
6241 set_to_gray: whether to change the PDF to gray at process start.
6242 options: (PdfImageRewriterOptions) Custom options for image
6243 rewriting (optional). Expert use only. If provided, other
6244 parameters are ignored, except set_to_gray.
6245 """
6246 quality_str = str(quality)
6247 if not dpi_threshold:
6248 dpi_threshold = dpi_target = 0
6249 if dpi_target > 0 and dpi_target >= dpi_threshold:
6250 raise ValueError("{dpi_target=} must be less than {dpi_threshold=}")
6251 template_opts = mupdf.PdfImageRewriterOptions()
6252 dir1 = set(dir(template_opts)) # for checking that only existing options are set
6253 if not options:
6254 opts = mupdf.PdfImageRewriterOptions()
6255 if bitonal:
6256 opts.bitonal_image_recompress_method = mupdf.FZ_RECOMPRESS_FAX
6257 opts.bitonal_image_subsample_method = mupdf.FZ_SUBSAMPLE_AVERAGE
6258 opts.bitonal_image_subsample_to = dpi_target
6259 opts.bitonal_image_recompress_quality = quality_str
6260 opts.bitonal_image_subsample_threshold = dpi_threshold
6261 if color:
6262 if lossless:
6263 opts.color_lossless_image_recompress_method = mupdf.FZ_RECOMPRESS_JPEG
6264 opts.color_lossless_image_subsample_method = mupdf.FZ_SUBSAMPLE_AVERAGE
6265 opts.color_lossless_image_subsample_to = dpi_target
6266 opts.color_lossless_image_subsample_threshold = dpi_threshold
6267 opts.color_lossless_image_recompress_quality = quality_str
6268 if lossy:
6269 opts.color_lossy_image_recompress_method = mupdf.FZ_RECOMPRESS_JPEG
6270 opts.color_lossy_image_subsample_method = mupdf.FZ_SUBSAMPLE_AVERAGE

Callers 1

test_rewrite_imagesFunction · 0.80

Calls 2

recolorMethod · 0.95
_as_pdf_documentFunction · 0.85

Tested by 1

test_rewrite_imagesFunction · 0.64