Skip to content

Commit 0ca5a0b

Browse files
committed
Use different variables for Image and ImageFile instances
1 parent 370da46 commit 0ca5a0b

File tree

8 files changed

+90
-89
lines changed

8 files changed

+90
-89
lines changed

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: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,16 @@ def test_palette_handling(tmp_path: Path) -> None:
354354
# see https://github.com/python-pillow/Pillow/issues/513
355355

356356
with Image.open(TEST_GIF) as im:
357-
im = im.convert("RGB")
357+
im_rgb = im.convert("RGB")
358358

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

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

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

368368

369369
def test_palette_434(tmp_path: Path) -> None:
@@ -383,35 +383,36 @@ def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image:
383383
with roundtrip(im, optimize=True) as reloaded:
384384
assert_image_similar(im, reloaded, 1)
385385

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)
386+
im_rgb = im.convert("RGB")
387+
388+
# check automatic P conversion
389+
with roundtrip(im_rgb) as reloaded:
390+
reloaded = reloaded.convert("RGB")
391+
assert_image_equal(im_rgb, reloaded)
391392

392393

393394
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
394395
def test_save_netpbm_bmp_mode(tmp_path: Path) -> None:
395396
with Image.open(TEST_GIF) as img:
396-
img = img.convert("RGB")
397+
img_rgb = img.convert("RGB")
397398

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)
399+
tempfile = str(tmp_path / "temp.gif")
400+
b = BytesIO()
401+
GifImagePlugin._save_netpbm(img_rgb, b, tempfile)
402+
with Image.open(tempfile) as reloaded:
403+
assert_image_similar(img_rgb, reloaded.convert("RGB"), 0)
403404

404405

405406
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
406407
def test_save_netpbm_l_mode(tmp_path: Path) -> None:
407408
with Image.open(TEST_GIF) as img:
408-
img = img.convert("L")
409+
img_l = img.convert("L")
409410

410411
tempfile = str(tmp_path / "temp.gif")
411412
b = BytesIO()
412-
GifImagePlugin._save_netpbm(img, b, tempfile)
413+
GifImagePlugin._save_netpbm(img_l, b, tempfile)
413414
with Image.open(tempfile) as reloaded:
414-
assert_image_similar(img, reloaded.convert("L"), 0)
415+
assert_image_similar(img_l, reloaded.convert("L"), 0)
415416

416417

417418
def test_seek() -> None:
@@ -1038,26 +1039,26 @@ def test_webp_background(tmp_path: Path) -> None:
10381039
im.save(out)
10391040

10401041
# 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)
1042+
im2 = Image.new("L", (100, 100), "#000")
1043+
im2.info["background"] = (0, 0, 0, 0)
1044+
im2.save(out)
10441045

10451046

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

10501051
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)
1052+
im2 = Image.new("L", (100, 100), "#000")
1053+
im2.info["comment"] = b"Test comment text"
1054+
im2.save(out)
10541055
with Image.open(out) as reread:
1055-
assert reread.info["comment"] == im.info["comment"]
1056+
assert reread.info["comment"] == im2.info["comment"]
10561057

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

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

Tests/test_file_libtiff.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,12 @@ def test_blur(self, tmp_path: Path) -> None:
507507
# and save to compressed tif.
508508
out = tmp_path / "temp.tif"
509509
with Image.open("Tests/images/pport_g4.tif") as im:
510-
im = im.convert("L")
510+
im_l = im.convert("L")
511511

512-
im = im.filter(ImageFilter.GaussianBlur(4))
513-
im.save(out, compression="tiff_adobe_deflate")
512+
im_l = im_l.filter(ImageFilter.GaussianBlur(4))
513+
im_l.save(out, compression="tiff_adobe_deflate")
514514

515-
assert_image_equal_tofile(im, out)
515+
assert_image_equal_tofile(im_l, out)
516516

517517
def test_compressions(self, tmp_path: Path) -> None:
518518
# Test various tiff compressions and assert similar image content but reduced
@@ -1078,8 +1078,10 @@ def test_old_style_jpeg_orientation(self) -> None:
10781078
data = data[:102] + b"\x02" + data[103:]
10791079

10801080
with Image.open(io.BytesIO(data)) as im:
1081-
im = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
1082-
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")
1081+
im_transposed = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
1082+
assert_image_equal_tofile(
1083+
im_transposed, "Tests/images/old-style-jpeg-compression.png"
1084+
)
10831085

