from PIL import Image import pytesseract path = r'D:\spyder\img\chinese.png' # cong = r'--psm 10' text = pytesseract.image_to_string(Image.open(path),lang="chi_sim") print(text) 以上代码什么意思
时间: 2024-04-09 19:30:54 浏览: 85
以上代码是使用Python的PIL库和pytesseract库进行图像文字识别的示例代码。
首先,导入了`Image`模块和`pytesseract`模块。
然后,定义了一个变量`path`,它存储了待识别图像的路径。
接下来,使用`Image.open()`函数打开图像文件,并使用`pytesseract.image_to_string()`函数将图像中的文字识别为字符串。`lang="chi_sim"`参数指定了要识别的文字是中文(简体)。
最后,将识别结果打印输出。
总体来说,以上代码的作用是读取一张图片,并使用Tesseract OCR引擎对其中的中文文字进行识别,并将识别结果打印输出。
相关问题
修改代码:import os from PIL import Image import glob import numpy as np # 遍历文件夹 folder_path = 'E:/机器学习/helefull/labels' folders = os.listdir(folder_path) # print(folders) for filename in glob.glob(r'E:/机器学习/helefull/labels/*.png'): img=Image.open(filename).convert("RGB") # images=np.asarray(img) # print(images) # 只处理其中的20个文件夹 folder for folder in folders[:20]: folder_full_path = os.path.join(folder_path, folder) # print(folder_full_path) if os.path.isdir(folder_full_path): images = os.listdir(folder_full_path) print(images) blank_img = Image.new('RGB', (417, 354), (0, 0, 0)) for i,image_name in images: # 打开当前图片 img_path = os.path.join(folder_full_path, image_name) img = Image.open(img_path) # 遍历每一个像素点 for x in range(img.width): for y in range(img.height): # 如果当前像素点值为255,则将该像素点在空白图片上标记为i+1 if img.getpixel((x, y)) == 255: blank_img.putpixel((x, y), i+100) blank_img.save(f'new_{folder}.png')
import os
from PIL import Image
import glob
import numpy as np
# 遍历文件夹
folder_path = 'E:/机器学习/helefull/labels'
folders = os.listdir(folder_path)
for filename in glob.glob(r'E:/机器学习/helefull/labels/*.png'):
img = Image.open(filename).convert("RGB")
# images=np.asarray(img)
# print(images)
# 只处理其中的20个文件夹
for folder in folders[:20]:
folder_full_path = os.path.join(folder_path, folder)
# print(folder_full_path)
if os.path.isdir(folder_full_path):
images = os.listdir(folder_full_path)
print(images)
blank_img = Image.new('RGB', (417, 354), (0, 0, 0))
for i, image_name in enumerate(images):
# 打开当前图片
img_path = os.path.join(folder_full_path, image_name)
img = Image.open(img_path)
# 遍历每一个像素点
for x in range(img.width):
for y in range(img.height):
# 如果当前像素点值为255,则将该像素点在空白图片上标记为i+1
if img.getpixel((x, y)) == 255:
blank_img.putpixel((x, y), i+100)
blank_img.save(f'new_{folder}.png')
改正代码:import os from PIL import Image import glob import numpy as np # 遍历文件夹 folder_path = 'E:/机器学习/helefull/labels' folders = os.listdir(folder_path) # print(folders) for filename in glob.glob(r'E:/机器学习/helefull/labels/*.png'): img=Image.open(filename).convert("RGB") # images=np.asarray(img) # print(images) # 只处理其中的20个文件夹 folder for folder in folders[:20]: folder_full_path = os.path.join(folder_path, folder) # print(folder_full_path) if os.path.isdir(folder_full_path): images = os.listdir(folder_full_path) print(images) blank_img = Image.new('RGB', (417, 354), (0, 0, 0)) for image_name in images: # 打开当前图片 img_path = os.path.join(folder_full_path, image_name) img = Image.open(img_path) # 遍历每一个像素点 for x in range(img.width): for y in range(img.height): # 如果当前像素点值为255,则将该像素点在空白图片上标记为i+1 if img.getpixel((x, y)) == 255: blank_img.putpixel((x, y), int(image_name.split('.')[0])+100) blank_img.save(f'new_{folder}.png')
import
from PIL import Image
import glob
import numpy as np
# 遍历文件夹
folder_path = 'E:/机器学习/helefull/labels'
folders = os.listdir(folder_path)
for filename in glob.glob(r'E:/机器学习/helefull/labels/*.png'):
img = Image.open(filename).convert("RGB")
# images=np.asarray(img)
# print(images)
# 只处理其中的20个文件夹
for folder in folders[:20]:
folder_full_path = os.path.join(folder_path, folder)
# print(folder_full_path)
if os.path.isdir(folder_full_path):
images = os.listdir(folder_full_path)
print(images)
# 创建一个空白图片
blank_img = Image.new('RGB', (417, 354), (0, 0, 0))
for image_name in images:
# 打开当前图片
img_path = os.path.join(folder_full_path, image_name)
img = Image.open(img_path)
# 遍历每一个像素点
for x in range(img.width):
for y in range(img.height):
# 如果当前像素点值为255,则将该像素点在空白图片上标记为i+1
if img.getpixel((x, y)) == (255, 255, 255):
blank_img.putpixel((x, y), int(image_name.split('.')[0])+100)
blank_img.save(f'new_{folder}.png')
阅读全文