Skip to content
Merged
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
17 changes: 17 additions & 0 deletions Tests/test_file_pcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^

Expand Down
6 changes: 6 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 3 additions & 3 deletions src/PIL/PcxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
Expand Down
Loading