Skip to content

Commit 2ebb3e9

Browse files
authored
Use different variables for Image and ImageFile instances (#9316)
1 parent a04c980 commit 2ebb3e9

13 files changed

+118
-119
lines changed

Tests/helper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ def assert_image_equal_tofile(
104104
msg: str | None = None,
105105
mode: str | None = None,
106106
) -> None:
107-
with Image.open(filename) as img:
108-
if mode:
109-
img = img.convert(mode)
110-
assert_image_equal(a, img, msg)
107+
with Image.open(filename) as im:
108+
converted_im = im.convert(mode) if mode else im
109+
assert_image_equal(a, converted_im, msg)
111110

112111

113112
def assert_image_similar(

Tests/test_bmp_reference.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ def get_compare(f: str) -> str:
9595
for f in get_files("g"):
9696
try:
9797
with Image.open(f) as im:
98-
im.load()
9998
with Image.open(get_compare(f)) as compare:
100-
compare.load()
101-
if im.mode == "P":
102-
# assert image similar doesn't really work
103-
# with paletized image, since the palette might
104-
# be differently ordered for an equivalent image.
105-
im = im.convert("RGBA")
106-
compare = compare.convert("RGBA")
107-
assert_image_similar(im, compare, 5)
99+
# assert image similar doesn't really work
100+
# with paletized image, since the palette might
101+
# be differently ordered for an equivalent image.
102+
im_converted = im.convert("RGBA") if im.mode == "P" else im
103+
compare_converted = (
104+
compare.convert("RGBA") if im.mode == "P" else compare
105+
)
106+
107+
assert_image_similar(im_converted, compare_converted, 5)
108108

109109
except Exception as msg:
110110
# there are three here that are unsupported:

Tests/test_file_apng.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,25 @@ def test_apng_mode() -> None:
278278
assert isinstance(im, PngImagePlugin.PngImageFile)
279279
assert im.mode == "P"
280280
im.seek(im.n_frames - 1)
281-
im = im.convert("RGB")
282-
assert im.getpixel((0, 0)) == (0, 255, 0)
283-
assert im.getpixel((64, 32)) == (0, 255, 0)
281+
im_rgb = im.convert("RGB")
282+
assert im_rgb.getpixel((0, 0)) == (0, 255, 0)
283+
assert im_rgb.getpixel((64, 32)) == (0, 255, 0)
284284

285285
with Image.open("Tests/images/apng/mode_palette_alpha.png") as im:
286286
assert isinstance(im, PngImagePlugin.PngImageFile)
287287
assert im.mode == "P"
288288
im.seek(im.n_frames - 1)
289-
im = im.convert("RGBA")
290-
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
291-
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
289+
im_rgba = im.convert("RGBA")
290+
assert im_rgba.getpixel((0, 0)) == (0, 255, 0, 255)
291+
assert im_rgba.getpixel((64, 32)) == (0, 255, 0, 255)
292292

293293
with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im:
294294
assert isinstance(im, PngImagePlugin.PngImageFile)
295295
assert im.mode == "P"
296296
im.seek(im.n_frames - 1)
297-
im = im.convert("RGBA")
298-
assert im.getpixel((0, 0)) == (0, 0, 255, 128)
299-
assert im.getpixel((64, 32)) == (0, 0, 255, 128)
297+
im_rgba = im.convert("RGBA")
298+
assert im_rgba.getpixel((0, 0)) == (0, 0, 255, 128)
299+
assert im_rgba.getpixel((64, 32)) == (0, 0, 255, 128)
300300

301301

302302
def test_apng_chunk_errors() -> None:

Tests/test_file_bmp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ def test_rgba_bitfields() -> None:
165165
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im:
166166
# So before the comparing the image, swap the channels
167167
b, g, r = im.split()[1:]
168-
im = Image.merge("RGB", (r, g, b))
168+
im_rgb = Image.merge("RGB", (r, g, b))
169169

170-
assert_image_equal_tofile(im, "Tests/images/bmp/q/rgb32bf-xbgr.bmp")
170+
assert_image_equal_tofile(im_rgb, "Tests/images/bmp/q/rgb32bf-xbgr.bmp")
171171

172172
# This test image has been manually hexedited
173173
# to change the bitfield compression in the header from XBGR to ABGR

Tests/test_file_dds.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@
5757
def test_sanity_dxt1_bc1(image_path: str) -> None:
5858
"""Check DXT1 and BC1 images can be opened"""
5959
with Image.open(TEST_FILE_DXT1.replace(".dds", ".png")) as target:
60-
target = target.convert("RGBA")
60+
target_rgba = target.convert("RGBA")
6161
with Image.open(image_path) as im:
6262
im.load()
6363

6464
assert im.format == "DDS"
6565
assert im.mode == "RGBA"
6666
assert im.size == (256, 256)
6767

68-
assert_image_equal(im, target)
68+
assert_image_equal(im, target_rgba)
6969

7070

7171
def test_sanity_dxt3() -> None:
@@ -520,9 +520,9 @@ def test_save_dx10_bc5(tmp_path: Path) -> None:
520520
im.save(out, pixel_format="BC5")
521521
assert_image_similar_tofile(im, out, 9.56)
522522

523-
im = hopper("L")
523+
im_l = hopper("L")
524524
with pytest.raises(OSError, match="only RGB mode can be written as BC5"):
525-
im.save(out, pixel_format="BC5")
525+
im_l.save(out, pixel_format="BC5")
526526

527527

528528
@pytest.mark.parametrize(

Tests/test_file_eps.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ def test_bytesio_object() -> None:
265265
img.load()
266266

267267
with Image.open(FILE1_COMPARE) as image1_scale1_compare:
268-
image1_scale1_compare = image1_scale1_compare.convert("RGB")
269-
image1_scale1_compare.load()
270-
assert_image_similar(img, image1_scale1_compare, 5)
268+
image1_scale1_compare_rgb = image1_scale1_compare.convert("RGB")
269+
image1_scale1_compare_rgb.load()
270+
assert_image_similar(img, image1_scale1_compare_rgb, 5)
271271

272272

273273
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
@@ -301,17 +301,17 @@ def test_render_scale1() -> None:
301301
with Image.open(FILE1) as image1_scale1:
302302
image1_scale1.load()
303303
with Image.open(FILE1_COMPARE) as image1_scale1_compare:
304-
image1_scale1_compare = image1_scale1_compare.convert("RGB")
305-
image1_scale1_compare.load()
306-
assert_image_similar(image1_scale1, image1_scale1_compare, 5)
304+
image1_scale1_compare_rgb = image1_scale1_compare.convert("RGB")
305+
image1_scale1_compare_rgb.load()
306+
assert_image_similar(image1_scale1, image1_scale1_compare_rgb, 5)
307307

308308
# Non-zero bounding box
309309
with Image.open(FILE2) as image2_scale1:
310310
image2_scale1.load()
311311
with Image.open(FILE2_COMPARE) as image2_scale1_compare:
312-
image2_scale1_compare = image2_scale1_compare.convert("RGB")
313-
image2_scale1_compare.load()
314-
assert_image_similar(image2_scale1, image2_scale1_compare, 10)
312+
image2_scale1_compare_rgb = image2_scale1_compare.convert("RGB")
313+
image2_scale1_compare_rgb.load()
314+
assert_image_similar(image2_scale1, image2_scale1_compare_rgb, 10)
315315

316316

317317
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
@@ -324,18 +324,16 @@ def test_render_scale2() -> None:
324324
assert isinstance(image1_scale2, EpsImagePlugin.EpsImageFile)
325325
image1_scale2.load(scale=2)
326326
with Image.open(FILE1_COMPARE_SCALE2) as image1_scale2_compare:
327-
image1_scale2_compare = image1_scale2_compare.convert("RGB")
328-
image1_scale2_compare.load()
329-
assert_image_similar(image1_scale2, image1_scale2_compare, 5)
327+
image1_scale2_compare_rgb = image1_scale2_compare.convert("RGB")
328+
assert_image_similar(image1_scale2, image1_scale2_compare_rgb, 5)
330329

331330
# Non-zero bounding box
332331
with Image.open(FILE2) as image2_scale2:
333332
assert isinstance(image2_scale2, EpsImagePlugin.EpsImageFile)
334333
image2_scale2.load(scale=2)
335334
with Image.open(FILE2_COMPARE_SCALE2) as image2_scale2_compare:
336-
image2_scale2_compare = image2_scale2_compare.convert("RGB")
337-
image2_scale2_compare.load()
338-
assert_image_similar(image2_scale2, image2_scale2_compare, 10)
335+
image2_scale2_compare_rgb = image2_scale2_compare.convert("RGB")
336+
assert_image_similar(image2_scale2, image2_scale2_compare_rgb, 10)
339337

340338

341339
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
@@ -345,8 +343,8 @@ def test_render_scale2() -> None:
345343
def test_resize(filename: str) -> None:
346344
with Image.open(filename) as im:
347345
new_size = (100, 100)
348-
im = im.resize(new_size)
349-
assert im.size == new_size
346+
im_resized = im.resize(new_size)
347+
assert im_resized.size == new_size
350348

351349

352350
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")

Tests/test_file_gif.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,13 @@ def test_loading_multiple_palettes(path: str, mode: str) -> None:
327327

328328
im.seek(1)
329329
assert im.mode == mode
330-
if mode == "RGBA":
331-
im = im.convert("RGB")
330+
im_rgb = im.convert("RGB") if mode == "RGBA" else im
332331

333332
# Check a color only from the old palette
334-
assert im.getpixel((0, 0)) == original_color
333+
assert im_rgb.getpixel((0, 0)) == original_color
335334

336335
# Check a color from the new palette
337-
assert im.getpixel((24, 24)) not in first_frame_colors
336+
assert im_rgb.getpixel((24, 24)) not in first_frame_colors
338337

339338

340339
def test_headers_saving_for_animated_gifs(tmp_path: Path) -> None:
@@ -354,16 +353,16 @@ def test_palette_handling(tmp_path: Path) -> None:
354353
# see https://github.com/python-pillow/Pillow/issues/513
355354

356355
with Image.open(TEST_GIF) as im:
357-
im = im.convert("RGB")
356+
im_rgb = im.convert("RGB")
358357

359-
im = im.resize((100, 100), Image.Resampling.LANCZOS)
360-
im2 = im.convert("P", palette=Image.Palette.ADAPTIVE, colors=256)
358+
im_rgb = im_rgb.resize((100, 100), Image.Resampling.LANCZOS)
359+
im_p = im_rgb.convert("P", palette=Image.Palette.ADAPTIVE, colors=256)
361360

362-
f = tmp_path / "temp.gif"
363-
im2.save(f, optimize=True)
361+
f = tmp_path / "temp.gif"
362+
im_p.save(f, optimize=True)
364363

365364
with Image.open(f) as reloaded:
366-
assert_image_similar(im, reloaded.convert("RGB"), 10)
365+
assert_image_similar(im_rgb, reloaded.convert("RGB"), 10)
367366

368367

369368
def test_palette_434(tmp_path: Path) -> None:
@@ -383,35 +382,36 @@ def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image:
383382
with roundtrip(im, optimize=True) as reloaded:
384383
assert_image_similar(im, reloaded, 1)
385384

386-
im = im.convert("RGB")
387-
# check automatic P conversion
388-
with roundtrip(im) as reloaded:
389-
reloaded = reloaded.convert("RGB")
390-
assert_image_equal(im, reloaded)
385+
im_rgb = im.convert("RGB")
386+
387+
# check automatic P conversion
388+
with roundtrip(im_rgb) as reloaded:
389+
reloaded = reloaded.convert("RGB")
390+
assert_image_equal(im_rgb, reloaded)
391391

392392

393393
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
394394
def test_save_netpbm_bmp_mode(tmp_path: Path) -> None:
395395
with Image.open(TEST_GIF) as img:
396-
img = img.convert("RGB")
396+
img_rgb = img.convert("RGB")
397397

398-
tempfile = str(tmp_path / "temp.gif")
399-
b = BytesIO()
400-
GifImagePlugin._save_netpbm(img, b, tempfile)
401-
with Image.open(tempfile) as reloaded:
402-
assert_image_similar(img, reloaded.convert("RGB"), 0)
398+
tempfile = str(tmp_path / "temp.gif")
399+
b = BytesIO()
400+
GifImagePlugin._save_netpbm(img_rgb, b, tempfile)
401+
with Image.open(tempfile) as reloaded:
402+
assert_image_similar(img_rgb, reloaded.convert("RGB"), 0)
403403

404404

405405
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
406406
def test_save_netpbm_l_mode(tmp_path: Path) -> None:
407407
with Image.open(TEST_GIF) as img:
408-
img = img.convert("L")
408+
img_l = img.convert("L")
409409

410410
tempfile = str(tmp_path / "temp.gif")
411411
b = BytesIO()
412-
GifImagePlugin._save_netpbm(img, b, tempfile)
412+
GifImagePlugin._save_netpbm(img_l, b, tempfile)
413413
with Image.open(tempfile) as reloaded:
414-
assert_image_similar(img, reloaded.convert("L"), 0)
414+
assert_image_similar(img_l, reloaded.convert("L"), 0)
415415

416416

417417
def test_seek() -> None:
@@ -1038,26 +1038,26 @@ def test_webp_background(tmp_path: Path) -> None:
10381038
im.save(out)
10391039

10401040
# Test non-opaque WebP background
1041-
im = Image.new("L", (100, 100), "#000")
1042-
im.info["background"] = (0, 0, 0, 0)
1043-
im.save(out)
1041+
im2 = Image.new("L", (100, 100), "#000")
1042+
im2.info["background"] = (0, 0, 0, 0)
1043+
im2.save(out)
10441044

10451045

10461046
def test_comment(tmp_path: Path) -> None:
10471047
with Image.open(TEST_GIF) as im:
10481048
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0"
10491049

10501050
out = tmp_path / "temp.gif"
1051-
im = Image.new("L", (100, 100), "#000")
1052-
im.info["comment"] = b"Test comment text"
1053-
im.save(out)
1051+
im2 = Image.new("L", (100, 100), "#000")
1052+
im2.info["comment"] = b"Test comment text"
1053+
im2.save(out)
10541054
with Image.open(out) as reread:
1055-
assert reread.info["comment"] == im.info["comment"]
1055+
assert reread.info["comment"] == im2.info["comment"]
10561056

1057-
im.info["comment"] = "Test comment text"
1058-
im.save(out)
1057+
im2.info["comment"] = "Test comment text"
1058+
im2.save(out)
10591059
with Image.open(out) as reread:
1060-
assert reread.info["comment"] == im.info["comment"].encode()
1060+
assert reread.info["comment"] == im2.info["comment"].encode()
10611061

10621062
# Test that GIF89a is used for comments
10631063
assert reread.info["version"] == b"GIF89a"

Tests/test_file_libtiff.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ def test_blur(self, tmp_path: Path) -> None:
516516
# and save to compressed tif.
517517
out = tmp_path / "temp.tif"
518518
with Image.open("Tests/images/pport_g4.tif") as im:
519-
im = im.convert("L")
519+
im_l = im.convert("L")
520520

521-
im = im.filter(ImageFilter.GaussianBlur(4))
522-
im.save(out, compression="tiff_adobe_deflate")
521+
im_l = im_l.filter(ImageFilter.GaussianBlur(4))
522+
im_l.save(out, compression="tiff_adobe_deflate")
523523

524-
assert_image_equal_tofile(im, out)
524+
assert_image_equal_tofile(im_l, out)
525525

526526
def test_compressions(self, tmp_path: Path) -> None:
527527
# Test various tiff compressions and assert similar image content but reduced
@@ -1087,8 +1087,10 @@ def test_old_style_jpeg_orientation(self) -> None:
10871087
data = data[:102] + b"\x02" + data[103:]
10881088

10891089
with Image.open(io.BytesIO(data)) as im:
1090-
im = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
1091-
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")
1090+
im_transposed = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
1091+
assert_image_equal_tofile(
1092+
im_transposed, "Tests/images/old-style-jpeg-compression.png"
1093+
)
10921094

10931095
def test_open_missing_samplesperpixel(self) -> None:
10941096
with Image.open(

0 commit comments

Comments
 (0)