Skip to content

Commit

Permalink
DOC: Demonstrate image quality reduction (py-pdf#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Jun 21, 2023
1 parent 8938d98 commit ab42636
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/user/file-size.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ with open("out.pdf", "wb") as f:
writer.write(f)
```

## Reducing Image Quality

If we reduce the quality of the images within the PDF, we can **sometimes**
reduce the file size of the PDF overall. That depends on how well the reduced
quality image can be compressed.

```python
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)
```

## Lossless Compression

pypdf supports the FlateDecode filter which uses the zlib/deflate compression
Expand Down

0 comments on commit ab42636

Please sign in to comment.