Skip to content

Commit 35f23fb

Browse files
authored
Set correct size for rotated PCD images after opening (#9086)
2 parents 801f7ad + 7328cf2 commit 35f23fb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Tests/test_file_pcd.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from __future__ import annotations
22

3+
from io import BytesIO
4+
5+
import pytest
6+
37
from PIL import Image
48

59

610
def test_load_raw() -> None:
711
with Image.open("Tests/images/hopper.pcd") as im:
12+
assert im.size == (768, 512)
813
im.load() # should not segfault.
914

1015
# Note that this image was created with a resized hopper
@@ -15,3 +20,13 @@ def test_load_raw() -> None:
1520

1621
# target = hopper().resize((768,512))
1722
# assert_image_similar(im, target, 10)
23+
24+
25+
@pytest.mark.parametrize("orientation", (1, 3))
26+
def test_rotated(orientation: int) -> None:
27+
with open("Tests/images/hopper.pcd", "rb") as fp:
28+
data = bytearray(fp.read())
29+
data[2048 + 1538] = orientation
30+
f = BytesIO(data)
31+
with Image.open(f) as im:
32+
assert im.size == (512, 768)

src/PIL/PcdImagePlugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _open(self) -> None:
3232
assert self.fp is not None
3333

3434
self.fp.seek(2048)
35-
s = self.fp.read(2048)
35+
s = self.fp.read(1539)
3636

3737
if not s.startswith(b"PCD_"):
3838
msg = "not a PCD file"
@@ -46,14 +46,13 @@ def _open(self) -> None:
4646
self.tile_post_rotate = -90
4747

4848
self._mode = "RGB"
49-
self._size = 768, 512 # FIXME: not correct for rotated images!
49+
self._size = (512, 768) if orientation in (1, 3) else (768, 512)
5050
self.tile = [ImageFile._Tile("pcd", (0, 0) + self.size, 96 * 2048)]
5151

5252
def load_end(self) -> None:
5353
if self.tile_post_rotate:
5454
# Handle rotated PCDs
5555
self.im = self.im.rotate(self.tile_post_rotate)
56-
self._size = self.im.size
5756

5857

5958
#

0 commit comments

Comments
 (0)