Μηνιαία αρχεία: Ιανουάριος 2022


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.

LibreOffice Calc: Finding duplicate / common entries between two columns

This video demonstrates how to find all matching values between two columns in LibreOffice Calc. Precisely, in this video, we fill in columns A and B with random integer values between the numbers 1 and 100 (inclusive). The formula to generate the random values was the following:

=RANDBETWEEN(1;100)

After we filled in columns A and B with random values, we used the following formula in each cell of column C to find all common values between the first two columns:

=IF(ISERROR(MATCH(B1;A:A;0));"";B1)

LibreOffice Cal commands used in this video

  • RANDBETWEEN(Bottom; Top) – Returns an integer random number in a specified range.
  • IF(Test; ThenValue; OtherwiseValue) – Specifies a logical test to be performed.
  • ISERROR(Value) – Tests for error conditions, including the #N/A error value, and returns TRUE or FALSE.
  • MATCH(SearchCriterion; LookupArray; Type) – Returns the relative position of an item in an array that matches a specified value. The function returns the position of the value found in the lookup_array as a number.


FFmpeg: Could find no file with path ‘%08d.ppm’ and index in the range 0-4 %08d.ppm: No such file or directory

During some work that we were doing, we used the following command to export the frames of a video using FFmpeg:

ffmpeg -v quiet -i "$video" "$input_frames_folder/%08d.ppm";

The above command exported the video frames into the selected folder and using eight digits zero-padding it named all the images in an increasing order starting from the number 00000001 (00000001.ppm).

Later on, we processed those frames and deleted some of the first ones (specifically, we deleted the first 61 frames, so the first available frame was named 00000062.ppm). When we tried to rebuild the video using the command below, we got the error that is listed after the command:

ffmpeg -framerate "25/1" -i "$input_frames_folder/%08d.ppm" -pix_fmt yuv420p -y processed.mkv;
Could find no file with path '%08d.ppm' and index in the range 0-4 %08d.ppm: No such file or directory

To fix the issue, we used the -start_number parameter with the value 62. The parameter sets the file starting index for the matched image files.

ffmpeg -start_number 62 -framerate "25/1" -i "$input_frames_folder/%08d.ppm" -pix_fmt yuv420p -y processed.mkv;

Please note that we also used -pix_fmt yuv420p as we were getting a video with black frames only, so we had to define the format of the pixels to use manually.


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.