Skip to content

In _dump(), use Python PPM save, instead of C#9566

Open
radarhere wants to merge 1 commit intopython-pillow:mainfrom
radarhere:ppm
Open

In _dump(), use Python PPM save, instead of C#9566
radarhere wants to merge 1 commit intopython-pillow:mainfrom
radarhere:ppm

Conversation

@radarhere
Copy link
Copy Markdown
Member

from PIL import Image
im = Image.new("RGBA", (1, 1))
im._dump("out")

currently raises an error

Traceback (most recent call last):
  File "demo.py", line 3, in <module>
    im._dump("out")
  File "PIL/Image.py", line 749, in _dump
    self.im.save_ppm(filename)
ValueError: image has wrong mode

_dump uses the C level save_ppm() for this scenario, which doesn't support RGBA.

Pillow/src/PIL/Image.py

Lines 748 to 751 in b893310

if not format or format == "PPM":
self.im.save_ppm(filename)
else:
self.save(filename, format, **options)

if (im->mode == IMAGING_MODE_1 || im->mode == IMAGING_MODE_L) {
/* Write "PGM" */
fprintf(fp, "P5\n%d %d\n255\n", im->xsize, im->ysize);
} else if (im->mode == IMAGING_MODE_RGB) {
/* Write "PPM" */
fprintf(fp, "P6\n%d %d\n255\n", im->xsize, im->ysize);
} else {
fclose(fp);
(void)ImagingError_ModeError();

However, if we consistently use Python level save(), then it can be handled.

def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
if im.mode == "1":
rawmode, head = "1;I", b"P4"
elif im.mode == "L":
rawmode, head = "L", b"P5"
elif im.mode in ("I", "I;16"):
rawmode, head = "I;16B", b"P5"
elif im.mode in ("RGB", "RGBA"):
rawmode, head = "RGB", b"P6"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant