How i can open PNG w alpha channel, if he is white - python

I have a lot images like this:
Screenshot from ImageMagick
When I open it image in cv2, PIL I see only white canvas without any alpha channel
I started looking for the difference between these files, and those who open correctly and in ImageMagick saw this line Alpha: srgba(255,255,255,0) #FFFFFF00 screenshot
Valid files have Alpha: srgba(0,0,0,0) #00000000
File link: https://drive.google.com/file/d/1tN9D1vjwbnHg8urYJzHqI4zljS110oun/view?usp=sharing
Help:)
UPD. The question is closed. Files must be opened with the flag IMREAD_UNCHANGED

I should have written like this
cv2.imread('file.png', cv2.IMREAD_UNCHANGED)

Related

PNG image wrongfully opened as RGB by pil.image

I have images like this, which I believe have 4 data channels (RGB + an alpha channel).
When I try to open this image like so, I can't access the transparency layer.
from PIL import Image
fn = 'FUgqUA5.png'
im = Image.open(fn)
im.mode # returns RGB instead of desired .png
im.getData()[0] # returns (0,0,0) instead of desired (0,0,0,255)
How do I open this image as an actual .png? Is it possible the image is saved as a 3-channel png instead and that this image simply doesn't have a 4th channel?
I saved the image and saw the properties via image editor and it did not mention that there is a 4th channel.
PS: (image editor is ImageMagick)

Image does not change when adding an alpha channel

The pillow package has a method called Image.putalpha() which is used to add or change the alpha channel of an image.
I tried to play with this method and found that I can not change the background color of an image. The original image is
This is my code to add alpha to it
from PIL import Image
im_owl = Image.open("owl.jpg")
alpha = Image.new("L", im_owl.size, 50)
im_owl.putalpha(alpha)
im_owl.show()
The produced image is nothing different from the original image. I have tried with different value of alpha and see no difference.
What could have been wrong?
try to save the image and see it.
I am also not able to see the image directly from
im_owl.show()
but when I saved it
im_owl.save()
I am able to see the image changed.
Try using
im_owl.save("alphadOwl.png")
And then view the saved image. It would seem that the alpha channel isn't applied to bmp or jpg files. It is a bmp file that gets displayed with im.show()
(For the record, I'm on a mac, I don't know if im.show() uses different applications on other devices).
As #sanyam and #Pam have pointed out, we can save the converted image and it shows correctly. This is because on Windows, images are saved as temporary BMP file before they are shown using the system default image viewer, as per the PIL documentation:
Image.show(title=None, command=None)
Displays this image. This method is mainly intended for debugging purposes.
On Unix platforms, this method saves the image to a temporary PPM file, and calls
either the xv utility or the display utility, depending on which one can be found.
On macOS, this method saves the image to a temporary BMP file, and opens it with
the native Preview application.
On Windows, it saves the image to a temporary BMP file, and uses the standard BMP
display utility to show it (usually Paint).
To fix this issue, we can patch the Pillow code to use PNG format as default. First, we need to find the root of Pillow package:
import PIL
print(PIL.__path__)
On my system, the output is:
[’D:\Anaconda\lib\site-packages\PIL’]
Go to this directory and open the file ImageShow.py. I add the following code after the line register(WindowsViewer):
class WindowsPNGViewer(Viewer):
format = "PNG"
def get_command(self, file, **options):
return ('start "Pillow" /WAIT "%s" '
'&& ping -n 2 127.0.0.1 >NUL '
'&& del /f "%s"' % (file, file))
register(WindowsPNGViewer, -1)
After that, I can show the image with alpha channel correctly.
References
https://github.com/python-pillow/Pillow/issues/3695

Different output with pytesseract

Why do I get different output with tesseract and pytesseract?
In tesseract:
tesseract t10.tiff output -1 eng
In python
ocr_text = pytesseract.image_to_string(image, lang='eng', config='-psm 3').
If you look closely at pytesseract.run_tesseract(), you will see that pytesseract runs a subprocess that creates another .PNG file, THEN run tesseract subprocess on that image. I put a python debugger right after the file is created and tried copying the file on disk for inspection. Turned out that the file color profile is different from the original image. Further, the new image has 3 color channels while the original image has an alpha channel. Try running tesseract from command line on this new image and you'll get the same result you get from running pytesseract on the original image.Generated PNG vs Original png

OpenCV imread transparency gone

I have an image (a captcha) that I download from the web.
When I loaded to opencv it seems to loose its properties or simply mixes the transparent background with the dark/black colors:
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.
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
Well this is a problem with opencv and it has a solution with opencv but it is kind of complex so I went on and use another libary (PIL) that I was going to use any way.
Basically what you do is put a white image behind the transparent one an with that you solve the problem.
The code is the following:
image = Image.open("captcha1.png")
image.convert("RGBA")
canvas = Image.new('RGBA', image.size, (255,255,255,255)) # Empty canvas colour (r,g,b,a)
canvas.paste(image, mask=image) # Paste the image onto the canvas, using it's alpha channel as mask
canvas.save("captcha1.png", format="PNG")
I hope it helps someone with the same problem.

PIL Image mode "P" -> "RGBA"

This is my issue:
import Image
im = Image.open("1.png")
im.show()
print im.mode
im.convert("RGBA").save("2.png")
Well, with my image you can see the difference.
My question is: how do I convert it properly?
Image:
Result:
NOTE: The original image has a semi-transparent glow, the result has a solid green "glow"
This issue was reported here:
https://bitbucket.org/effbot/pil-2009-raclette/issue/8/corrupting-images-in-palette-mode
In March 2012, a comment says it's now fixed in development version of PIL. The most recent released version is 1.1.7, so the fix won't be available until 1.2 comes out. PIL updates very slowly, so don't expect this to come out soon.
Unfortunately your PNG image is a type that PIL doesn't handle very well - a paletted image with an alpha channel. When you open the image, the alpha is thrown away and there's no way to get it back.
This is different from the usual palette transparency where one index of the palette is used to denote fully transparent pixels.
You could use scipy.misc.imread:
img = scipy.misc.imread(filename, mode='RGBA')
img = Image.fromarray(img)
Your problem is that you do not provide info about what PIL should use as source of ALPHA channel.
PIL will not on its own add transparency to your image.
What part of your image you want to be transparent?

Categories