Even if I converted jpg to png with python, I could not change the transparency, so I solved it by force
If you google "python convert jpg to png", you will see Pillow.
Install as below while looking at the official
pip install Pillow
If you write a sample referring to here
jpgToPNG.py
from PIL import Image
#Absolute pass is also possible
img = Image.open('input.jpg')
img.save('output.png')
With this, PNG can be created, but if you write a source that modifies transparency, you will get an error. By the way, the transparency of the image converted using paint can be adjusted properly.
If I was googled for a while, snarling
Notes on handling png and jpg images
The above site is a hit Indeed, if A of RGBA may remain (is it interpreted correctly?) Even if png is converted to jpg, the opposite may be true.
So, create a source to check by referring to the above site
checkPNG.py
from PIL import Image
path_png1 = "output1.png " #PNG that cannot be edited for transparency
path_png2 = "output2.png " #Transparent editable PNG converted with paint
image = np.asarray(Image.open(path_png1))
print(Image.open(path_png1))
print(image.shape)
image = np.asarray(Image.open(path_png2))
print(Image.open(path_png2))
print(image.shape)
result
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1920x1080 at 0x1762DB300B8>
(1080, 1920, 3)
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1920x1080 at 0x176252F92B0>
(1080, 1920, 4)
Certainly, PNGs that cannot be edited are in RGB mode.
Now that we know the cause, we will resolve it.
According to Notes on handling png and jpg images, you can change the mode by adding convert () after open ().
So, implement as follows
jpgToPNG.py
from PIL import Image
rgba_img = Image.open('input.jpg').convert('RGBA')
rgba_img.save('output.png')
print(rgba_img)
print(np.asarray(rgba_img).shape)
result
<PIL.Image.Image image mode=RGBA size=1920x1080 at 0x1CF941EF9E8>
(1080, 1920, 4)
There was no problem with the result, and I was able to play with the transparency safely. that's all.
Recommended Posts