fork download
  1. def get_patches(filepath,patch_size,crop_sizes,save_dir=None):
  2. '''This functions creates and return patches of given image with a specified patch_size, if save_dir is given patches are saved to the directory'''
  3. image = cv2.imread(filepath)
  4. filename=filepath.split('/')[-1].split('.')[0]
  5. height, width , channels= image.shape
  6. patches = []
  7. for crop_size in crop_sizes: #We will crop the image to different sizes
  8. crop_h, crop_w = int(height*crop_size),int(width*crop_size)
  9. image_scaled = cv2.resize(image, (crop_w,crop_h), interpolation=cv2.INTER_CUBIC)
  10. for i in range(0, crop_h-patch_size+1, patch_size):
  11. for j in range(0, crop_w-patch_size+1, patch_size):
  12. x = image_scaled[i:i+patch_size, j:j+patch_size] # This gets the patch from the original image with size patch_size x patch_size
  13.  
  14. if save_dir!=None: #If save_dir is specified we will save the patches to the given directory
  15. if not os.path.exists(save_dir):
  16. os.mkdir(save_dir)
  17. pactch_filepath=save_dir+'/'+filename+''+str(crop_h)+''+str(i)+'_'+str(j)+'.jpg'# Creating a distinct patch filename
  18. cv2.imwrite(pactch_filepath,x) # Saving the image to the path
  19.  
  20. patches.append(x)
  21. return patches
Success #stdin #stdout 0.04s 9544KB
stdin
https://i...content-available-to-author-only...o.gl/M2Qvcm3K4D9F2t4a7
stdout
Standard output is empty