From 4126f6cdf7881c91e41abf5fd604f70f7d90d2e3 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 24 Nov 2019 04:55:49 +0300 Subject: [PATCH] return chosen image mode and the box of the image --- Tests/test_image_draft.py | 6 +++++- src/PIL/Image.py | 3 +++ src/PIL/JpegImagePlugin.py | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_draft.py b/Tests/test_image_draft.py index 5d92ee797..0f7402698 100644 --- a/Tests/test_image_draft.py +++ b/Tests/test_image_draft.py @@ -13,7 +13,11 @@ class TestImageDraft(PillowTestCase): im = Image.new(in_mode, in_size) data = tostring(im, "JPEG") im = fromstring(data) - im.draft(req_mode, req_size) + mode, box = im.draft(req_mode, req_size) + scale, _ = im.decoderconfig + self.assertEqual(box[:2], (0, 0)) + self.assertTrue((im.width - scale) < box[2] <= im.width) + self.assertTrue((im.height - scale) < box[3] <= im.height) return im def test_size(self): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index f8367a40c..05ff9239e 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1190,6 +1190,9 @@ class Image(object): JPEG to greyscale while loading it, or to extract a 128x192 version from a PCD file. + If any changes are made, returns a tuple with the chosen `mode` and + `box` with coordinates of the original image within the altered one. + Note that this method modifies the :py:class:`~PIL.Image.Image` object in place. If the image has already been loaded, this method has no effect. diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 2a96b169c..57564a245 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -416,6 +416,7 @@ class JpegImageFile(ImageFile.ImageFile): d, e, o, a = self.tile[0] scale = 1 + original_size = self.size if a[0] == "RGB" and mode in ["L", "YCbCr"]: self.mode = mode @@ -438,7 +439,9 @@ class JpegImageFile(ImageFile.ImageFile): self.tile = [(d, e, o, a)] self.decoderconfig = (scale, 0) - return self + box = (0, 0, original_size[0] / float(scale), + original_size[1] / float(scale)) + return (self.mode, box) def load_djpeg(self):