Programmatically resizing a folder of images

This is a note to self about how to programmatically resize and crop many images using ImageMagick.

It all started with a folder full of photos with different pixel sizes and ratios. That is because they had been captured with various cameras and had also been manually cropped. This could be verified by running this command to print their pixel sizes:

identify -format "%wx%h\n" *.JPG

Fortunately, all the images had a reasonably large pixel count, so I decided to go for a 5MP pixel count (2560×1920 in 4:3 ratio). That was achieved with this one-liner:

for i in *.JPG; do convert "$i" -resize 3000x1920 -crop 2560x1920+0+0 "$i"; done

The little script looks for all the image files in the folder and starts by resizing them to the preferred height (1920 pixels) and then cropping them to the correct width (2560 pixels). The result is a folder full of equally sized images.

Ps: the script above overwrites the original files in the folder.

Rotate video using FFmpeg

Here is another FFmpeg-related blog post, this time to explain how to rotate a video using the command-line tool FFmpeg. There are two ways of doing this, and I will explain both in the following.

Rotation in metadata

The best first try could be to make the rotation by only modifying the metadata in the file. This does not work for all file types, but should work for some (including .mp4) files.

ffmpeg -i input.mp4 -metadata:s:v rotate="-90" -codec copy output.mp4

The nice thing here is that it is superfast and also non-destructive.

Rotation with compression

If the above does not work, you will need to recompress the file. That is not ideal, but will do the trick. To rotate a movie by 90 degrees, you can do:

ffmpeg -i input.mp4 -vf "transpose=1" -c:a copy output.mp4

The trick here is to use the rotation value:

  • 0 – Rotate by 90 degrees counter-clockwise and flip vertically. This is the default.
  • 1 – Rotate by 90 degrees clockwise.
  • 2 – Rotate by 90 degrees counter-clockwise.
  • 3 – Rotate by 90 degrees clockwise and flip vertically.

I often record with cameras hanging upside down. Then I want to rotate 180 degrees, which can be done like this:

ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4

Even though the video is re-compressed using this method, you can force the audio to be copied without compression by adding the -c:a copy tag:

ffmpeg -i input.mp4 -vf "transpose=1" -c:a copy output.mp4

Of course, these commands can also be included in a chain of other FFmpeg-commands.

Crop video files with FFmpeg

I have previously written about how to trim video files with FFmpeg. It is also easy to crop a video file. Here is a short how-to guide for myself and others.

Cropping is not the same as trimming

This may be basic, but I often see the concepts of cropping and trimming used interchangeably. So, to clarify, trimming a video file means making it shorter by removing frames in the beginning and/or end. That is not the same as cropping a video file, which only selects a particular part of the video for export.

The one-liner

If you want to get it done, here is the one-liner:

ffmpeg -i input.avi ffmpeg -i output2.mp4 -vf crop=1920:1080:0:200 output.avi

There is more information about this command in many places (for example, here). the most critical parts of the above command are:

  • -vf is the command for “video filter”. It can also be spelt out as -filter:v.
  • crop=1920:1080:0:200 means to make a 1920×1080 video starting from the leftmost side of the source image (=0 pixels horizontally) and 200 pixels down from the top.

Of course, this command can be combined with other FFmpeg combinations.

Converting a .WAV file to .AVI

Sometimes, there is a need to convert an audio file into a blank video file with an audio track. This can be useful if you are on a system that does not have a dedicated audio player but a video player (yes, rare, but I work with odd technologies…). Here is a quick recipe

FFmpeg to the rescue

When it comes to converting from one media format to another, I always turn to FFmpeg. It requires “coding” in the terminal, but usually, it is only necessary to write a oneliner. When it comes to converting an audio file (say in .WAV format) to a blank video file (for example, a .AVI file), this is how I would do it:

ffmpeg -i infile.wav -c copy outfile.avi

The “-c copy” part of this command is to preserve the original audio content. The new black video file will have a copy of the original .WAV file content. If you are okay with compressing the audio, you can instead run this command:

ffmpeg -i infile.wav outfile.avi

Then FFmpeg will (by default) compress the audio using the mp3 algorithm. This may or may not be what you are after, but it will at least create a substantially smaller output file.

Of course, you can easily vary the above conversion. For example, if you want to go from .AIFF to .MP4, you would just do:

ffmpeg -i infile.aiff outfile.mp4

Happy converting!

Soft drop shadows in LibreOffice Draw

My new book will be published Open Access, and I also aim only to use open-source tools as part of the writing process. The most challenging has been to figure out how to make nice-looking illustrations.

Parts of the book are based on the Ph.D. dissertation that I wrote a long time ago. I wrote that on a MacBook and made all the illustrations in OmniGraffle. While it was quite easy to make the switch to Ubuntu in general, OmniGraffle has been one of the few programs I have really missed in the Linux world. True, many graphics tools are available, but none with the same type of finesse as OmniGraffle.

After trying and failing with many of the most popular vector graphics tools, I have ended up using LibreOffice Draw to make all the illustrations. It is more limited than, say, Inkscape but provides the tools I need. It was also easy to get used to the workflow. The only thing I have struggled with figuring out is how to create nice-looking soft drop shadows. In OmniGraffle, the shadow function immediately looks nice, such as in this illustration of how one can think of an instrument as the mediator between action and sound:

My first attempts at adding a shadow in LibreOffice Draw ended up with a gruesome non-soft shadow:

I searched around, but could only find all sorts of complicated solutions. This was last year, and I decided to move on with the project without shadows. I could have moved to another graphics tool but had already spent quite some time making Draw figures. So I decided that shadows were not that important after all.

As part of the final manuscript completion, I am currently making minor changes to all the figures. Out of the blue, I found out that the latest version of Draw now has a more developed shadow section.

Hooray! By changing the “distance,” “glow,” and “transparency” settings, I now finally have soft drop shadows in place:

Draw does still not provide the same smooth user experience as OmniGraffle. But getting the soft drop shadow function in place helps a lot!