From 660894cd367b79e1280cea8fa67536416d1a9138 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 24 May 2020 23:58:30 +1000 Subject: [PATCH 1/2] Write JFIF header when saving JPEG --- Tests/test_file_jpeg.py | 18 ++++++++++-------- src/libImaging/JpegEncode.c | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 08db11645..616bb4dd8 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -91,15 +91,17 @@ class TestFileJpeg: assert k > 0.9 def test_dpi(self): - def test(xdpi, ydpi=None): - with Image.open(TEST_FILE) as im: - im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi)) - return im.info.get("dpi") + for test_image_path in [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"]: - assert test(72) == (72, 72) - assert test(300) == (300, 300) - assert test(100, 200) == (100, 200) - assert test(0) is None # square pixels + def test(xdpi, ydpi=None): + with Image.open(test_image_path) as im: + im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi)) + return im.info.get("dpi") + + assert test(72) == (72, 72) + assert test(300) == (300, 300) + assert test(100, 200) == (100, 200) + assert test(0) is None # square pixels def test_icc(self, tmp_path): # Test ICC support diff --git a/src/libImaging/JpegEncode.c b/src/libImaging/JpegEncode.c index 8882b61be..b255025fa 100644 --- a/src/libImaging/JpegEncode.c +++ b/src/libImaging/JpegEncode.c @@ -222,6 +222,7 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) context->cinfo.smoothing_factor = context->smooth; context->cinfo.optimize_coding = (boolean) context->optimize; if (context->xdpi > 0 && context->ydpi > 0) { + context->cinfo.write_JFIF_header = TRUE; context->cinfo.density_unit = 1; /* dots per inch */ context->cinfo.X_density = context->xdpi; context->cinfo.Y_density = context->ydpi; From 696aa7972df42dbd0408b79c3b5178d515845be1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 26 May 2020 07:15:20 +1000 Subject: [PATCH 2/2] Parametrized test --- Tests/test_file_jpeg.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 616bb4dd8..258908871 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -90,18 +90,19 @@ class TestFileJpeg: ] assert k > 0.9 - def test_dpi(self): - for test_image_path in [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"]: + @pytest.mark.parametrize( + "test_image_path", [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"], + ) + def test_dpi(self, test_image_path): + def test(xdpi, ydpi=None): + with Image.open(test_image_path) as im: + im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi)) + return im.info.get("dpi") - def test(xdpi, ydpi=None): - with Image.open(test_image_path) as im: - im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi)) - return im.info.get("dpi") - - assert test(72) == (72, 72) - assert test(300) == (300, 300) - assert test(100, 200) == (100, 200) - assert test(0) is None # square pixels + assert test(72) == (72, 72) + assert test(300) == (300, 300) + assert test(100, 200) == (100, 200) + assert test(0) is None # square pixels def test_icc(self, tmp_path): # Test ICC support