|
87 | 87 |
|
88 | 88 | import playa |
89 | 89 | from playa import Document, Page, PDFPasswordIncorrect, asobj |
| 90 | +from playa.color import ColorSpace |
90 | 91 | from playa.data.content import Image |
91 | 92 | from playa.data.metadata import asobj_document |
92 | 93 | from playa.outline import Outline |
@@ -517,30 +518,50 @@ def get_one_image(stream: ContentStream, path: Path) -> Path: |
517 | 518 | else: |
518 | 519 | # Otherwise, try to write a PNM file |
519 | 520 | bits = stream.bits |
520 | | - colorspace = stream.colorspace |
521 | | - ncomponents = colorspace.ncomponents |
522 | | - if colorspace.name == "Indexed": |
523 | | - from playa.color import get_colorspace |
524 | | - |
525 | | - assert isinstance(colorspace.spec, list) |
526 | | - _, underlying, _, _ = colorspace.spec |
527 | | - underlying = get_colorspace(resolve1(underlying)) |
528 | | - if underlying is not None: |
529 | | - ncomponents = underlying.ncomponents |
| 521 | + colorspace: Union[ColorSpace, None] = stream.colorspace |
| 522 | + ncomponents = 1 |
| 523 | + if colorspace is not None: |
| 524 | + ncomponents = colorspace.ncomponents |
| 525 | + if colorspace.name == "Indexed": |
| 526 | + from playa.color import get_colorspace |
| 527 | + |
| 528 | + assert isinstance(colorspace.spec, list) |
| 529 | + _, underlying, _, _ = colorspace.spec |
| 530 | + colorspace = get_colorspace(resolve1(underlying)) |
| 531 | + if colorspace is not None: |
| 532 | + ncomponents = colorspace.ncomponents |
530 | 533 | if bits == 1: |
531 | 534 | path = path.with_suffix(".pbm") |
| 535 | + elif colorspace is None or colorspace.name not in ("DeviceGray", "DeviceRGB"): |
| 536 | + path = path.with_suffix(".dat") |
| 537 | + LOG.warning( |
| 538 | + "Unsupported colorspace %s, writing data to %s", asobj(colorspace), path |
| 539 | + ) |
532 | 540 | elif ncomponents == 1: |
533 | 541 | path = path.with_suffix(".pgm") |
534 | 542 | elif ncomponents == 3: |
535 | 543 | path = path.with_suffix(".ppm") |
536 | | - try: |
| 544 | + elif ncomponents == 3: |
| 545 | + path = path.with_suffix(".ppm") |
| 546 | + else: |
| 547 | + path = path.with_suffix(".dat") |
| 548 | + LOG.warning( |
| 549 | + "Unsupported colorspace %s, writing data to %s", asobj(colorspace), path |
| 550 | + ) |
| 551 | + |
| 552 | + if path.suffix != ".dat": |
| 553 | + try: |
| 554 | + with open(path, "wb") as outfh: |
| 555 | + stream.write_pnm(outfh) |
| 556 | + except ValueError: |
| 557 | + datpath = path.with_suffix(".dat") |
| 558 | + LOG.exception( |
| 559 | + "Failed to write PNM to %s, writing data to %s", path, datpath |
| 560 | + ) |
| 561 | + path = datpath |
| 562 | + |
| 563 | + if path.suffix == ".dat": |
537 | 564 | with open(path, "wb") as outfh: |
538 | | - stream.write_pnm(outfh) |
539 | | - except ValueError: |
540 | | - # Fall back to a binary file |
541 | | - datpath = path.with_suffix(".dat") |
542 | | - LOG.warning("Failed to write PNM to %s, writing data to %s", path, datpath) |
543 | | - with open(datpath, "wb") as outfh: |
544 | 565 | outfh.write(stream.buffer) |
545 | 566 | return path |
546 | 567 |
|
|
0 commit comments