I have an image and I want to split it into multiple images using vertical and horizontal strides like a sliding window and the resultant images will all be of same resolution. How can I do that efficiently in Python? I have done this much:
from PIL import Image
def sliding_window(image, stride, imgSize):
width, height = image.size
img = []
for y in range(0, height-imgSize, stride):
for x in range(0, width-imgSize, stride):
# Setting the points for cropped image
left = x
top = y
right = x + imgSize
bottom = y + imgSize
im1 = image.crop((left, top, right, bottom))
img.append(im1)
return img
file = "/home/xxxxxx/yyyyyy.png"
im = Image.open(file)
img = sliding_window(im, 1, 838) # Strides of 1 takes too much time
but this code requires too much RAM and is too time consuming. Please help.
Example :
Sample code : img = sliding_window(im, 200, 300)
The following image is of 800*800 size.
Output :

