convert png to jpeg in python

To convert a PNG file to a JPEG file in Python, we can use the Pillow library. Here is an example code:

main.py
from PIL import Image

# Open the PNG image
with Image.open('example.png') as png_image:
    
    # Save the image in JPEG format with desired quality
    png_image.save('example.jpg', 'JPEG', quality=80)
205 chars
8 lines

In the code above, we first open the PNG file using the Image class from the Pillow library. Then, we save the image in JPEG format with the save() method, specifying the file name and format ('JPEG'), as well as the desired quality (80 in this case).

Note that the Pillow library needs to be installed before running the code. You can install it using pip:

main.py
pip install Pillow
19 chars
2 lines

gistlibby LogSnag