[PYTHON] Even if I converted jpg to png, I managed to do it because the transparency was not tampered with

Introduction

Even if I converted jpg to png with python, I could not change the transparency, so I solved it by force

environment

Simply convert

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.

problem

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.

Solution

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

Even if I converted jpg to png, I managed to do it because the transparency was not tampered with
Even if I uploaded HTML with boto3, it was not recognized as HTML
The sound of tic disorder at work is ... I managed to do it with the code
The file edited with vim was readonly but I want to save it
I managed to do it because the custom of attaching a zip with a password to an email and saying "I will send you the password separately" is troublesome.
When I tried to do socket communication with Raspberry Pi, the protocol was different
What I did when I was angry to put it in with the enable-shared option
It was a little difficult to do flask with the docker version of nginx-unit
A beginner tried coloring line art with chainer. I was able to do it.
(Note) I'll do a Django tutorial. Even if I follow the site, it doesn't work.
[IOS] GIF animation with Pythonista3. I was addicted to it.
Insecure Platform Warning still appears with pip install, but I didn't do it because it was troublesome.
GCP's Cloud Shell Editor was clogged up with the Python development environment, so I managed to rebuild it in a vacant lot.
Made it possible to convert PNG to JPG with Pillow of Python
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
I want to do ○○ with Pandas
What skills do I need to program with the FBX SDK Python?
I want to do it with Python lambda Django, but I will stop
When I tried to run Python, it was skipped to the Microsoft Store
I installed PySide2, but pyside2-uic didn't work, so I managed to do it.
Use Python from Java with Jython. I was also addicted to it.
What to do if the Chainer (Windows) sample mnist terminates with WinError 183.
What to do if pyenv install does not proceed with an error
What to do if the server doesn't start with python manage.py runserver
What to do if you get Could not fetch URL 443 with pip
The story that Apache dealt with because it was down at AH00144
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.