Skip to content

Instantly share code, notes, and snippets.

@santhalakshminarayana
Created September 27, 2019 14:33
Show Gist options
  • Save santhalakshminarayana/cb27dc5861cbea34726004c775d627a3 to your computer and use it in GitHub Desktop.
Save santhalakshminarayana/cb27dc5861cbea34726004c775d627a3 to your computer and use it in GitHub Desktop.
Medium:Imagetopdf Load images and convert to gray scale
files_in_dir=os.listdir()
curr_path=os.getcwd()
#get image file names in current directory
image_names=[]
conventions=['jpeg','png','jpg']
for file in files_in_dir:
ext=file.split('.')[-1]
if ext in conventions:
image_names.insert(0,file)
#Read images into opencv numpy arrays
images_read=[]
for name in image_names:
img=cv2.imread(name)
images_read.insert(0,img)
#Convert RGB images to Gray Scale
thsh_images=[]
for img in images_read:
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
clahe=cv2.createCLAHE(clipLimit=4.0,tileGridSize=(16,16))
img_gray=clahe.apply(img_gray)
ret,th=cv2.threshold(img_gray,130,255,cv2.THRESH_BINARY)
thsh_images.append(th)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment