title: Why am I getting tile cannot extend outside image error when trying to split image in half
body: <p>My program is supposed to take an image and split it vertically into n sections, then save the sections as individual png files. It should look something like this for 2 sections</p>
  
  <p><a href="https://i.stack.imgur.com/TD4oe.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TD4oe.png" alt="enter image description here"></a></p>
  
  <p>I'm having problems right now, what I'm getting is the first half of my image get saved properly, and then I'm getting the following error when it tries to crop the second half:
  <code>SystemError: tile cannot extend outside image</code></p>
  
  <p>The image I'm working with has</p>
  
  <ul>
  <li>Width: 1180px</li>
  <li>Height: 842px</li>
  </ul>
  
  <p>The rectangles it calculates to crop is:</p>
  
  <ul>
  <li><code>(0.0, 0, 590.0, 842)</code> - this works properly</li>
  <li><code>(590.0, 0, 590.0, 842)</code> - this crashes the program</li>
  </ul>
  
  <p>My questions is: Why is this sub rectangle out of bounds and how can I fix it to properly slice my image in half like shown in picture? </p>
  
  <pre><code>from PIL import Image, ImageFilter
  
  
  im = Image.open("image.png")
  width, height = im.size
  numberOfSplits = 2
  splitDist = width / numberOfSplits #how many pixels each crop should be in width
  print(width, height) #prints 1180, 842
  
  for i in range(0, numberOfSplits):
      x = splitDist * i
      y = 0
      w = splitDist
      h = height
  
      print(x, y, w, h)
      #first run through prints 0.0, 0, 590.0, 842
      #second run through prints 590.0, 0, 590.0, 842 then crashes
  
      croppedImg = im.crop((x,y,w,h)) #crop the rectangle into my x,y,w,h
      croppedImg.save("images\\new-img" + str(i) + ".png") #save to file
  </code></pre>