Page 1 of 1

Captioning Images

Posted: 2019-07-18T20:09:20-07:00
by buchert
I've been using the following bash script to caption images. This bash script worked on Linux, but since switching to Windows and using Windows Subsystem for Linux I'm not able to get it to work, despite have all the required programs installed. When using it with WSL I get a command prompt to enter the caption and the "bakup" folder is created, but nothing else works.

I'd like the script to be converted into a one-line command, if possible. I've included some example images below. If I'm not able to get it to work with WSL, then perhaps I'll install ImageMagick on Windows, but I'd prefer to use it with WSL.

Code: Select all

#!/bin/sh
command -v convert >/dev/null 2>&1 || { echo >&2 "I require convert, but it's not installed. aborting!";exit 1;}
command -v identify >/dev/null 2>&1 || { echo >&2 "I require identify, but it's not installed. aborting!";exit 1;}
command -v bc >/dev/null 2>&1 || { echo >&2 "I require bc, but it's not installed. aborting!";exit 1;}

basedir="$(dirname "$(readlink -f "${1}")")"
cd "$basedir"

echo "Please enter your caption and press enter"
read caption

if [ -z "$caption" ]; then
	printf "no caption was selected, aborting!\n"
	exit 1
fi
printf "caption is $caption\n"
if [ ! -d "$basedir"/bakups ]; then
	mkdir -p "$basedir"/bakups
fi
while [ $# -gt 0 ]; do
	file="$1"
	if [ -s "$file" ]; then
		cp -f "$file" bakups
		export imagesize=$(identify -format "%w,%h" "$file")
		export imagewidth=$(echo "$imagesize" | cut -f1 -d",")
		export imageheight=$(echo "$(echo "$imagesize" | cut -f2 -d",")*0.03000" | bc)
		convert -background "#0008" -fill white -gravity center \
		-size $(echo $imagewidth)x$(echo $imageheight) caption:"$caption" \
		"$file" +swap -gravity south -composite "$file" && \
		printf "\n$file watermarked successfully\n"
	fi
	shift
done
Image
Image

Re: Captioning Images

Posted: 2019-07-18T22:15:48-07:00
by fmw42
Are you running it in a Unix terminal or a Window terminal? Did you install Imagemagick for Unix in the Windows 10 Unix subsystem?

try using bash

#!/bin/bash

as opposed to

#!/bin/sh

Re: Captioning Images

Posted: 2019-07-18T22:25:38-07:00
by buchert
Tx for your reply. I just tried it with your advice, but still the same result.

I'm running it in a Windows terminal. When running in Windows terminal I prepend it with "wsl". This works with all the other bash scripts I use. But when I try it in the Windows Subsystem for Linux Unix terminal it doesn't work either.

Yes I installed Imagemagick for Unix in the Windows 10 Unix subsystem. My other ImageMagick commands work with WSL. I'm just having problems with this one for some reason. I have bash scripts using Mogrify that work with WSL.

Could you give me a one-line code to try?

Re: Captioning Images

Posted: 2019-07-18T23:02:41-07:00
by fmw42
I am not a Windows user and only know to use bash scripts in a Unix bash terminal window on a PC, though I do not have a PC. But many of my bash script (at my link below) have been reported to work fine.

Code: Select all

#!/bin/bash

convert logo: logo.gif

See
https://imagemagick.org/discourse-serve ... 26&t=36055

Re: Captioning Images

Posted: 2019-07-19T04:32:26-07:00
by snibgo
buchert wrote:... and the "bakup" folder is created, but nothing else works.
What happens instead? How do you know that "nothing else works"? What is the first command that doesn't do what you want?

Re: Captioning Images

Posted: 2019-07-19T15:39:36-07:00
by buchert
I'm trying to apply this bash script to a .jpg file. When I run the following from Windows terminal:

Code: Select all

wsl /mnt/c/Users/surfacepro/Scripts/imagemagickcaption.sh $SHELL
I get:

Code: Select all

Please enter your caption and press enter
testing
caption is testing
cp: cannot remove 'bakups/bash': Permission denied
identify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/556.
(standard_in) 1: syntax error
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/556.
convert: no such image `testing' @ error/mogrify.c/MogrifyImageList/8786.
convert: no images defined `/bin/bash' @ error/convert.c/ConvertImageCommand/3273.
It creates no bakup folder. If I use sudo in the command:

Code: Select all

wsl sudo /mnt/c/Users/surfacepro/Scripts/imagemagickcaption.sh $SHELL
it returns the following:

Code: Select all

[sudo] password for ubuntu:
Please enter your caption and press enter
testing
caption is testing
identify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/556.
(standard_in) 1: syntax error
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/556.
convert: no such image `testing' @ error/mogrify.c/MogrifyImageList/8786.
convert: no images defined `/bin/bash' @ error/convert.c/ConvertImageCommand/3273.
And no bakup folder is created, but I don't get the permission denied error.

When I run

Code: Select all

wsl identify -version 
in Windows terminal I get:

Code: Select all

Version: ImageMagick 7.0.8-48 Q16 x86_64 2019-06-07 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): fontconfig freetype jbig jng jpeg lzma png tiff webp x xml zlib
I get the same result when I run

Code: Select all

identify -version
in Ubuntu's unix terminal.

As I said before I use another Imagemagick bash script, and this following one works fine:

Code: Select all

#!/bin/bash

mogrify -format jpg * && rm *.png *.webp

Re: Captioning Images

Posted: 2019-07-19T15:53:53-07:00
by snibgo
I'm not a bash expert, but you are calling the script with one parameter, "$SHELL", which translates on my computer to "/bin/bash".

That is $1. Then you set the variable "file" to $1, and try to find its size etc, as if it was an image file. But it's not.

Try calling the script with the name of an image file instead.

A useful hint: shell scripts should contain documentation saying what the parameters are.

Re: Captioning Images

Posted: 2019-07-19T17:40:07-07:00
by buchert
Tx for your help! I moved the $SHELL command within the script at the end, and when I run the following in a Windows terminal it works!

Code: Select all

wsl /mnt/c/Users/surfacepro/Scripts/imagemagickcaption.sh *.jpg
The thing I was missing was following the bash script command with *.jpg.