I\'m trying to split a photo into multiple pieces using PIL.
def crop(Path,input,height,width,i,k,x,y,page):
im = Image.open(input)
imgwidth = im.siz
crop would be a more reusable
function if you separate the
cropping code from the
image saving
code. It would also make the call
signature simpler.im.crop returns a
Image._ImageCrop instance. Such
instances do not have a save method.
Instead, you must paste the
Image._ImageCrop instance onto a
new Image.Imageheight-2 and not
height? for example. Why stop at
imgheight-(height/2)?).So, you might try instead something like this:
import Image
import os
def crop(infile,height,width):
im = Image.open(infile)
imgwidth, imgheight = im.size
for i in range(imgheight//height):
for j in range(imgwidth//width):
box = (j*width, i*height, (j+1)*width, (i+1)*height)
yield im.crop(box)
if __name__=='__main__':
infile=...
height=...
width=...
start_num=...
for k,piece in enumerate(crop(infile,height,width),start_num):
img=Image.new('RGB', (height,width), 255)
img.paste(piece)
path=os.path.join('/tmp',"IMG-%s.png" % k)
img.save(path)