Search code examples
pythonopencvtransparencyalphaimread

OpenCV imread transparency gone


I have an image (a captcha) that I download from the web.

Initial Image

When I loaded to opencv it seems to loose its properties or simply mixes the transparent background with the dark/black colors:

Processed Image

Currently the code does nothing but loading a writing again:

captchaImg = cv2.imread('captcha1.png')
cv2.imwrite("captcha2.png", captchaImg)

I have tried loading also with options 0, 1, 2, 3 but the result is the same.


Solution

  • Using the provided constants might help. I do the equivalent of

    captchaImg = cv2.imread('captcha1.png', cv2.IMREAD_UNCHANGED)
    

    which reads the alpha channel (if there is one). The REPL says that cv2.IMREAD_UNCHANGED is -1