DEV Community

Cover image for How to Convert Multiple WEBP Images to JPG with C++ and ImageMagick
Marcos Oliveira
Marcos Oliveira

Posted on

How to Convert Multiple WEBP Images to JPG with C++ and ImageMagick

🪄 With speed and precision.


A long time ago, I had created a script where I could just enter a folder, run a command, and even with multiple files, it would convert WEBP images to JPG.

However, besides occasionally witnessing conversion failures, there was a time I had a folder with many .webp files and noticed a slight slowness.

So, I decided to rewrite the code in C++.


Code

First of all, you need to have ImageMagick Dev (the .h API) installed on your machine. To do this, run:

Example for systems with APT:

sudo apt install imagemagick graphicsmagick-libmagick-dev-compat
Enter fullscreen mode Exit fullscreen mode

Now create the C++ file, for example: main.cpp and paste this code inside.

The code itself is self-explanatory; it lists the .webp files, converts them to .jpg, and after that, removes the .webp files. If there are no .webp files in the directory where you run the binary, it issues a warning.

#include <Magick++.h>
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main(int argc, char **argv){
  (void)argc;
  Magick::InitializeMagick(*argv);

  bool found = false;

  for(const auto &entry : fs::directory_iterator(fs::current_path())){
    if(entry.is_regular_file() && entry.path().extension() == ".webp"){
      found = true;
      std::string input = entry.path().string();

      fs::path out_path = entry.path();
      out_path.replace_extension(".jpg");
      std::string output = out_path.string();

      try{
        Magick::Image img(input);
        img.write(output);
        fs::remove(entry.path());
      }catch(Magick::Exception &e){
        std::cerr << input << ": " << e.what() << "\n";
        return EXIT_FAILURE;
      }
    }
  }

  if(!found){
    // No images to convert.
    std::cout << "There are no images to convert.\n";
  }
}
Enter fullscreen mode Exit fullscreen mode

Compile with -ffast-math for even faster binary execution:

g++ -ffast-math -o w2j $(Magick++-config --cxxflags --cppflags) main.cpp $(Magick++-config --ldflags --libs)
Enter fullscreen mode Exit fullscreen mode

After that, just install it:

sudo install -v w2j /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Then just use it, for example:

cd folder/with/many/webp/
w2j
Enter fullscreen mode Exit fullscreen mode

And all WEBP files will be converted to JPG and automatically deleted at the end.

Just out of curiosity, the GNU Bash script that did this was this:

w2jpg(){
    shopt -s nullglob
    local files=( *.webp )

    (( ${#files[@]} == 0 )) && {
        echo "No images to convert."
        return
    }

    for f in "${files[@]}"; do
        convert "$f" "${f%.webp}.jpg"
    done

    rm -- *.webp
}
Enter fullscreen mode Exit fullscreen mode

See also:

How to Combine Images with C++ and ImageMagick

Top comments (0)