From JPG to WEBP: Image Resizing and Conversion Made Easy

Greem
2 min readOct 18, 2024

--

Recommended image sizes online are

convert images from .heic to .jpg extension

for file in *.heic; do sips -s format jpeg "$file" - out "${file%.HEIC}.jpg"; done

Change image size with python image processing library, PIL/Pillow

pip3 --version
pip3 install Pillow

python3 // start a python shell
from PIL import Image // verify Pillow installation

create a python script

import os
from PIL import Image

# Define the folder containing the images
folder_path = 'path/to/your/folder' # Change this to your folder path

# Define new width (height will be adjusted to maintain aspect ratio)
new_width = 2000

# Loop through all files in the directory
for filename in os.listdir(folder_path):
if filename.endswith(('.jpg', '.jpeg', '.png')): # Add more extensions if needed
file_path = os.path.join(folder_path, filename)

# Open the image file
with Image.open(file_path) as img:
# Calculate the new height to maintain aspect ratio
new_height = int((new_width / img.width) * img.height)
img = img.resize((new_width, new_height))

# Save the resized image (you can overwrite or save with a new name)
img.save(os.path.join(folder_path, f'resized_{filename}'))

print("Resizing complete!")

run the python script

$python3 filename.py

convert images from .jpg to .webp extension

.webp and .avif image files are generally preferred over .jpeg and .png files because they offer high quality while being compressed, making them lighter and optimizing speed. By installing cwebp on our local machine, we can easily create .webp files.

$brew install webp

/*** get to your directory ***/
$cd path/to/your/folder
/*** run this bash command, in this folder, all files with .jpg create .webp files ***/
$for file in *.jpg; do cwebp "$file" -o "${file%.jpg}.webp"; done

--

--

No responses yet