Fix `ValueError` when extracting images, due to breaking changes in Pillow (#827)

* Fix #795

* Update CHANGELOG.md

Co-authored-by: Kunal Gehlot <kunal.g@360hvpl.com>
pull/829/head
Pieter Marsman 2022-11-05 16:44:15 +01:00 committed by GitHub
parent 769dbb6343
commit fa71062c35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -18,7 +18,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `TypeError` when getting default width of font ([#720](https://github.com/pdfminer/pdfminer.six/issues/720)) - `TypeError` when getting default width of font ([#720](https://github.com/pdfminer/pdfminer.six/issues/720))
- Installing typing-extensions on Python 3.6 and 3.7 ([#775](https://github.com/pdfminer/pdfminer.six/pull/775)) - Installing typing-extensions on Python 3.6 and 3.7 ([#775](https://github.com/pdfminer/pdfminer.six/pull/775))
- `TypeError` in cmapdb.py when parsing null characters ([#768](https://github.com/pdfminer/pdfminer.six/pull/768)) - `TypeError` in cmapdb.py when parsing null characters ([#768](https://github.com/pdfminer/pdfminer.six/pull/768))
- Color "convenience operators" now (per spec) also set color space ([#779](https://github.com/pdfminer/pdfminer.six/issues/779)) - Color "convenience operators" now (per spec) also set color space ([#794](https://github.com/pdfminer/pdfminer.six/pull/794))
- `ValueError` when extracting images, due to breaking changes in Pillow ([#827](https://github.com/pdfminer/pdfminer.six/pull/827))
### Deprecated ### Deprecated

View File

@ -225,20 +225,24 @@ class ImageWriter:
with open(path, "wb") as fp: with open(path, "wb") as fp:
try: try:
from PIL import Image # type: ignore[import] from PIL import Image # type: ignore[import]
from PIL import ImageOps
except ImportError: except ImportError:
raise ImportError(PIL_ERROR_MESSAGE) raise ImportError(PIL_ERROR_MESSAGE)
mode: Literal["1", "8", "RGB", "CMYK"] mode: Literal["1", "L", "RGB", "CMYK"]
if image.bits == 1: if image.bits == 1:
mode = "1" mode = "1"
elif image.bits == 8 and channels == 1: elif image.bits == 8 and channels == 1:
mode = "8" mode = "L"
elif image.bits == 8 and channels == 3: elif image.bits == 8 and channels == 3:
mode = "RGB" mode = "RGB"
elif image.bits == 8 and channels == 4: elif image.bits == 8 and channels == 4:
mode = "CMYK" mode = "CMYK"
img = Image.frombytes(mode, image.srcsize, image.stream.get_data(), "raw") img = Image.frombytes(mode, image.srcsize, image.stream.get_data(), "raw")
if mode == "L":
img = ImageOps.invert(img)
img.save(fp) img.save(fp)
return name return name