Python 本机图片扫描

'''
scanimage.py by 郑瑞国
'''
import numpy as np
import cv2
import os
import string
 
def get_disklist():
    disk_list = []
    for c in string.ascii_uppercase:
        disk = c+':'
        if os.path.isdir(disk):
            disk_list.append(disk)
    return disk_list
 
def scan_image(disk_list):
    cv2.namedWindow('image',cv2.WINDOW_NORMAL)
    for disk in disk_list:
        os.chdir(disk+'/')
        tree = os.walk(disk)
        for dir in tree:
            for file in dir[2]:
                exname = os.path.splitext(file)
                if '.bmp' in exname or '.jpg'in exname or '.jpeg' in exname or '.png' in exname:
                    try:
                        img = cv2.imdecode(np.fromfile(dir[0]+'\\'+file,dtype=np.uint8),cv2.IMREAD_COLOR)                        
                        if img.shape[0]>1250:
                            print(dir[0]+'\\'+file,img.shape)
                            cv2.imshow('image',img)
                            cv2.waitKey(20)
                    except:
                        pass
 
if __name__ == '__main__':
    scan_image(get_disklist())
'''
scanimage2.py by 郑瑞国
空格:单步,回车:快速,Esc:退出,Backspace:常速
'''
import numpy as np
import cv2
import os
import string
 
def get_disklist():
    disk_list = []
    for c in string.ascii_uppercase:
        disk = c+':'
        if os.path.isdir(disk):
            disk_list.append(disk)
    return disk_list
 
def scan_image(disk_list):
    cv2.namedWindow('image',cv2.WINDOW_NORMAL)
    speed = 2000
    for disk in disk_list:
        os.chdir(disk+'/')
        tree = os.walk(disk)
        for dir in tree:
            for file in dir[2]:
                exname = os.path.splitext(file)
                if '.bmp' in exname or '.jpg'in exname or '.jpeg' in exname or '.png' in exname:
                    try:
                        img = cv2.imdecode(np.fromfile(dir[0]+'\\'+file,dtype=np.uint8),cv2.IMREAD_COLOR)                        
                        if img.shape[0]>1250:
                            print(dir[0]+'\\'+file,img.shape)
                            cv2.imshow('image',img)
                            key = cv2.waitKey(speed)
                            if key == 32:             #press space key then step by step
                                speed = 0
                            elif key == 27:           #press Esc key then close window and return
                                cv2.destroyAllWindows()
                                return 0
                            elif key == 13:           #press Enter key then max speed
                                speed = 1
                            elif key == 8:           #press Backspace key then normal speed
                                speed = 2000       
                            else:
                                pass
                    except:
                        pass
 
if __name__ == '__main__':
    scan_image(get_disklist())

 

你可能感兴趣的:(Python,image,picture,scan,cv2)