Using PIL, we can read an image in one file format and save it to another; for example, from PNG to JPG, as shown in the following:
im = Image.open("../images/parrot.png")print(im.mode) # RGBim.save("../images/parrot.jpg")
But if the PNG file is in the RGBA mode, we need to convert it into the RGB mode before we save it as JPG, as otherwise it will give an error. The next code block shows how to first convert and then save:
im = Image.open("../images/hill.png")print(im.mode)# RGBAim.convert('RGB').save("../images/hill.jpg") # first convert to RGB mode