Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,11 @@ def test_dump(self, tmp_path: Path) -> None:
im = Image.new("RGB", (10, 10))
im._dump(str(tmp_path / "temp_RGB.ppm"))

im = Image.new("RGBA", (10, 10))
im._dump(str(tmp_path / "temp_RGBA.ppm"))

im = Image.new("HSV", (10, 10))
with pytest.raises(ValueError):
with pytest.raises(OSError):
im._dump(str(tmp_path / "temp_HSV.ppm"))

def test_comparison_with_other_type(self) -> None:
Expand Down
21 changes: 7 additions & 14 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,24 +731,17 @@ def _ensure_mutable(self) -> None:
def _dump(
self, file: str | None = None, format: str | None = None, **options: Any
) -> str:
suffix = ""
if format:
suffix = f".{format}"
suffix = f".{format}" if format else ""

if not file:
f, filename = tempfile.mkstemp(suffix)
os.close(f)
else:
if file:
filename = file
if not filename.endswith(suffix):
filename = filename + suffix

self.load()

if not format or format == "PPM":
self.im.save_ppm(filename)
filename += suffix
else:
self.save(filename, format, **options)
f, filename = tempfile.mkstemp(suffix)
os.close(f)

self.save(filename, format or "PPM", **options)

return filename

Expand Down
30 changes: 4 additions & 26 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))

/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
/* OBJECT ADMINISTRATION */
/* -------------------------------------------------------------------- */

typedef struct {
Expand Down Expand Up @@ -379,7 +379,7 @@ ImagingError_ValueError(const char *message) {
}

/* -------------------------------------------------------------------- */
/* HELPERS */
/* HELPERS */
/* -------------------------------------------------------------------- */

static int
Expand Down Expand Up @@ -683,7 +683,7 @@ getink(PyObject *color, Imaging im, char *ink) {
}

/* -------------------------------------------------------------------- */
/* FACTORIES */
/* FACTORIES */
/* -------------------------------------------------------------------- */

static PyObject *
Expand Down Expand Up @@ -3611,7 +3611,7 @@ _effect_spread(ImagingObject *self, PyObject *args) {
}

/* -------------------------------------------------------------------- */
/* UTILITIES */
/* UTILITIES */
/* -------------------------------------------------------------------- */

static PyObject *
Expand Down Expand Up @@ -3646,25 +3646,6 @@ _getcodecstatus(PyObject *self, PyObject *args) {
return PyUnicode_FromString(msg);
}

/* -------------------------------------------------------------------- */
/* DEBUGGING HELPERS */
/* -------------------------------------------------------------------- */

static PyObject *
_save_ppm(ImagingObject *self, PyObject *args) {
char *filename;

if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL;
}

if (!ImagingSavePPM(self->image, filename)) {
return NULL;
}

Py_RETURN_NONE;
}

/* -------------------------------------------------------------------- */

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

/* Misc. */
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},

/* arrow */
{"__arrow_c_schema__", (PyCFunction)ExportArrowSchemaPyCapsule, METH_VARARGS},
{"__arrow_c_array__", (PyCFunction)ExportArrowArrayPyCapsule, METH_VARARGS},
Expand Down
34 changes: 0 additions & 34 deletions src/libImaging/File.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,3 @@ ImagingSaveRaw(Imaging im, FILE *fp) {

return 1;
}

int
ImagingSavePPM(Imaging im, const char *outfile) {
FILE *fp;

if (!im) {
(void)ImagingError_ValueError(NULL);
return 0;
}

fp = fopen(outfile, "wb");
if (!fp) {
PyErr_SetString(PyExc_OSError, "error when accessing file");
return 0;
}

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();
return 0;
}

ImagingSaveRaw(im, fp);

fclose(fp);

return 1;
}
Loading