Skip to content

Commit 816f537

Browse files
committed
Use Python PPM save, instead of C
1 parent b893310 commit 816f537

4 files changed

Lines changed: 15 additions & 75 deletions

File tree

‎Tests/test_image.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ def test_dump(self, tmp_path: Path) -> None:
270270
im = Image.new("RGB", (10, 10))
271271
im._dump(str(tmp_path / "temp_RGB.ppm"))
272272

273+
im = Image.new("RGBA", (10, 10))
274+
im._dump(str(tmp_path / "temp_RGBA.ppm"))
275+
273276
im = Image.new("HSV", (10, 10))
274-
with pytest.raises(ValueError):
277+
with pytest.raises(OSError):
275278
im._dump(str(tmp_path / "temp_HSV.ppm"))
276279

277280
def test_comparison_with_other_type(self) -> None:

‎src/PIL/Image.py‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -731,24 +731,17 @@ def _ensure_mutable(self) -> None:
731731
def _dump(
732732
self, file: str | None = None, format: str | None = None, **options: Any
733733
) -> str:
734-
suffix = ""
735-
if format:
736-
suffix = f".{format}"
734+
suffix = f".{format}" if format else ""
737735

738-
if not file:
739-
f, filename = tempfile.mkstemp(suffix)
740-
os.close(f)
741-
else:
736+
if file:
742737
filename = file
743738
if not filename.endswith(suffix):
744-
filename = filename + suffix
745-
746-
self.load()
747-
748-
if not format or format == "PPM":
749-
self.im.save_ppm(filename)
739+
filename += suffix
750740
else:
751-
self.save(filename, format, **options)
741+
f, filename = tempfile.mkstemp(suffix)
742+
os.close(f)
743+
744+
self.save(filename, format or "PPM", **options)
752745

753746
return filename
754747

‎src/_imaging.c‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
109109

110110
/* -------------------------------------------------------------------- */
111-
/* OBJECT ADMINISTRATION */
111+
/* OBJECT ADMINISTRATION */
112112
/* -------------------------------------------------------------------- */
113113

114114
typedef struct {
@@ -379,7 +379,7 @@ ImagingError_ValueError(const char *message) {
379379
}
380380

381381
/* -------------------------------------------------------------------- */
382-
/* HELPERS */
382+
/* HELPERS */
383383
/* -------------------------------------------------------------------- */
384384

385385
static int
@@ -683,7 +683,7 @@ getink(PyObject *color, Imaging im, char *ink) {
683683
}
684684

685685
/* -------------------------------------------------------------------- */
686-
/* FACTORIES */
686+
/* FACTORIES */
687687
/* -------------------------------------------------------------------- */
688688

689689
static PyObject *
@@ -3611,7 +3611,7 @@ _effect_spread(ImagingObject *self, PyObject *args) {
36113611
}
36123612

36133613
/* -------------------------------------------------------------------- */
3614-
/* UTILITIES */
3614+
/* UTILITIES */
36153615
/* -------------------------------------------------------------------- */
36163616

36173617
static PyObject *
@@ -3646,25 +3646,6 @@ _getcodecstatus(PyObject *self, PyObject *args) {
36463646
return PyUnicode_FromString(msg);
36473647
}
36483648

3649-
/* -------------------------------------------------------------------- */
3650-
/* DEBUGGING HELPERS */
3651-
/* -------------------------------------------------------------------- */
3652-
3653-
static PyObject *
3654-
_save_ppm(ImagingObject *self, PyObject *args) {
3655-
char *filename;
3656-
3657-
if (!PyArg_ParseTuple(args, "s", &filename)) {
3658-
return NULL;
3659-
}
3660-
3661-
if (!ImagingSavePPM(self->image, filename)) {
3662-
return NULL;
3663-
}
3664-
3665-
Py_RETURN_NONE;
3666-
}
3667-
36683649
/* -------------------------------------------------------------------- */
36693650

36703651
/* methods */
@@ -3748,9 +3729,6 @@ static struct PyMethodDef methods[] = {
37483729
/* Special effects */
37493730
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
37503731

3751-
/* Misc. */
3752-
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},
3753-
37543732
/* arrow */
37553733
{"__arrow_c_schema__", (PyCFunction)ExportArrowSchemaPyCapsule, METH_VARARGS},
37563734
{"__arrow_c_array__", (PyCFunction)ExportArrowArrayPyCapsule, METH_VARARGS},

‎src/libImaging/File.c‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,3 @@ ImagingSaveRaw(Imaging im, FILE *fp) {
4141

4242
return 1;
4343
}
44-
45-
int
46-
ImagingSavePPM(Imaging im, const char *outfile) {
47-
FILE *fp;
48-
49-
if (!im) {
50-
(void)ImagingError_ValueError(NULL);
51-
return 0;
52-
}
53-
54-
fp = fopen(outfile, "wb");
55-
if (!fp) {
56-
PyErr_SetString(PyExc_OSError, "error when accessing file");
57-
return 0;
58-
}
59-
60-
if (im->mode == IMAGING_MODE_1 || im->mode == IMAGING_MODE_L) {
61-
/* Write "PGM" */
62-
fprintf(fp, "P5\n%d %d\n255\n", im->xsize, im->ysize);
63-
} else if (im->mode == IMAGING_MODE_RGB) {
64-
/* Write "PPM" */
65-
fprintf(fp, "P6\n%d %d\n255\n", im->xsize, im->ysize);
66-
} else {
67-
fclose(fp);
68-
(void)ImagingError_ModeError();
69-
return 0;
70-
}
71-
72-
ImagingSaveRaw(im, fp);
73-
74-
fclose(fp);
75-
76-
return 1;
77-
}

0 commit comments

Comments
 (0)