10841086
def test_open_missing_samplesperpixel(self) -> None:
10851087
with Image.open(
@@ -1146,9 +1148,9 @@ def test_exif_transpose(self) -> None:
11461148
with Image.open("Tests/images/g4_orientation_1.tif") as base_im:
11471149
for i in range(2, 9):
11481150
with Image.open("Tests/images/g4_orientation_" + str(i) + ".tif") as im:
1149-
im = ImageOps.exif_transpose(im)
1151+
im_transposed = ImageOps.exif_transpose(im)
11501152

1151-
assert_image_similar(base_im, im, 0.7)
1153+
assert_image_similar(base_im, im_transposed, 0.7)
11521154

11531155
@pytest.mark.parametrize(
11541156
"test_file",

Tests/test_file_png.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ def test_sanity(self, tmp_path: Path) -> None:
101101
assert im.get_format_mimetype() == "image/png"
102102

103103
for mode in ["1", "L", "P", "RGB", "I;16", "I;16B"]:
104-
im = hopper(mode)
105-
im.save(test_file)
104+
im1 = hopper(mode)
105+
im1.save(test_file)
106106
with Image.open(test_file) as reloaded:
107107
if mode == "I;16B":
108108
reloaded = reloaded.convert(mode)
109-
assert_image_equal(reloaded, im)
109+
assert_image_equal(reloaded, im1)
110110

111111
def test_invalid_file(self) -> None:
112112
invalid_file = "Tests/images/flower.jpg"
@@ -225,11 +225,11 @@ def test_load_transparent_p(self) -> None:
225225
test_file = "Tests/images/pil123p.png"
226226
with Image.open(test_file) as im:
227227
assert_image(im, "P", (162, 150))
228-
im = im.convert("RGBA")
229-
assert_image(im, "RGBA", (162, 150))
228+
im_rgba = im.convert("RGBA")
229+
assert_image(im_rgba, "RGBA", (162, 150))
230230

231231
# image has 124 unique alpha values
232-
colors = im.getchannel("A").getcolors()
232+
colors = im_rgba.getchannel("A").getcolors()
233233
assert colors is not None
234234
assert len(colors) == 124
235235

@@ -239,11 +239,11 @@ def test_load_transparent_rgb(self) -> None:
239239
assert im.info["transparency"] == (0, 255, 52)
240240

241241
assert_image(im, "RGB", (64, 64))
242-
im = im.convert("RGBA")
243-
assert_image(im, "RGBA", (64, 64))
242+
im_rgba = im.convert("RGBA")
243+
assert_image(im_rgba, "RGBA", (64, 64))
244244

245245
# image has 876 transparent pixels
246-
colors = im.getchannel("A").getcolors()
246+
colors = im_rgba.getchannel("A").getcolors()
247247
assert colors is not None
248248
assert colors[0][0] == 876
249249

@@ -262,11 +262,11 @@ def test_save_p_transparent_palette(self, tmp_path: Path) -> None:
262262
assert len(im.info["transparency"]) == 256
263263

264264
assert_image(im, "P", (162, 150))
265-
im = im.convert("RGBA")
266-
assert_image(im, "RGBA", (162, 150))
265+
im_rgba = im.convert("RGBA")
266+
assert_image(im_rgba, "RGBA", (162, 150))
267267

268268
# image has 124 unique alpha values
269-
colors = im.getchannel("A").getcolors()
269+
colors = im_rgba.getchannel("A").getcolors()
270270
assert colors is not None
271271
assert len(colors) == 124
272272

@@ -285,13 +285,13 @@ def test_save_p_single_transparency(self, tmp_path: Path) -> None:
285285
assert im.info["transparency"] == 164
286286
assert im.getpixel((31, 31)) == 164
287287
assert_image(im, "P", (64, 64))
288-
im = im.convert("RGBA")
289-
assert_image(im, "RGBA", (64, 64))
288+
im_rgba = im.convert("RGBA")
289+
assert_image(im_rgba, "RGBA", (64, 64))
290290

291-
assert im.getpixel((31, 31)) == (0, 255, 52, 0)
291+
assert im_rgba.getpixel((31, 31)) == (0, 255, 52, 0)
292292

293293
# image has 876 transparent pixels
294-
colors = im.getchannel("A").getcolors()
294+
colors = im_rgba.getchannel("A").getcolors()
295295
assert colors is not None
296296
assert colors[0][0] == 876
297297

Tests/test_imageops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ def check(orientation_im: Image.Image) -> None:
457457
assert 0x0112 not in transposed_im.getexif()
458458

459459
# Orientation set directly on Image.Exif
460-
im = hopper()
461-
im.getexif()[0x0112] = 3
462-
transposed_im = ImageOps.exif_transpose(im)
460+
im1 = hopper()
461+
im1.getexif()[0x0112] = 3
462+
transposed_im = ImageOps.exif_transpose(im1)
463463
assert 0x0112 not in transposed_im.getexif()
464464

465465

0 commit comments

Comments
 (0)