# -*- coding: utf-8 -*-
"""
Created on Thu Feb 14 22:12:00 2019
@author: Administrator
"""
import os
from PIL import Image
import pytesseract
import time
while True:
path="F:\\PythonWebPYCode\\mma\\static"
def get_file_list(file_path):
dir_list = os.listdir(file_path)
if not dir_list:
return
else:
# 注意,这里使用lambda表达式,将文件按照最后修改时间顺序升序排列
# os.path.getmtime() 函数是获取文件最后修改时间
# os.path.getctime() 函数是获取文件最后创建时间
dir_list = sorted(dir_list, key=lambda x: os.path.getctime(os.path.join(file_path, x)))
# print(dir_list)
return dir_list
listS = get_file_list(path)
print("all files")
print(listS)
#上面都是导包,只需要下面这一行就能实现图片文字识别
#text=pytesseract.image_to_string(Image.open('333.jpeg'),lang='chi_sim')
I=[]
II=[]
for s in listS:
if 'txt' in s:
II.append(s)
continue
if 'jpeg' in s:
I.append(s)
print("file name")
print(I)
print(II)
num=len(II)-len(I)
if num==0:
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
time.sleep(1)
else:
Nnum=num
print(num)
print("How mash times....")
print("%%%%%%%%%%%%%%%%%%%%%")
print(len(I))
print(len(II))
print("%%%%%%%%%%%%%%%%%%%%%")
for n in range(len(I)-len(II)):
print(n)
print("translation")
print(num)
FilePath=os.path.join(path,I[num])
print(FilePath)
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
NewFile=FilePath+'.txt'
print(NewFile)
num=num+1
text=pytesseract.image_to_string(Image.open(FilePath),lang='chi_sim')
with open(NewFile,"w",encoding='utf-8')as f:
f.write(text)
f.close()
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[′1.jpeg′,′2.jpeg′,′3.jpeg′,′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]filename[′1.jpeg′,′2.jpeg′,′3.jpeg′][′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[′1.jpeg′,′2.jpeg′,′3.jpeg′,′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]filename[′1.jpeg′,′2.jpeg′,′3.jpeg′][′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[′1.jpeg′,′2.jpeg′,′3.jpeg′,′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]filename[′1.jpeg′,′2.jpeg′,′3.jpeg′][′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[′1.jpeg′,′2.jpeg′,′3.jpeg′,′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]filename[′1.jpeg′,′2.jpeg′,′3.jpeg′][′1.jpeg.txt′,′2.jpeg.txt′,′3.jpeg.txt′]$$$$$$$$$$$$$$$$$$$$$$$$$$$$$