defimage_slice(path,prefix,outdir,slice_size):"""slice an image into parts slice_size tall"""img=Image.open(path)width,height=img.sizeupper=0left=0slices=int(math.ceil(height/slice_size))imgs=[]count=1forsliceinrange(slices):#ifweareattheend,setthelowerboutobethebottomoftheimageifcount==slices:lower=heightelse:lower=int(count*slice_size)bbox=(left,upper,width,lower)working_slice=img.crop(bbox)upper+=slice_size#savetheslicefull_path=os.path.join(outdir,"slice_"+prefix+"_"+str(count)+".png")working_slice.save(full_path)count+=1imgs.append(full_path)returnimgs
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.