In the documentation of pypdf I can find the following about reducing image quality:
from pypdf import PdfReader, PdfWriter
reader = PdfReader("example.pdf")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
for page in writer.pages:
for img in page.images:
img.replace(img.image, quality=80)
with open("out.pdf", "wb") as f:
writer.write(f)
However, with respect to img.replace(img.image, quality=80)
I cannot find what the default quality setting is. Is it percentage-wise, so it would be 100? That is to say, if I would use quality=100
it would replace the image with the same image? Or is it something else entirely?
I've tried using help(img.replace)
, help(img.image.save)
, and help(open(img.name, "wb").write)
but to no avail.
The reason I need this is because I want to know if it's relative or absolute, as I'd like for the user to give a "level" of compression.
Thanks to @Demetry Pascal's comment I was able to find the following information on the pillow docs:
Saving
The save() method supports the following options:
quality
The image quality, on a scale from 0 (worst) to 95 (best), or the string
keep
. The default is 75. Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality. The valuekeep
is only valid for JPEG files and will retain the original image quality level, subsampling, and qtables.
This is exactly what I needed to know, so thanks for pointing me in the right direction!