convert


Bulk convert PNG images to JPG / JPEG

for i in *.png ; do convert "$i" "${i%.*}.jpg" ; done

The command “for i in .png ; do convert “$i” “${i%.}.jpg” ; done” is a Bash script that converts all PNG files in the current directory to JPEG files.

Let’s break down this command:

  • “for i in *.png ;” is a loop that iterates over each PNG file in the current directory.
  • “$i” is the name of the current PNG file being processed.
  • “convert” is a command-line tool that is part of the ImageMagick software suite. It is used for image conversion, resizing, and manipulation.
  • “${i%.}.jpg” is the new filename that the PNG file will be converted to. The “${i%.}” syntax is used to remove the file extension from the original PNG file name, leaving just the base filename, which is then followed by “.jpg” to indicate that the new file should be a JPEG file.

In summary, this command converts each PNG file in the current directory to a JPEG file with the same base filename. For example, “example.png” would be converted to “example.jpg”. This command can be useful when you have a large number of PNG files that you need to convert to JPEG format quickly and easily.


ImageMagick: merge two images using a black and white mask

Recently, we wanted to merge two images into one using a custom black and white mask. To avoid using heavy GUI-based software, we decided to do it using the composite command of the ImageMagick package. Using the composite program, you can overlap one image over another. The command used is straightforward once you get the order of the parameters in the command line.

convert black.jpg white.jpg mask.png -composite masked.jpg;

The above command accepts four parameters:

convert "${BLACK_PART}" "${WHITE_PART}" "${MASK}" -composite "${OUTPUT}";

The first parameter (${BLACK_PART}) is the input picture you want to be placed in all black parts of the mask. The second parameter (${WHITE_PART}) is the input photo you wish to use on all white parts of the mask. The third parameter (${MASK}) is the black and white image that you will be used to merge the previous two images. The final parameter (${OUTPUT}) is the filename to write the final results.

Below we present the results of a demo we created using the above command:

The first parameter (${BLACK_PART}) is the input picture you want to be placed in all black parts of the mask.
The second parameter (${WHITE_PART}) is the input photo you wish to use on all white parts of the mask.
The third parameter (${MASK}) is the black and white image that you will be used to merge the previous two images.
The final parameter (${OUTPUT}) is the filename to write the final results.

ImageMagick apply blur to photo using a black and white mask

Recently, we were trying to apply blurriness to the frames of a video using a custom mask. Our needs would not be short of describing using geometric shapes, so we created the following image (blur.png) as a template for the blurring effect:

The above mask applies a blur effect to all black pixels and leaves all white pixels in the original image intact.

The command that we used was the following:

convert "${FILE}" -mask blur.png -blur 0x8 +mask "blur/${FILE}";

This command creates a new copy of the input file and places it into the folder named blur, so be sure to make the folder before using the above command (e.g., using the command mkdir blur).

Parameters and other information

  • -mask this flag assosiates the filename that is given with the mask of the command.
  • -blur defines the geometry that is used reduce image noise and reduce detail levels.
    To increase the blurriness you can increase the number in this variable 0x8.
  • +mask The ‘plus’ form of the operator +mask removes the mask from the input image.

The version of convert that we used for this example was the following:

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC

Below is a result frame from a video that we processed:

Additional material

To apply it to all video frames in the folder, we used the following command to make our life easier:

find . -maxdepth 1 -type f -name "*.ppm" -exec bash -c 'FILE="$1"; convert "${FILE}" -mask blur.png -blur 0x8 +mask "blur/${FILE}";' _ '{}' \;

The above command finds all frames in the current folder and executes the convert command described above. Since FFmpeg names the frames as PPM, we used that to filter our search. The blur folder is in the same folder as the original images. To avoid processing the pictures in that folder again, we defined the -maxdepth parameter in find that prevents it from navigating into child folders of the one we are working in.


convertAndPrintSeconds – Convert seconds to minutes, hours and days in bash 1

The following code can be used to convert some time in seconds to minutes, hours and days in bash.
It will print on screen the converted values only if they are not 0. i.e If the resulting days is 0, it will not print the text for days at all.
You can use it in any script without copy pasting everything in it by executing the following command source ./convertAndPrintSeconds.sh.
Doing so, it will load to your script the function that is defined in convertAndPrintSeconds.sh, making it available for you to use (something like including code in C, with some caveats).

[download id=”2118″]


#!/bin/bash

convertAndPrintSeconds() {

    local totalSeconds=$1;
    local seconds=$((totalSeconds%60));
    local minutes=$((totalSeconds/60%60));
    local hours=$((totalSeconds/60/60%24));
    local days=$((totalSeconds/60/60/24));
    (( $days > 0 )) && printf '%d days ' $days;
    (( $hours > 0 )) && printf '%d hours ' $hours;
    (( $minutes > 0 )) && printf '%d minutes ' $minutes;
    (( $days > 0 || $hours > 0 || $minutes > 0 )) && printf 'and ';
    printf '%d seconds\n' $seconds;
}

Usage

Function convertAndPrintSeconds accepts one parameter, the positive integer number that represents the seconds count we want to convert.

Example


$ source ./time.sh 
$ convertAndPrintSeconds 10
10 seconds
$ convertAndPrintSeconds 100
1 minutes and 40 seconds
$ convertAndPrintSeconds 1000
16 minutes and 40 seconds
$ convertAndPrintSeconds 10000
2 hours 46 minutes and 40 seconds
$ convertAndPrintSeconds 100000
1 days 3 hours 46 minutes and 40 seconds
$ convertAndPrintSeconds 1000000
11 days 13 hours 46 minutes and 40 seconds