import cv2 import pytesseract import os, os.path pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe' #pytesseract needs to be installed seperately NumFiles = (len([name for name in os.listdir('E:/Lichens/2020_09_21 3/doubles/Export') if os.path.isfile(os.path.join('E:/Lichens/2020_09_03/doubles/Export', name))])) NumSpecimens = NumFiles/2 for x in range(int(NumSpecimens)): #print(x) BarcodeIndex = str(int((2*x)+1)) LichenIndex = str(int(2*x)+2) Date = str(2020_09_14) Barcode = cv2.imread('E:/Lichens/2020_09_21 3/doubles/Export/'+BarcodeIndex+'.TIF') Lichen = cv2.imread('E:/Lichens/2020_09_21 3/doubles/Export/'+LichenIndex+'.TIF') Output = Lichen.copy() crop = Barcode[1450:1850, 75:1100] resized = cv2.resize(crop, (int(1000), int(900))) resized = cv2.cvtColor(resized,cv2.COLOR_RGB2GRAY) #Cropping, recoloring, and stretching the barcode seems to help pytesseract recognize the numbers #cv2.imshow('result', crop) #cv2.waitKey(0) #cv2.imshow('result',resized) imagelabel = (pytesseract.image_to_string(resized)) #imagelabel = imagelabel+"hello" nochars = ''.join(i for i in imagelabel if i.isdigit()) #This removes all non-number characters from the pytesseract output #print(nodigs) Output[1450:1850, 75:1100, :] = Barcode[1450:1850, 75:1100] #This moves the barcode onto the lichen photograph #OutputSmall = cv2.resize(Output, (int(1500), int(1000))) #cv2.imshow(nodigs, OutputSmall) #cv2.waitKey(0) cv2.imwrite('E:/Lichens/2020_09_21 3/doubles/Output/' + str(x) +" " + nochars + '.jpg', Output) cv2.imwrite('E:/Lichens/2020_09_21 3/doubles/Output/' + str(x) +" " + nochars + '.tif', Output) #save as tif and jpg #rotated = cv2.rotate(resized, cv2.cv2.ROTATE_90_CLOCKWISE)