0

i made python code to convert jpg into webp

from PIL import Image
import PIL
import os
import glob



# Open The Image:
image_path = "D:/..../image_name.jpg"

image = Image.open(image_path)


# Convert the image to RGB Colour:
image = image.convert('RGB')

# Save The Image as webp:
new_image_path = "D:/.../converted_image_name.webp"

image.save(new_image_path, 'webp')

#open converted image for inspection

new_image = Image.open(new_image_path)
new_image.show()

Don't know how to build a function that does the conversion for all files in folder, keeping the orginial file_names.

2 Answers 2

1

Here is a working solution to your problem:

from pathlib import Path

from PIL import Image
import PIL
import os
import glob

# the path to the directory containing the images
directory_path = 'D:\PATH'

def convert_images(directory_path):
    for image_path in os.listdir(directory_path):
        if image_path.endswith(".jpg"):
            image = Image.open(os.path.join(directory_path, image_path))
            # get image filename without an extension
            name = Path(image_path).stem
            rgb_image = image.convert('RGB')
            new_image_path = f"{name}.webp"
            rgb_image.save(os.path.join(directory_path, new_image_path), 'webp')
            new_image = Image.open(new_image_path)
            new_image.show()
            continue
        else:
            continue


convert_images(directory_path)
Sign up to request clarification or add additional context in comments.

7 Comments

I get this error
What error are you getting?
FileNotFoundError: [Errno 2] No such file or directory: 'resized_marie_jo-lingerie-underwired_bra-avero-0100410-%28maincolor%29-0_3515169.jpg' so it can find the file name, but doesn't open it
That's weird. did you set the directory_path = 'D:\PATH' variable to the directory containing the images(not the actual image path)? Plus, is error line number shown? It can help me understand where there is an issue.
errorline ----> 7 image = Image.open(image_path). directory_path = 'D:/Lokale schijf/VANDERLINDEN/datafiles/BRANDS/VDV/images/SS2023/Python_API_imports/'
|
0

final code

from pathlib import Path

from PIL import Image
import PIL
import os
import glob

# the path to the directory containing the images
directory_path =  'D:/Lokale schijf/SS2023/Python_API_imports/'


def convert_images(directory_path):
    for image_path in os.listdir(directory_path):
        if image_path.endswith(".jpg"):
            image = Image.open(os.path.join(directory_path, image_path)) 
            # get image filename without an extension
            name = Path(image_path).stem
            rgb_image = image.convert('RGB')
            new_image_path = f"{name}.webp"
            rgb_image.save(os.path.join(directory_path, new_image_path), 'webp') 
            new_image = Image.open(new_image_path)
            new_image.show()
            continue
        

convert_images(directory_path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.