• Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

What's Hot

Design a plugin for wordpress woocommerce to display a tab to show attachment download

2024-04-06

TranslatePress v2.6.9 – WordPress Translation Plugin

2023-12-25

A Linux batch script converting pictures to webp format

2023-07-10
Facebook Twitter Instagram
  • 中文
  • English
Facebook Twitter Instagram Pinterest VKontakte
Weilai Tech Weilai Tech
  • Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us
Weilai Tech Weilai Tech
Home»linux»Centos»A Linux batch script converting pictures to webp format
Centos

A Linux batch script converting pictures to webp format

chris pengBy chris peng2023-07-10No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Share
Facebook Twitter LinkedIn Pinterest Email

The webp format is a picture format launched by Google. Compared with other conventional formats, this format has better compression efficiency. If the client’s browser supports it, our website will send the webp format to reduce the packet size.

Based on other scripts on the Internet, I wrote this batch file to achieve batch conversion from conventional format to webp format. This script will search all the image files in the specified folder (and all the subfolders), and create a corresponding directory structure in the specified path to store the converted webp format files.

The script

#! /bin/bash

shopt -s nullglob       # Globs that match nothing expand to nothing
shopt -s globstar       # ** matches multiple directory levels

root_webp_dir=/path_to_webp_directory
www_base_dir=/path_to_root_directory

pushd "$www_base_dir"

[[ -d "$www_base_dir" ]] || exit 1

for jpg_path in ./**/*.{png,jpg,jpeg} ; do
    jpg_file=${jpg_path##*/}
    [[ $jpg_path == */* ]] && jpg_dir=${jpg_path%/*} || jpg_dir=.

    webp_dir=${root_webp_dir}/${jpg_dir}
    webp_path=${webp_dir}/${jpg_file}.webp

    [[ -d "$webp_dir" ]] || mkdir -p -- "$webp_dir"
    if [ !  -e "$webp_path" ] || [ "$jpg_path" -nt "$webp_path" ]
    then
        if [[ $jpg_file == *.gif ]]
        then
            gif2webp -lossy -q 80 -m 6 "$jpg_path" -o "$webp_path"
        else
            cwebp -q 75 -m 6 -af "$jpg_path" -o "$webp_path"
        fi
    else
        echo "exists: $webp_path"
    fi
done

echo "$root_webp_dir/**/*.{webp}"

for w_path in $root_webp_dir/**/*.webp ; do
    j_path=.${w_path#"$root_webp_dir"}
    j_path=${j_path%.webp}
    #echo "j_path: $j_path"
    if [ ! -e "$j_path" ]
    then
        echo "remove: $w_path"
        rm "$w_path"
    fi
done

popd

Usage

Change these two paths according to your needs, which are the folder for storing webp and the folder for original pictures.

root_webp_dir=/path_to_webp_directory
www_base_dir=/path_to_root_directory

Since pictures in webp format may be larger than gif, the above script does not convert pictures in gif format by default. If you need to convert gif format pictures, change the first for statement to

for jpg_path in ./**/*.{png,jpg,jpeg,gif} ; do

If there is a webp format picture in the specified root_webp_dir directory, but with no picture corresponding to its file name in www_base_dir, the above script will delete this webp picture in the root_webp_dir directory. If you do not need this function, remove the following code.

for w_path in $root_webp_dir/**/*.webp ; do
    j_path=.${w_path#"$root_webp_dir"}
    j_path=${j_path%.webp}
    #echo "j_path: $j_path"
    if [ ! -e "$j_path" ]
    then
        echo "remove: $w_path"
        rm "$w_path"
    fi
done

Enjoy 🙂

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
chris peng

Related Posts

Keep Your SSH Session Running when You Disconnect

2020-11-25

centos push jekyll to github

2020-11-13

Solved:Could not resolve host: mirrors.iuscommunity.org

2020-06-01

Install: Quick Install Magento 2 on litespeed

2019-11-25
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
July 2023
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« May   Dec »
Tags
app branding design digital Docly docs etc faq fix github Helpdesk Image issue magento Manual marketing memecached Photography planing seo sequrity tips Travel ui/ux web WordPress 爬虫
Editors Picks
About Us

Guangzhou Weilai Technology is a foreign trade integrated marketing service provider focusing on Google as the drainage center and marketing self-built website as the carrier.

Email Us: [email protected]
Contact: +86 18676917505

Facebook Pinterest YouTube LinkedIn
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
From Flickr
Website Design Case
© 2024 Copyright by Guangzhou Weilai Technology Co.,Ltd..
  • Home
  • About Us
  • SEO Services
  • Website Design Service
  • Projects
  • Blog
  • Contact Us

Type above and press Enter to search. Press Esc to cancel.