DEV Community

doldoki
doldoki

Posted on

.webp to .jpg for Product Image

Recently a friend of mine asked me to help him with converting .webp image files to .jpg. Apparently you can't upload .webp files on Shopify. Converting one or two files can be done easily using free services available online, but bulk conversion was a different story. That's right - You need to pay up!

So I did a quick research and found out that I can use the Pillow library https://pillow.readthedocs.io/en/stable/# for image file conversion.

Let's look at the bare minimum.


from PIL import Image

im = Image.open('image_file.webp')
im.save('image_file.jpg')

Enter fullscreen mode Exit fullscreen mode

This code will take in an image file and spit out an image file of your choice.

As mentioned above, I need to a bulk conversion.
Let's bring in the os module to work with files and path.

from PIL import Image
import os

cwd = os.getcwd()
file_path = os.path.join(cwd, 'imgs')
output_path = os.path.join(cwd,'output')
file_list = os.listdir(file_path)

for file in file_list:

    im = Image.open(os.path.join(file_path, file))
    file_name = file.split('.')[0]
    im.save(f'{output_path}\\{file_name}.jpg')
    print(f'{file_name} Done!')



Enter fullscreen mode Exit fullscreen mode

I made 'imgs' folder where I have the original image files for conversion(.webp). Then I created 'output' folder for storing my output(.jpg).

Then I got a list of my input files using the os.listdir() and looped over the list to save each file into .jpg.

Quick and easy!

Top comments (0)