# Image Grayscale (tesseract OCR)

**URL:** <https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142>\
**Category:** Python\
**Tags:** ocr, tesseract\
**Created:** [August 5, 2023, 4:15am UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142 "2023-08-05T04:15:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![walmeida](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/walmeida/32/8450_2.png) [@walmeida](https://forum.opencv.org/u/walmeida)\
**Post date:** [August 5, 2023, 4:15am UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/1 "2023-08-05T04:15:31Z")

</div>

Hi guys! I am trying extracts text from a screenshot in memory using pytesseract without read/write file on disk.

this is my screenshot:  
 ![Screenshot](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/207ab3f398a113ba9cd1cad7563db4ac319f0b0a.png)

so, take a look two grayscale images between cvtColor and imread we see that diferents.  
from gray = cv2.cvtColor(img, cv2.COLOR\_BGR2GRAY) my threash  
limiar, imgThreash = cv2.threshold(gray, 127, 255, cv2.THRESH\_BINARY\_INV + cv2.THRESH\_OTSU) does not work. Someone has any idea how ca i by pass this to achieve threash screenshot without save in file before to get it from imread?

PS: I did try upload all other image examples but the website block me because Im newer user!

Best regards!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 5, 2023, 2:05pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/4 "2023-08-05T14:05:28Z")

</div>

Dima\_Fantasy was an AI spam bot. do not trust anything the AI spam bot posted.

---

<div class="post-metadata">

**Author:** ![walmeida](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/walmeida/32/8450_2.png) [@walmeida](https://forum.opencv.org/u/walmeida)\
**Post date:** [August 5, 2023, 2:11pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/5 "2023-08-05T14:11:22Z")

</div>

@crackwitz, sorry about that. I did not see that.  
thanks.

---

<div class="post-metadata">

**Author:** ![walmeida](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/walmeida/32/8450_2.png) [@walmeida](https://forum.opencv.org/u/walmeida)\
**Post date:** [August 6, 2023, 12:57pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/6 "2023-08-06T12:57:25Z")

</div>

I finally found a solution to my problem.

Follows the code:

```auto
def ThresholdFromScreenShot(tupleCoordenates):

pixels = np.array(ImageGrab.grab(bbox=tupleCoordenates))

gray_f = np.array(Image.fromarray(pixels).convert('L'))

limiar, imgThreash = cv2.threshold(gray_f, 127, 255, 
cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

gray_s = np.array(Image.fromarray(imgThreash).convert('L'))
            
blur = cv2.blur(gray_s,(3,3))

limiar,thresh = cv2.threshold(blur,240,255,cv2.THRESH_BINARY)
    
return thresh

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 6, 2023, 2:23pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/7 "2023-08-06T14:23:26Z")

</div>

> [@walmeida](#):
>
> ```auto
> gray_f = np.array(Image.fromarray(pixels).convert('L'))
> 
> ```

that is equivalent to using `cv.cvtColor()`

also… you use ImageGrab already. why convert to numpy array and then back to PIL Image? you could just call `convert("L")` directly on that

> [@walmeida](#):
>
> ```auto
> gray_s = np.array(Image.fromarray(imgThreash).convert('L'))
> 
> ```

that seems entirely superfluous

and those _**two threshold() calls**_ could be just one, followed by a the “dilation” morphology operation

---

<div class="post-metadata">

**Author:** ![walmeida](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/walmeida/32/8450_2.png) [@walmeida](https://forum.opencv.org/u/walmeida)\
**Post date:** [August 6, 2023, 3:23pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/8 "2023-08-06T15:23:50Z")

</div>

Hi @crackwitz, I’m newer programmer python and opencv, but the grayscale result from cvtcolor give me a result from red color more close of gray and np.array(Image.fromarray(pixels).convert(‘L’)) give me a result from red color more close of white! In the first case pytesseracts has no effect on the text extraction.

Thanks good!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 6, 2023, 9:22pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/9 "2023-08-06T21:22:32Z")

</div>

[crosspost](https://meta.stackoverflow.com/a/266159):

> <https://stackoverflow.com/questions/76830929/how-can-i-binarize-an-image-and-extract-a-text-in-memory-without-save-file-on-d>

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 6, 2023, 9:22pm UTC](https://forum.opencv.org/t/image-grayscale-tesseract-ocr/14142/10 "2023-08-06T21:22:56Z")

</div>


