You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to transform a PNG file using PIL.ImageOps.fit().
What did you expect to happen?
I expected to get back the transformed image.
What actually happened?
Traceback (most recent call last):
File "example.py", line 6, in <module>
PIL.ImageOps.fit(img, (600, 453), PIL.Image.ANTIALIAS)
File ".../lib/python3.7/site-packages/PIL/ImageOps.py", line 445, in fit
return image.resize(size, method, box=crop)
File ".../lib/python3.7/site-packages/PIL/Image.py", line 1892, in resize
return self._new(self.im.resize(size, resample, box))
ValueError: box offset can't be negative
This exception seems to be dependent on the size of the image (1000, 755) and the target size (600, 453). In this case the value for box gets (-5.684341886080802e-14, 0.0, 1000.0, 755.0) which leads to the exception. (The attached PNG is not the original one where the problem happened in production but an arbitrarily constructed one having the same dimensions.)
It started in Pillow 5.4.0. Git bisecting between 5.3.0 and 5.4.0 gives:
73eec9000d5fa0e88786a560882fba921cfc42df is the first bad commit
commit 73eec9000d5fa0e88786a560882fba921cfc42df
Date: Wed Oct 17 01:57:55 2018 +0300
Optimise ImageOps.fit by combining resize in crop
:040000 040000 d0967f545f67423f334dd2725678fc213432e780 76675c8bf4e9c6520b739e31b4df4d4f143a0632 M src
bisect run success
The ratio for this image is 1000.0 / 755 = 1.3245033112582782. Pillow currently treats the case where the ratios are equal the same as the case where the width needs to be cropped -
So calculating the width from the ratio gives 1.3245033112582782 * 755 = 1000.0000000000001. Pillow then tries to center this greater width, causing a negative x offset when cropping, leading to the error.
What did you do?
I wanted to transform a PNG file using
PIL.ImageOps.fit()
.What did you expect to happen?
I expected to get back the transformed image.
What actually happened?
This exception seems to be dependent on the size of the image
(1000, 755)
and the target size(600, 453)
. In this case the value forbox
gets(-5.684341886080802e-14, 0.0, 1000.0, 755.0)
which leads to the exception. (The attached PNG is not the original one where the problem happened in production but an arbitrarily constructed one having the same dimensions.)What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: