return chosen image mode and the box of the image

This commit is contained in:
Alexander 2019-11-24 04:55:49 +03:00
parent 2d1482b400
commit 4126f6cdf7
3 changed files with 12 additions and 2 deletions

View File

@ -13,7 +13,11 @@ class TestImageDraft(PillowTestCase):
im = Image.new(in_mode, in_size) im = Image.new(in_mode, in_size)
data = tostring(im, "JPEG") data = tostring(im, "JPEG")
im = fromstring(data) 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 return im
def test_size(self): def test_size(self):

View File

@ -1190,6 +1190,9 @@ class Image(object):
JPEG to greyscale while loading it, or to extract a 128x192 JPEG to greyscale while loading it, or to extract a 128x192
version from a PCD file. 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 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 in place. If the image has already been loaded, this method has no
effect. effect.

View File

@ -416,6 +416,7 @@ class JpegImageFile(ImageFile.ImageFile):
d, e, o, a = self.tile[0] d, e, o, a = self.tile[0]
scale = 1 scale = 1
original_size = self.size
if a[0] == "RGB" and mode in ["L", "YCbCr"]: if a[0] == "RGB" and mode in ["L", "YCbCr"]:
self.mode = mode self.mode = mode
@ -438,7 +439,9 @@ class JpegImageFile(ImageFile.ImageFile):
self.tile = [(d, e, o, a)] self.tile = [(d, e, o, a)]
self.decoderconfig = (scale, 0) 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): def load_djpeg(self):