Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: box offset can't be negative #4087

Closed
icemac opened this issue Sep 24, 2019 · 3 comments · Fixed by #4088
Closed

ValueError: box offset can't be negative #4087

icemac opened this issue Sep 24, 2019 · 3 comments · Fixed by #4088
Labels
Bug Any unexpected behavior, until confirmed feature.

Comments

@icemac
Copy link

icemac commented Sep 24, 2019

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?

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.)

What are your OS, Python and Pillow versions?

  • OS: MacOS
  • Python: 2.7 and 3.7
  • Pillow: 6.1.0
import PIL.Image
import PIL.ImageOps

with open('example.png', 'rb') as reader:
    img = PIL.Image.open(reader)
    PIL.ImageOps.fit(img, (600, 453), PIL.Image.ANTIALIAS)

example

@hugovk
Copy link
Member

hugovk commented Sep 24, 2019

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

Commit 73eec90 is from PR #3409.

@radarhere
Copy link
Member

I can reproduce the error simply with

from PIL import Image, ImageOps
img = Image.new("RGB", (1000, 755))
ImageOps.fit(img, (1000, 755))

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 -

if live_size_ratio >= output_ratio:

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.

I've created PR #4088 to resolve this.

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Sep 25, 2019
@icemac
Copy link
Author

icemac commented Sep 30, 2019

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants