"SystemError: tile cannot extend outside image" in PIL during save image

37,782

Solution 1

With reference to the comments, the error occurred due to improper passing of the coordinates to PIL's crop() function.

As mentioned in the documentation, the function returns an image having taken in a tuple of four (x, y, width and height).

In the given text file the y coordinate is mentioned in the first column and x coordinate in the second column. The crop() function however accepts the value of x coordinate as the first parameter and the y coordinate as the second parameter.

The same applies for OpenCV as well

Here is ANOTHER POST regarding the same.

Solution 2

The mentioned way on the internet is like this:

imageScreenshot.crop((x, y, width, height))

But the correct way is this:

imageScreenshot.crop((x, y, x + width, y + height))

Meaning that you should add the x to the width and y to the height.
This is a simple example (driver is for python selenium):

def screenShotPart(x, y, width, height) -> str:
    screenshotBytes = driver.get_screenshot_as_png()
    imageScreenshot = Image.open(BytesIO(screenshotBytes))
    imageScreenshot = imageScreenshot.crop((x, y, x + width, y + height))
    imagePath = pathPrefix + "_____temp_" + str(time.time()).replace(".", "") + ".png"
    imageScreenshot.save(imagePath)

Hope it helps.

Solution 3

In my case the issue was that I was specifying start and end coordinates where the start X and start Y were not always less than the end X and Y. You cannot do this.

For example,

Start: (0, 50) End: (50, 0)

These coordinates make sense to me, but should actually be specified as:

Start: (0, 0) End: (50, 50)

Visually the same rectangle, but the latter is required for Pillow to crop.

Share:
37,782
Sudip Das
Author by

Sudip Das

01001000 01101111 01110111 00100000 01101111 01101110 01100101 00100000 01100011 01100001 01101110 00100000 01110011 01100001 01111001 00100000 01100001 01100010 01101111 01110101 01110100 00100000 01101000 01101001 01101101 01110011 01100101 01101100 01100110 0111111 Try to Understand??? :P

Updated on November 04, 2021

Comments

  • Sudip Das
    Sudip Das over 2 years

    I have this Image =>

    enter image description here

    here is, all coordinates of above yellow boxes that is written in 3.txt file.

    #Y   X Height     Width 
    
    46 135 158 118 
    46 281 163 104 
    67 494 188 83 
    70 372 194 101 
    94 591 207 98 
    252 132 238 123 
    267 278 189 105 
    320 741 69 141 
    322 494 300 135 
    323 389 390 124 
    380 726 299 157 
    392 621 299 108 
    449 312 227 93 
    481 161 425 150 
    678 627 285 91 
    884 13 650 437 
    978 731 567 158 
    983 692 60 43 
    1402 13 157 114 
    

    My intension is to crop those boxes and save all boxes as Image. I have written a code for that but getting error.

    Here is my code =>

    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np
    from os import listdir
    #from scipy.misc import imsave
    
    ARR = np.empty([1,4])
    # print(ARR)
    
    i = 0
    k = 0
    img = Image.open('3.png')
    
    fo = open("3.txt", "r")
    for line in fo:
        if not line.startswith('#'):
            for word in line.split():
    
                ARR[0][i] = int(word)
                print(int(word))
                # ARR[0][i] = int(word)
                i = i +1
    
        img2 = img.crop((int(ARR[0][1]), int(ARR[0][0]), int(ARR[0][0] + ARR[0][2]), int(ARR[0][1] + ARR[0][3])))
        name = "new-img" + str(k) + ".png"
        img2.save(name)
        k = k + 1
        i = 0
    

    I am getting these error =>

    Traceback (most recent call last): File "reshape.py", line 26, in img2.save(name) File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1468, in save save_handler(self, fp, filename) File "/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 624, in _save ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)]) File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 462, in _save e.setimage(im.im, b) SystemError: tile cannot extend outside image

    How do I fix these?

  • Max Kuchenkiller
    Max Kuchenkiller over 4 years
    Pointing out that for me AmirHosseins answer actually was the correct one. The parameters are imageScreenshot.crop((x, y, x + width, y + height)) and not imageScreenshot.crop((x, y, width, height))
  • kmario23
    kmario23 over 2 years
    The Pillow doc says both are correct, although I found the above answer to be working fine.
  • Can
    Can over 2 years
    I could not see such information in the corresponding document. It says "four coordinates; left, upper, right, lower". It means the function takes four coordinates of the region. Last two parameters are not width and height. Instead, x+width and y+height as explained in the document. Maybe people get consued because of other functions that take (x,y,width,height) (such as ImageDraw.rectangle).