How to convert a large number of images to another format
August 13, 2022
Bulk image conversion
The article “How to convert images to any formats in Linux” shows how you can easily convert an image from one format to another using the magick program (hundreds of formats are supported).
The “magick” utility is part of the ImageMagick package. For details on installing ImageMagick, including which dependencies need to be installed to support the maximum number of formats, as well as a description of the command structure and all options, see the article: ImageMagick guide: installing, using, and troubleshooting.
But what if you need to convert not just one file, but dozens or even hundreds of photos or pictures to another format?
The magick utility is great for bulk conversion and converting a large number of files.
Suppose there is a task to convert all JPG files in the current directory so that they are converted to PNG, HEIF or another format and saved in the specified folder, while each file must keep its own name.
First, let's note that in order not to specify each file separately, we will use “Wildcards in the file name”, that is, we will specify “*.jpg” as the input file.
As the output file, we will use “Specifying a file name pattern”.
Some “properties” must be defined in a special way in order to be used. For example, only “properties” prefixed with “filename:” can be used to change the output filename of an image.
Let's create a “png” directory:
mkdir png
The following command reads all the .jpg files in the current directory one by one, converts them to PNG format, and saves them to the “png” directory with the same names:
magick *.jpg -set filename:currentfile '%t' 'png/%[filename:currentfile].png'
Why does ImageMagick save all images to one file?
Suppose you need to solve a similar problem, but convert files to another format, for example, one of GIF, MIFF, TIFF, PDF, MNG, HEIF or HEIC.
See also: How to enable saving photos in HEIC (HEIF) format in Android and whether it should be done
The program as a whole will perform the specified task, but you will find that all images are saved in one file. The matter is that all specified formats support files with several pictures.
Magick's default behavior is to save all images to a single file if the format supports it. To change this default behavior, use the “+adjoin” option
For example, the following command will convert all images to .heif format and store the individual files in the “heif” directory:
mkdir heif magick *.jpg -set filename:currentfile '%t' +adjoin 'heif/%[filename:currentfile].heif'
How to convert photos and reduce their size at the same time
You can combine image conversion with image resizing. For example, the following command will reduce all JPEG files in the current directory to 1000 pixels and convert them to HEIF format, then save them all to the “heif” folder with the original file name, to which the new photo resolution and new file extension will be added:
magick '*.jpg[1000x>]' -set filename:currentfile '%t-%wx%h' +adjoin 'heif/%[filename:currentfile].heif'
To avoid adding a new photo resolution, use the following command (the old name will be kept, but with a new filename extension):
magick '*.jpg[1000x>]' -set filename:currentfile '%t' +adjoin 'heif/%[filename:currentfile].heif'
Instead of changing the resolution of a photo, you can keep its size but increase the compression ratio (reduce image quality) by using the -quality option. The following command will read all JPEG files, compress them with quality 40 and convert them to HEIF format, then save them all to the “heif” folder with the original file name (only the extension will change):
magick '*.jpg' -quality 40 -set filename:currentfile '%t' +adjoin 'heif/%[filename:currentfile].heif'
For details on resizing and compressing photos, see the article: How to reduce image size. Bulk photo scaling and resizing on the command line.
Related articles:
- How to reduce image size. Bulk photo scaling and resizing on the command line (100%)
- The structure of the magick command (ImageMagick) (100%)
- How to check JPG quality level (SOLVED) (83.9%)
- ImageMagick guide: installing, using, and troubleshooting (71.8%)
- How to convert images to any formats in Linux (SOLVED) (64.9%)
- How to set up automatic login in Linux with SDDM (RANDOM - 50%)