diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 1f2d8e18d67..4cae4971e11 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -40,6 +40,23 @@ def test_sanity(tmp_path: Path) -> None: im.save(f) +def test_dpi(tmp_path: Path) -> None: + f = tmp_path / "temp.pcx" + im = hopper() + + im.save(f) + with Image.open(f) as reloaded: + assert reloaded.info["dpi"] == (100, 100) + + im.save(f, dpi=(300, 150)) + with Image.open(f) as reloaded: + assert reloaded.info["dpi"] == (300, 150) + + im.save(f, dpi=(72.4, 95.6)) + with Image.open(f) as reloaded: + assert reloaded.info["dpi"] == (72, 96) + + @pytest.mark.parametrize("size", ((0, 1), (1, 0), (0, 0))) def test_save_zero(size: tuple[int, int]) -> None: b = io.BytesIO() diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 8ca82474b3e..adec6ea8776 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -888,6 +888,15 @@ The :py:func:`~PIL.Image.open` function sets the following **dpi** A tuple of ``(x_resolution, y_resolution)``, with inches as the resolution unit. +Saving +~~~~~~ + +The :py:meth:`~PIL.Image.Image.save` method can take the following keyword arguments: + +**dpi** + A tuple of ``(x_resolution, y_resolution)``, with inches as the resolution unit. + Values are rounded to the nearest integer. Defaults to ``(100, 100)``. + PNG ^^^ diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index ac68a41f922..b9f2f0f919e 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -75,6 +75,12 @@ skips 2, draws 3, skips 1, draws 2, and so on). When ``dash`` is set, ``width`` is ignored. +Saving PCX dpi +^^^^^^^^^^^^^^ + +PCX images can now be saved with a ``dpi`` argument. Previously, the resolution was +written as 100 dpi. + ImageOps sepia and neon ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index f62e75b3b77..aaa986f7bea 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -152,7 +152,7 @@ def _open(self) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: - if im.width == 0 or im.height == 0: + if 0 in im.size: msg = "Cannot write empty image as PCX" raise ValueError(msg) @@ -177,12 +177,12 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: stride, ) - # under windows, we could determine the current screen size with + # under Windows, we could determine the current screen size with # "Image.core.display_mode()[1]", but I think that's overkill... screen = im.size - dpi = 100, 100 + dpi = tuple(round(d) for d in im.encoderinfo.get("dpi", (100, 100))) # PCX header fp.write(