0 votes
in Python Flask by
How would you use PIL to convert an image into a different file format?

1 Answer

0 votes
by
To convert an image into a different file format using Python Imaging Library (PIL), you first need to import the Image module from PIL. Then, load your image using the Image.open() function and pass in the path of your image as a string argument. Once the image is loaded, use the save() method on the image object and specify the new filename along with its desired extension. The library will automatically recognize the intended format from the extension provided. For instance, if you want to convert ‘image.jpg’ to PNG, the code would be:

from PIL import Image

img = Image.open('image.jpg')

img.save('new_image.png')
...