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

getbbox() returns bounding box on all-black RGB image #5247

Closed
ghost opened this issue Feb 3, 2021 · 4 comments
Closed

getbbox() returns bounding box on all-black RGB image #5247

ghost opened this issue Feb 3, 2021 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 3, 2021

What did you do?

Running getbbox() on this all black RGB image:

weird_bbox

Context / Where is this image coming from?

I'm upgrading a project that was heavily affected by the getbbox() alpha channel behavior change (#4849). For example before the change we were doing something like this (the simplest example):

def images_equal(im1: Image, im2: Image):
  return ImageChops.difference(im1, im2).getbbox() is None

Because when comparing some images, the diff is only in the non-alpha channels and so it broke after the 7.1.0 update.

To fix this, we converted these cases to variations of this:

def images_equal(im1: Image, im2: Image):
  return ImageChops.difference(im1, im2).convert('RGB').getbbox() is None

This works in 99% of the cases, however we have one test case where it isn't because Pillow, for reasons I can't figure out, returns this (rogue ?) bounding box on this all black diff image.

What did you expect to happen?

I expected None to be returned.

What actually happened?

Pillow returned bounding box (8, 7, 995, 223).

What are your OS, Python and Pillow versions?

  • OS: Linux 5.8.0-7630-generic #32~1609193707~20.10~781bb80-Ubuntu SMP Tue Jan 5 21:29:56 UTC 2
  • Python: Python 3.6.12
  • Pillow: 8.1.0

Repro

from PIL import Image

image = Image.open('./weird_bbox.bmp')
print(image.getbbox())
@ghost ghost changed the title getbbox() returns bounding box on all-black image. getbbox() returns bounding box on all-black image Feb 3, 2021
@ghost ghost changed the title getbbox() returns bounding box on all-black image getbbox() returns bounding box on all-black RGB image Feb 3, 2021
@nulano
Copy link
Contributor

nulano commented Feb 3, 2021

Adjusting the levels in an image editor, I can see that the image is not completely black:
106730747-fcb18600-660e-11eb-912f-953fc5faec7d__

@ghost
Copy link
Author

ghost commented Feb 3, 2021

Wow. I even iterated over all the pixels and checked that they are (0, 0, 0) 🤔 Maybe I made a mistake there. Maybe after all my images (see context detail box) aren't the same... I'll check and maybe close.

The question then kinda becomes, why was this not detected in RGBA space, before the change in 7.1.0?

@hugovk
Copy link
Member

hugovk commented Feb 3, 2021

Adding in the version:

from PIL import Image

print("Pillow:", Image.__version__)
image = Image.open('./weird_bbox.bmp')
print(image.getbbox())

I get the same result for 7.0.0, 7.1.0 and 8.1.0 (macOS, Python 3.6.12):

Pillow: 7.0.0
(8, 7, 995, 223)
Pillow: 7.1.0
(8, 7, 995, 223)
Pillow: 8.1.0
(8, 7, 995, 223)

@ghost
Copy link
Author

ghost commented Feb 4, 2021

@hugovk Yes, this image for sure. But before upgrading I was comparing images without converting to RGB, because getbbox behaved differently.

Anyway, after sleeping over this one more night I finally think I figured it out. Something else about the upgrade, made the resulting image look slightly different from the fixture created on 7.0.0. I just got tricked into attributing it to the getbbox change because it was the cause for 99% of the other test failures.

I'm sorry for wasting time everyone. I was just brain-fried by the getbbox breaking change. Thanks for your help.

@ghost ghost closed this as completed Feb 4, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants