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: tile cannot extend outside image #4517

Closed
vnyk opened this issue Apr 3, 2020 · 5 comments
Closed

ValueError: tile cannot extend outside image #4517

vnyk opened this issue Apr 3, 2020 · 5 comments

Comments

@vnyk
Copy link

vnyk commented Apr 3, 2020

What did you do?

I am trying to crop an image
Sorry, I am not able to provide a source image

A_img = Image.fromarray(A_img[y1:y2, x1:x2, :])

What did you expect to happen?

To get the properly cropped image without any bugs

What actually happened?

File "/home/developer/workspace/dance/few-shot-vid2vid/data/fewshot_pose_dataset.py", line 185, in crop_person_region
    A_img = Image.fromarray(A_img[ys:ye, xs:xe, :])
  File "/home/developer/anaconda3/envs/fewshot-dev/lib/python3.7/site-packages/PIL/Image.py", line 2670, in fromarray
    return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
  File "/home/developer/anaconda3/envs/fewshot-dev/lib/python3.7/site-packages/PIL/Image.py", line 2613, in frombuffer
    return frombytes(mode, size, data, decoder_name, args)
  File "/home/developer/anaconda3/envs/fewshot-dev/lib/python3.7/site-packages/PIL/Image.py", line 2546, in frombytes
    im.frombytes(data, decoder_name, args)
  File "/home/developer/anaconda3/envs/fewshot-dev/lib/python3.7/site-packages/PIL/Image.py", line 829, in frombytes
    d.setimage(self.im)
ValueError: tile cannot extend outside image

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 16.04.6
  • Python: 3.7
  • Pillow: 6.1.0
@radarhere
Copy link
Member

Without an image, this is difficult to debug. I mean, I can construct a script that runs the line you have provided, using one of our test images, and it runs without a problem.

from PIL import Image
import numpy

x1, x2 = 20, 100
y1, y2 = 20, 100
im = Image.open('Tests/images/hopper.jpg')
A_img = numpy.asarray(im)

A_img = Image.fromarray(A_img[y1:y2, x1:x2, :])

@radarhere
Copy link
Member

I do find that ValueError: tile cannot extend outside image is raised if I set the dimensions to zero.

Image.fromarray(A_img[50:50, 50:50, :])

Is that the case with your code?

@radarhere
Copy link
Member

At the moment, the simplest explanation is that the dimensions that you're trying to crop are wrong. If they're zero, or negative, then I can replicate your error.

If you're able to provide values for x1, x2, y1 and y2 to show how you're cropping the image, that could be helpful. If you're able to let us know the format of your image (JPG, PNG, TIFF, etc.) that would be helpful.

Closing, as without more information, there isn't much to do here.

@ye-yng
Copy link

ye-yng commented Oct 14, 2022

Going to necro this issue with some information. I also encountered this issue when cropping images with bounding boxes detected through AWS Rekognition. The error only happens when negative coordinates are passed to the cropped image.

AWS Rekognition documentation actually has a note documenting the return values for the bounding box: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_BoundingBox.html

@radarhere
Copy link
Member

Hi. If you're using the same code in this issue, then I ask you to consider the following.

The following works when x2 is greater than x1 and y2 is greater than y1,

from PIL import Image
import numpy

x1, x2 = 0, 1
y1, y2 = 0, 1
im = Image.open('Tests/images/hopper.jpg')
A_img = numpy.asarray(im)

a = A_img[y1:y2, x1:x2, :]
print(a)
A_img = Image.fromarray(a)

printing

[[[20 21 67]]]

Pillow can load that array without a problem.

If x2 and y2 are the same as x1 and y1, then there is the error.

from PIL import Image
import numpy

x1, x2 = 1, 1
y1, y2 = 1, 1
im = Image.open('Tests/images/hopper.jpg')
A_img = numpy.asarray(im)

a = A_img[y1:y2, x1:x2, :]
print(a)
A_img = Image.fromarray(a)

printing

[]

If x2 and y2 are greater than x1 and y1, then there is the error.

from PIL import Image
import numpy

x1, x2 = 2, 1
y1, y2 = 2, 1
im = Image.open('Tests/images/hopper.jpg')
A_img = numpy.asarray(im)

a = A_img[y1:y2, x1:x2, :]
print(a)
A_img = Image.fromarray(a)

printing

[]

So in those last two examples, Pillow is being passed an empty array. Unless you'd like Pillow to show a (0, 0) image, there's nothing to do, as Pillow isn't being given any image data.

If you're not using the same code as this issue, then please create a new issue with a self-contained example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants