Skip to content

Instantly share code, notes, and snippets.

@suyashdamle
Created February 17, 2019 07:31
Show Gist options
  • Save suyashdamle/403a66eb8be4986830fa5fd206fe9700 to your computer and use it in GitHub Desktop.
Save suyashdamle/403a66eb8be4986830fa5fd206fe9700 to your computer and use it in GitHub Desktop.
Trim images in bulk using opencv to pre-defined dimensions (implemented for grayscale images)
import os
import numpy as np
import cv2
from scipy.misc import imsave
DIR = './'
LEFT=190
RIGHT=1246
TOP=0
BOTTOM=590
for file_name in os.listdir(DIR):
if '.png' not in file_name:
continue
img=cv2.imread(DIR+file_name,cv2.IMREAD_GRAYSCALE)
img=img[TOP:BOTTOM,LEFT:RIGHT]
imsave('new_'+file_name,img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment