CamVid Lesson 3 Question (How to extract patches)

I am using the classic CamVid dataset (http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamSeq01/) for object segmentation. It is basically a dataset with videoframes (think of it just as images) and their corresponding segmentation mask.

I would like now to try something else: I would like to crop each frame and its segmentation mask in e.g. 100 patches centered at random positions from the image with a given size (e.g. 224*224) and save the results in the corresponding image / label folders. Note that both the image and the label have to be cropped in the same way for it to work. Does anybody know how to achieve this with few lines of code?

I finally found how to do it with pytorch

# create custom class transform
class RRC(transforms.RandomResizedCrop):
    def __call__(self, img1, img2):
        assert img1.size == img2.size
        # fix parameter
        i, j, h, w = self.get_params(img1, self.scale, self.ratio)
        # return the image with the same transformation
        return [tvF.resized_crop(img1, i, j, h, w, self.size, self.interpolation), tvF.resized_crop(img2, i, j, h, w, self.size, self.interpolation)]

imgInput = Image.open(fnames[1])
imshow(imgInput)
imgTarget = Image.open(get_y_fn(fnames[1]))

outputSize = (224, 224)
imgInput, imgTarget = RRC(size=outputSize)(imgInput, imgTarget)