66from io import BytesIO
77from pathlib import Path
88from types import ModuleType
9- from typing import Any
9+ from typing import Any , cast
1010
1111import pytest
1212
4545
4646@skip_unless_feature ("jpg" )
4747class TestFileJpeg :
48- def roundtrip (self , im : Image .Image , ** options : Any ) -> Image .Image :
48+ def roundtrip_with_bytes (
49+ self , im : Image .Image , ** options : Any
50+ ) -> tuple [JpegImagePlugin .JpegImageFile , int ]:
4951 out = BytesIO ()
5052 im .save (out , "JPEG" , ** options )
5153 test_bytes = out .tell ()
5254 out .seek (0 )
53- im = Image .open (out )
54- im .bytes = test_bytes # for testing only
55- return im
55+ reloaded = cast (JpegImagePlugin .JpegImageFile , Image .open (out ))
56+ return reloaded , test_bytes
57+
58+ def roundtrip (
59+ self , im : Image .Image , ** options : Any
60+ ) -> JpegImagePlugin .JpegImageFile :
61+ return self .roundtrip_with_bytes (im , ** options )[0 ]
5662
5763 def gen_random_image (self , size : tuple [int , int ], mode : str = "RGB" ) -> Image .Image :
5864 """Generates a very hard to compress file
@@ -246,13 +252,13 @@ def test_large_icc_meta(self, tmp_path: Path) -> None:
246252 im .save (f , progressive = True , quality = 94 , exif = b" " * 43668 )
247253
248254 def test_optimize (self ) -> None :
249- im1 = self .roundtrip (hopper ())
250- im2 = self .roundtrip (hopper (), optimize = 0 )
251- im3 = self .roundtrip (hopper (), optimize = 1 )
255+ im1 , im1_bytes = self .roundtrip_with_bytes (hopper ())
256+ im2 , im2_bytes = self .roundtrip_with_bytes (hopper (), optimize = 0 )
257+ im3 , im3_bytes = self .roundtrip_with_bytes (hopper (), optimize = 1 )
252258 assert_image_equal (im1 , im2 )
253259 assert_image_equal (im1 , im3 )
254- assert im1 . bytes >= im2 . bytes
255- assert im1 . bytes >= im3 . bytes
260+ assert im1_bytes >= im2_bytes
261+ assert im1_bytes >= im3_bytes
256262
257263 def test_optimize_large_buffer (self , tmp_path : Path ) -> None :
258264 # https://github.com/python-pillow/Pillow/issues/148
@@ -262,15 +268,15 @@ def test_optimize_large_buffer(self, tmp_path: Path) -> None:
262268 im .save (f , format = "JPEG" , optimize = True )
263269
264270 def test_progressive (self ) -> None :
265- im1 = self .roundtrip (hopper ())
271+ im1 , im1_bytes = self .roundtrip_with_bytes (hopper ())
266272 im2 = self .roundtrip (hopper (), progressive = False )
267- im3 = self .roundtrip (hopper (), progressive = True )
273+ im3 , im3_bytes = self .roundtrip_with_bytes (hopper (), progressive = True )
268274 assert not im1 .info .get ("progressive" )
269275 assert not im2 .info .get ("progressive" )
270276 assert im3 .info .get ("progressive" )
271277
272278 assert_image_equal (im1 , im3 )
273- assert im1 . bytes >= im3 . bytes
279+ assert im1_bytes >= im3_bytes
274280
275281 def test_progressive_large_buffer (self , tmp_path : Path ) -> None :
276282 f = str (tmp_path / "temp.jpg" )
@@ -341,6 +347,7 @@ def test_empty_exif_gps(self) -> None:
341347 assert exif .get_ifd (0x8825 ) == {}
342348
343349 transposed = ImageOps .exif_transpose (im )
350+ assert transposed is not None
344351 exif = transposed .getexif ()
345352 assert exif .get_ifd (0x8825 ) == {}
346353
@@ -419,14 +426,14 @@ def test_progressive_compat(self) -> None:
419426 assert im3 .info .get ("progression" )
420427
421428 def test_quality (self ) -> None :
422- im1 = self .roundtrip (hopper ())
423- im2 = self .roundtrip (hopper (), quality = 50 )
429+ im1 , im1_bytes = self .roundtrip_with_bytes (hopper ())
430+ im2 , im2_bytes = self .roundtrip_with_bytes (hopper (), quality = 50 )
424431 assert_image (im1 , im2 .mode , im2 .size )
425- assert im1 . bytes >= im2 . bytes
432+ assert im1_bytes >= im2_bytes
426433
427- im3 = self .roundtrip (hopper (), quality = 0 )
434+ im3 , im3_bytes = self .roundtrip_with_bytes (hopper (), quality = 0 )
428435 assert_image (im1 , im3 .mode , im3 .size )
429- assert im2 . bytes > im3 . bytes
436+ assert im2_bytes > im3_bytes
430437
431438 def test_smooth (self ) -> None :
432439 im1 = self .roundtrip (hopper ())
0 commit comments