Skip to content

Commit 2fa5440

Browse files
authored
Merge pull request #3203 from radarhere/size
Changed Image size property to be read-only by default
2 parents 8344aec + 05f2169 commit 2fa5440

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+142
-62
lines changed

Tests/test_file_icns.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def test_sizes(self):
6464
self.assertEqual(im2.mode, 'RGBA')
6565
self.assertEqual(im2.size, (wr, hr))
6666

67+
# Check that we cannot load an incorrect size
68+
with self.assertRaises(ValueError):
69+
im.size = (1, 1)
70+
6771
def test_older_icon(self):
6872
# This icon was made with Icon Composer rather than iconutil; it still
6973
# uses PNG rather than JP2, however (since it was made on 10.9).

Tests/test_file_ico.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def test_save_to_bytes(self):
4747
self.assert_image_equal(reloaded,
4848
hopper().resize((32, 32), Image.LANCZOS))
4949

50+
def test_incorrect_size(self):
51+
im = Image.open(TEST_ICO_FILE)
52+
with self.assertRaises(ValueError):
53+
im.size = (1, 1)
54+
5055
def test_save_256x256(self):
5156
"""Issue #2264 https://github.com/python-pillow/Pillow/issues/2264"""
5257
# Arrange

Tests/test_file_tiff.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def test_set_legacy_api(self):
6969
self.assertEqual(str(e.exception),
7070
"Not allowing setting of legacy api")
7171

72+
def test_size(self):
73+
filename = "Tests/images/pil168.tif"
74+
im = Image.open(filename)
75+
76+
def set_size():
77+
im.size = (256, 256)
78+
self.assert_warning(DeprecationWarning, set_size)
79+
7280
def test_xyres_tiff(self):
7381
filename = "Tests/images/pil168.tif"
7482
im = Image.open(filename)

Tests/test_image.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ def test_width_height(self):
5656
self.assertEqual(im.width, 1)
5757
self.assertEqual(im.height, 2)
5858

59-
im.size = (3, 4)
60-
self.assertEqual(im.width, 3)
61-
self.assertEqual(im.height, 4)
59+
with self.assertRaises(AttributeError) as e:
60+
im.size = (3, 4)
6261

6362
def test_invalid_image(self):
6463
if py3:

Tests/test_imagefile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class MockImageFile(ImageFile.ImageFile):
154154
def _open(self):
155155
self.rawmode = 'RGBA'
156156
self.mode = 'RGBA'
157-
self.size = (200, 200)
157+
self._size = (200, 200)
158158
self.tile = [("MOCK", (xoff, yoff, xoff+xsize, yoff+ysize), 32, None)]
159159

160160

docs/example/DdsImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def _open(self):
221221
header = BytesIO(header_bytes)
222222

223223
flags, height, width = struct.unpack("<3I", header.read(12))
224-
self.size = (width, height)
224+
self._size = (width, height)
225225
self.mode = "RGBA"
226226

227227
pitch, depth, mipmaps = struct.unpack("<3I", header.read(12))

docs/handbook/writing-your-own-file-decoder.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ true color.
6969
header = string.split(header)
7070

7171
# size in pixels (width, height)
72-
self.size = int(header[1]), int(header[2])
72+
self._size = int(header[1]), int(header[2])
7373

7474
# mode setting
7575
bits = int(header[3])

docs/releasenotes/5.3.0.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ and size, new method ``ImageOps.pad`` pads images to fill a requested aspect
3939
ratio and size, filling new space with a provided ``color`` and positioning the
4040
image within the new area through a ``centering`` argument.
4141

42+
Image Size
43+
==========
44+
45+
If you attempt to set the size of an image directly, e.g.
46+
``im.size = (100, 100)``, you will now receive an ``AttributeError``. This is
47+
not about removing existing functionality, but instead about raising an
48+
explicit error to prevent later consequences. The ``resize`` method is the
49+
correct way to change an image's size.
50+
51+
The exceptions to this are:
52+
* The ICO and ICNS image formats, which use ``im.size = (100, 100)`` to select
53+
a subimage.
54+
* The TIFF image format, which now has a ``DeprecationWarning`` for this
55+
action, as direct image size setting was previously necessary to work around an
56+
issue with tile extents.
57+
4258
Other Changes
4359
=============
4460

src/PIL/BlpImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _read_blp_header(self):
270270
self._blp_alpha_encoding, = struct.unpack("<b", self.fp.read(1))
271271
self._blp_mips, = struct.unpack("<b", self.fp.read(1))
272272

273-
self.size = struct.unpack("<II", self.fp.read(8))
273+
self._size = struct.unpack("<II", self.fp.read(8))
274274

275275
if self.magic == b"BLP1":
276276
# Only present for BLP1

src/PIL/BmpImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _bitmap(self, header=0, offset=0):
143143
file_info['header_size'])
144144
# ------------------ Special case : header is reported 40, which
145145
# ---------------------- is shorter than real size for bpp >= 16
146-
self.size = file_info['width'], file_info['height']
146+
self._size = file_info['width'], file_info['height']
147147
# -------- If color count was not found in the header, compute from bits
148148
file_info['colors'] = file_info['colors'] if file_info.get('colors', 0) else (1 << file_info['bits'])
149149
# -------------------------------- Check abnormal values for DOS attacks

0 commit comments

Comments
 (0)