Skip to content

Commit

Permalink
Renamed Jpeg2K variable to prevent masking Image reduce method
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 23, 2020
1 parent dab94e6 commit 2d91fbe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -119,6 +119,12 @@ def test_reduce(self):
im.load()
assert im.size == (160, 120)

def test_load_reduce(self):
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.load_reduce = 2
im.load()
self.assertEqual(im.size, (160, 120))

def test_layers_type(self):
outfile = self.tempfile("temp_layers.jp2")
for quality_layers in [[100, 50, 10], (100, 50, 10), None]:
Expand Down
7 changes: 7 additions & 0 deletions Tests/test_image_reduce.py
Expand Up @@ -3,6 +3,8 @@

from .helper import PillowTestCase, convert_to_comparable

codecs = dir(Image.core)


class TestImageReduce(PillowTestCase):
# There are several internal implementations
Expand Down Expand Up @@ -236,3 +238,8 @@ def test_mode_F(self):
for factor in self.remarkable_factors:
self.compare_reduce_with_reference(im, factor, 0, 0)
self.compare_reduce_with_box(im, factor)

@pytest.mark.skipif("jpeg2k_decoder" not in codecs, reason="JPEG 2000 support not available")
def test_jpeg2k(self):
with Image.open("Tests/images/test-card-lossless.jp2") as im:
self.assertEqual(im.reduce(2).size, (320, 240))
11 changes: 6 additions & 5 deletions src/PIL/Jpeg2KImagePlugin.py
Expand Up @@ -176,7 +176,7 @@ def _open(self):
if self.size is None or self.mode is None:
raise SyntaxError("unable to determine size/mode")

self.reduce = 0
self.load_reduce = 0
self.layers = 0

fd = -1
Expand All @@ -200,13 +200,14 @@ def _open(self):
"jpeg2k",
(0, 0) + self.size,
0,
(self.codec, self.reduce, self.layers, fd, length),
(self.codec, self.load_reduce, self.layers, fd, length),
)
]

def load(self):
if self.reduce:
power = 1 << self.reduce
reduce = self.reduce if not callable(self.reduce) else self.load_reduce
if reduce:
power = 1 << reduce
adjust = power >> 1
self._size = (
int((self.size[0] + adjust) / power),
Expand All @@ -216,7 +217,7 @@ def load(self):
if self.tile:
# Update the reduce and layers settings
t = self.tile[0]
t3 = (t[3][0], self.reduce, self.layers, t[3][3], t[3][4])
t3 = (t[3][0], reduce, self.layers, t[3][3], t[3][4])
self.tile = [(t[0], (0, 0) + self.size, t[2], t3)]

return ImageFile.ImageFile.load(self)
Expand Down

0 comments on commit 2d91fbe

Please sign in to comment.