if name != "original": padding = 0.1 else: padding = 0 padding = 0.2 ncols,nrows = dmtx_im.size padw = (ncols)*padding padh = (nrows)*padding isize = (round(ncols+2*padw),round(nrows+2*padh))#cols*photow+padw,nrows*photoh+padho) # Create the new image. The background doesn't have to be white dmtx_image = Image.new('RGB',isize,0) bbox = (round(padw),round(padh),ncols+round(padw),nrows+round(padh)) dmtx_image.paste(dmtx_im,bbox) # dmtx_im.save("/tmp/t1.png") # dmtx_image.save("/tmp/t2.png") # import pdb;pdb.set_trace() (width, height) = dmtx_image.size if findcode.low_res: dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10) else: dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10, shrink = 2) #dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10, shrink = SHRINK)
def parseImages(self, files = None): if files != None: self.files = files n=0 m=0 failNum=0 lastCVTime = 0 timeForCV = 0 print "\nFiles to decode: ",len(self.files) stop = False # import pdb;pdb.set_trace() for filename in self.files: is_found = False # if filename.find('/') != -1: # self.myDir,filename = path.split(filename) # self.myDir += '/' lastCVTime = time() cv_orig,cv_smoo,cv_final = findcode.findAndOrient(self.myDir, filename, self.do_display, self.verbose) timeForCV += (time() - lastCVTime) cv.SaveImage(self.myDir+filename.replace('tif','jpg'),cv_final) test = cv.Avg(cv_final) if stop == True: pdb.set_trace() if test[0] < 130 and test[0] > 40: # hard threshold works for avision for img,name in [#[cv_smoo,"smooth"], #seems to introduce many errors [cv_final,"clipped"], [cv_orig,"original"]]: if is_found: break dmtx_im = Image.fromstring("L", cv.GetSize(img), img.tostring()) if name != "original": padding = 0.1 else: padding = 0 ncols,nrows = dmtx_im.size padw = (ncols)*padding padh = (nrows)*padding isize = (int(round(ncols+2*padw)),int(round(nrows+2*padh))) # Create a new color image onto which we can paste findcode output dmtx_image = Image.new('RGB',isize,0) bbox = (round(padw),round(padh),ncols+round(padw),nrows+round(padh)) dmtx_image.paste(dmtx_im,map(int,bbox)) (width, height) = dmtx_image.size # Send to datamatrix library if findcode.low_res: dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10) else: dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10, shrink = 2) #dm_read = DataMatrix(max_count = 1, timeout = 300, shape = DataMatrix.DmtxSymbol12x12, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10) dmtx_code = dm_read.decode (width, height, buffer(dmtx_image.tostring())) if dmtx_code is not None and len(dmtx_code) == EXPECTED_LEN: how = "Quick Search: "+str(name) is_found = True out = dmtx_code if not is_found: self.failed.append(filename) else: failNum+=1 if is_found: n+=1 self.output[filename] = out #print filename, out, test[0] else: #print filename, None, test[0] pass #self.failed.append(filename) m+=1 print failNum, "failed to produce images worth decoding" print n,"of the",m-failNum,"remaining were successfully decoded." self.status += "\nFound %d of %d in "%(n,m) self.status += str(time()-self.totalTime)+" seconds.\n" self.status += "(OpenCV: "+str(timeForCV)+" sec)\n" if not(len(self.failed) == 0):# and verbose: dirr = ' '+self.myDir self.status+= "\nmissing: "+str(self.failed)+'\n' return self.output,self.failed,self.status