Search code examples
pythondiscord.pyopencvimage-recognitionpython-tesseract

Pytesseract detecting scrambled words


I have a simple pytesseract script that runs inside a discord bot to detect text from an image. However when given this image, it returns ['ESC es Sum Ls a ns ay', 'on', '', 'Sa eon', '', 'Lape een ne eeren eee eserees', '', 'omeereer ee ate erence ecco at arte', '', 'Ue te eect eet rac contac', '', ' ', '', 'ree Cee ed', 'ema eect eens', '\x0c'] My code is

im = cv2.imread(attachment.filename)
            config = ('-l eng --oem 1 --psm 3')
            text = pytesseract.image_to_string(im, config=config)
            text = text.split('\n')

Solution

  • Thanks to barny for this answer but what I did was

                image = Image.open(attachment.filename)
                if image.mode == 'RGBA':
                    r, g, b, a = image.split()
                    rgb_image = Image.merge('RGB', (r, g, b))
    
                    inverted_image = PIL.ImageOps.invert(rgb_image)
    
                    r2, g2, b2 = inverted_image.split()
    
                    final_transparent_image = Image.merge('RGBA', (r2, g2, b2, a))
    
                    final_transparent_image.save(attachment.filename)
    
                else:
                    inverted_image = PIL.ImageOps.invert(image)
                    inverted_image.save(attachment.filename)
                im = cv2.imread(attachment.filename)
                text = pytesseract.image_to_string(im)
    

    Which basically inverts the colors/colours and changes it into RGBA. I got perfect readings from this!