Skip to content
Advertisement

What is the best way of converting a large folder of bmp files to jpeg?

I have a large folder of BMP files and I want to write a script that will loop through all the files in the folder and convert all BMP files into jpeg. I want it to continuously run as it will be used on a production line where new BMP images will be uploaded regularly.

Advertisement

Answer

You can simply use Pillow library. Use os.listdir to get a list of all the images to convert, then open each image with Pillow and save it as .png.

from PIL import Image
import os

path = "/image_folder/"
images= os.listdir(path)
for img in images:
    Image.open(img).save(os.path.join(path+ str(img).replace(".bmp",".png") + '.png'))
    os.remove(os.path.join(path + img))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement