如何在Python中读取图像中文本的颜色

如何在Python中读取图像中文本的颜色,python,ocr,Python,Ocr,我正在建立一个项目,可以从图像中读取文本。我还需要确定这个文本是用什么颜色写的。图像由计算机生成,并且总是由数字组成。 我正在使用PyteSeract进行OCR检测。有人能建议我怎么做吗 比如说,我需要python代码中的信息,比如429.05绿色 我的代码如下 import pytesseract import cv2 pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesser

我正在建立一个项目,可以从图像中读取文本。我还需要确定这个文本是用什么颜色写的。图像由计算机生成,并且总是由数字组成。 我正在使用PyteSeract进行OCR检测。有人能建议我怎么做吗

比如说,我需要python代码中的信息,比如429.05绿色

我的代码如下

import pytesseract
import cv2

pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img = cv2.imread("D:\\test2.png")
text = pytesseract.image_to_string(img)

print(text)

这可以通过枕头库来完成

首先导入所需的库,并使用
getcolors
方法获取颜色托盘,按像素计数升序排序

from PIL import Image
i = Image.open("D:\\test2.png")

colors = sorted(i.getcolors())
对于您的图像,
colors
现在是一个元组列表,其中每个元组中的第一项是包含所述颜色的像素数,第二项是指示RGB颜色代码的另一个元组

列表中的最后一项是像素最多的项目(白色):

最后一种可能是您想要的颜色:

>>> colors[-2]
(175, (76, 175, 80))
这可以是:

并使用基于web的十六进制选择器快速确认:


这对于您的测试图像来说是正确的,但是如果您正在处理的图像不同,您可能需要稍微调整一下。

感谢所有人的支持。我裁剪了包含第一个字母的图像,然后按照@v25的建议应用步骤。下面是代码

import pytesseract
from PIL import Image


pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img = Image.open("D:\\test1.png")

text = pytesseract.image_to_boxes(img).split(" ")
(left, upper, right, lower) = (int(text[1]),int(text[2])-8,int(text[3]),int(text[4])+8)
im_crop = img.crop((left, upper, right, lower))
colors = sorted(im_crop.getcolors())
hex = ('#%02x%02x%02x' % colors[-2][1])
color = None
if (hex == '#91949a'):
    color = "Black"
elif ( hex == '#4caf50'):
    color = "Green"
elif ( hex == '#ff9d9d'):
color= "Red"
number = pytesseract.image_to_string(img)
print("Number is: "+number+" Color is: "+color)

429.05的单位是什么。它是示例图像中的文本。您可以使用网络颜色获取颜色。参考:Ouze,您能用python示例演示一下吗。图像将只包含数字,最大可能的颜色是绿色、红色和黑色,并且一个图像的颜色太单一请求库/软件对于StackOverflow来说是离题的。
>>> '#%02x%02x%02x' % colors[-2][1]
'#4caf50'
import pytesseract
from PIL import Image


pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img = Image.open("D:\\test1.png")

text = pytesseract.image_to_boxes(img).split(" ")
(left, upper, right, lower) = (int(text[1]),int(text[2])-8,int(text[3]),int(text[4])+8)
im_crop = img.crop((left, upper, right, lower))
colors = sorted(im_crop.getcolors())
hex = ('#%02x%02x%02x' % colors[-2][1])
color = None
if (hex == '#91949a'):
    color = "Black"
elif ( hex == '#4caf50'):
    color = "Green"
elif ( hex == '#ff9d9d'):
color= "Red"
number = pytesseract.image_to_string(img)
print("Number is: "+number+" Color is: "+color)