As seen here, I had a bit of an issue to get my vertical images working with the 4k Slideshow Maker. It could recognize the image orientation but would zoom the images too much as to pixelate them. This rendered the images right in the face of the viewer. I needed a way to best fit these images on the screen while the background could be black as in most other software I have used. Sure I could quickly overlay the image onto a black canvas in photo editing software and merge down the layers to create a horizontal image more suited to 4k’s liking. But I couldn’t possibly keep editing tens or hundreds of such images before making a photo dvd. It would frustrate me to no end.
To get around this tricky situation, I took help of some Imagemagick commands and put my unix scripting skills to some practical use. The idea was to create a template black background and superimpose a portrait image on top of it an save the edited image. This would give me the required files in landscape mode and thus make it more convinient to create a photo slideshow.
Well if only things were that easy. The script that I wanted write would have to be smart to check the orientation of the images and edit only those which were portrait, plus landscapes could have different aspect ratios. 16:9, 4:3, 3:2 are all valid landscape aspect ratios. I decided to go with 16:9. For the images that would be identified as portraits, I would create a black background which is 16:9 in dimensions based on the height of the current image.
To check dimensions of the image I am using the identify command from the Imagemagick suite.
$ identify image_portrait.jpg
image_portrait.jpg JPEG 1440x2560 1440x2560+0+0 8-bit DirectClass 1.752MB 0.010u 0:00.010
$ identify image_landscape.jpg
image_landscape.jpg JPEG 2560x1440 2560x1440+0+0 8-bit DirectClass 1.939MB 0.010u 0:00.019
This lists the dimensions of the image in the third column of the output, this value can then be parsed further to get the height and width of the image. This helps in identifying the image orientation.
Next command was to generate a black background template.
convert -size 2560x1440 canvas:none -fill black black.jpg
This will create a black background. The script calculates the new dimensions before creating a new template for each conversion. Finally the magic would happen using the composite command.
composite -gravity center portrait.jpg black.jpg new_landscape.jpg
This simply super imposes the first file onto the second to create the final file.
The script also works a bit to backup original files so that they remain unharmed. With some trial and error I was able to get it working just the way I wanted. The full script is here. And it results pasted below.
#!/bin/sh
if [ ! -d backup ]
then
mkdir backup
fi
alljpegs=`ls *.jpg`
for file in $alljpegs
do
fileinfo=`identify $file`
filedimensions=`echo $fileinfo | cut -d " " -f 3`
x=`echo $filedimensions | cut -d "x" -f 1`
y=`echo $filedimensions | cut -d "x" -f 2`
if [ $x -lt $y ]
then
newx=`awk -v m=$y 'BEGIN {print int((16 / 9 * m)+1)}'` # stretch to a 16:9 image
identitycommand="convert -size "$newx"x"$y" canvas:none -fill black black.jpg"
`$identitycommand`
mv $file backup/$file
composite -gravity center backup/$file black.jpg $file
fi
done
if [ -f black.jpg ]
then
rm black.jpg
fi
echo "DONE"
Seems to work for me, but I see several improvements that I can work on as time moves along. I am thinking the script could be parameterized so that user can provide the file extensions (jpg or png or some other), also specify a different acpect ratio and maybe also the solid color for the background. The idea would be to invoke this script with user defined parameters and let it work its magic.
$ makelandscapes -a 4:3 -e png -b red
where the flags mean this
-a aspect ratio taking in values like 16:9 4:3 or 3:2
-e extension of the files to process
-b fill color for the background
Also since the script depends heavily on Imagemagick commands, it would make sense to check for its installation before processing any further. I think this will make for a great utility for me and my photo dvd experiments on Linux.