Xfce Forum

Sub domains
 

You are not logged in.

#1 2019-07-07 22:20:25

yurbev
Member
Registered: 2016-04-18
Posts: 55

Imagemagick Caption Images Command Line Prompt

Could this sixsixfive script, from this thread, be converted so that instead of a YAD input box there will be a bash terminal that pops up and allows one to input the data and press Enter?

I'm using WSL 1 Ubuntu on Windows and this WSL version doesn't have the ability to use GUI Linux programs, so I would like the following code to be modified to allow me to use a terminal prompt for input instead of YAD. I hear that WSL 2 will allow for use of GUI Linux programs, but it isn't yet stable so I don't want to use it.

#!/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"
caption=$(yad --entry --editable --no-buttons --width 410 \
	--title="Please enter your caption and press enter:")
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

Offline

#2 2019-07-07 23:17:19

Skaperen
Member
From: right by Jesus, our Saviour
Registered: 2013-06-15
Posts: 862

Re: Imagemagick Caption Images Command Line Prompt

if all you want to know is yes or no, is it possible, i will say yes, it is possible.  how to do it?  that i cannot tell you.  i do know the terminal can be opened to run anything.  so having a small separate script to take in the input and store it in a file and exit, the have this script read it and clean up the file, is one way to do it.  there may be nice tools to take care of that, or just open a small window that plays like a terminal.

Last edited by Skaperen (2019-07-07 23:18:44)

Offline

#3 2019-07-08 00:38:15

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,485

Re: Imagemagick Caption Images Command Line Prompt

You can simply replace the yad command:

caption=$(yad --entry --editable --no-buttons --width 410 \
    --title="Please enter your caption and press enter:")

...with a simple echo and read:

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

Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#4 2019-07-08 16:36:08

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Imagemagick Caption Images Command Line Prompt

Tx!

I'm using the script identical to the one in the OP, but with your edit incorporated. Could you help me with the following errors?

C:\Users\surfacepro\Pictures\000\fer\test>wsl /mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh
/mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh: 3: cd: can't cd to .
Please enter your caption and press enter...
Tomato
: bad variable nameepro/work/Scripts/imagemagickcaption.sh: 5: read: caption
/mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh: 28: /mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh: Syntax error: end of file unexpected (expecting "then")

Last edited by yurbev (2019-07-08 16:37:12)

Offline

#5 2019-07-08 20:28:35

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,485

Re: Imagemagick Caption Images Command Line Prompt

Here is the full contents of the changed script. This version works for me.

#!/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

Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#6 2019-07-08 23:16:39

Skaperen
Member
From: right by Jesus, our Saviour
Registered: 2013-06-15
Posts: 862

Re: Imagemagick Caption Images Command Line Prompt

i just discovered i don't even have yad on my system (Xubuntu 18.04).  so this is making the script wider portable.

Offline

#7 2019-07-09 04:00:16

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Imagemagick Caption Images Command Line Prompt

ToZ wrote:

Here is the full contents of the changed script. This version works for me.

Yes, I'm using the same. But I still get the error I posted. It's likely a problem due to my using it on Windows Subsystem for Linux. I'll tinker some more with it.

If I can get it to work I actually prefer using the command prompt to input data to either YAD or Zenity. It begs the question, why do people use YAD and Zenity when they could just have a popup command prompt to input data?

Offline

#8 2019-07-09 11:05:11

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,485

Re: Imagemagick Caption Images Command Line Prompt

yurbev wrote:

It begs the question, why do people use YAD and Zenity when they could just have a popup command prompt to input data?

Graphically more pleasing I guess.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#9 2019-07-09 18:07:00

MountainDewManiac
Member
From: Where Mr. Bankruptcy is Prez
Registered: 2013-03-24
Posts: 1,115

Re: Imagemagick Caption Images Command Line Prompt

yurbev, a thought just occurred to me. Although you are probably correct in your assumption about the cause of the errors... From time to time, someone encounters a problem with a script or bit of code that has been posted, and - very occasionally - it turns out that they incorrectly entered that code and then missed their error. Are you sure you did a correct and complete copy & paste on the code that ToZ posted in this thread?

Regards,
MDM


Mountain Dew Maniac

How to Ask for Help <=== Click on this link

Offline

#10 2019-07-10 14:32:56

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Imagemagick Caption Images Command Line Prompt

I should have mentioned that when running Toz's modified script, it initially showed the terminal flashing opened and closed and doing nothing.

#!/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

I then took out the following:

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;}

When running the script again I get the prompt to enter data but after pressing Enter is doesn't work. That is when I received the errors I posted above. What might be the problem is that 1 or more of the 3 programs aren't found, and so the script can't execute.

However, I do have Imagemagick and bc installed, so perhaps I could tell the bash script, within the script, where to find those programs?

Here are their locations:

/usr/local/bin/convert
/usr/local/bin/identify
/usr/bin/bc

But they're installed in WSL, so I might need to modify the paths to reflect that, I'm not sure.

And this is command I run in Windows terminal to run the bash script:

wsl /mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh

Offline

#11 2019-07-10 22:53:45

Misko_2083
Member
Registered: 2015-10-13
Posts: 211
Website

Re: Imagemagick Caption Images Command Line Prompt

1.The problem exists between the keyboard and the chair.
You have to pass the file to the script.
wsl /mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh /patho/to/image.jpeg
2. You have carrige returns at the end of the lines in your script. That is why read returns an error.  It's trying to read  caption\r instead of caption
You are in Windows not Linux.
Use dos2unix to convert your script file. That will replace the carriage returns with newline characters.
3. Some syntax error, not sure

Yes, you can use the full paths in the script for the three commands
/usr/local/bin/convert
/usr/local/bin/identify
/usr/bin/bx

Instead of

convert
identify
bc


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#12 2019-07-11 21:11:59

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Imagemagick Caption Images Command Line Prompt

Tx!

Instead of using dos2unix I used Notepad++'s inbuilt EOL conversion utility.

This time I run TOZ's modified script and I get no initials errors. I type in the input and press Enter. The backup folder "bakup" is created, but no backup .jpeg is placed in the backup folder and no caption is created for the image.

So it's an improvement, but everything works except the Imagemagick (and maybe bc) commands.

I could try using the full paths to convert, identify, and bc in the script, as you suggested, but I'm not quite sure how to do that with WSL. Could I use something like  WSLpath?

Offline

#13 2019-07-13 10:50:12

Misko_2083
Member
Registered: 2015-10-13
Posts: 211
Website

Re: Imagemagick Caption Images Command Line Prompt

No idea. Haven't used Windows since 2007. Perhaps Toby Zed knows. (◔‿◔)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#14 2019-07-20 00:46:28

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Imagemagick Caption Images Command Line Prompt

You have to pass the file to the script.
wsl /mnt/c/Users/surfacepro/work/Scripts/imagemagickcaption.sh /patho/to/image.jpeg

I tried this method when you posted it on the 10th, but it didn't work for me. Following your suggestion I tried passing the file to the script using the full path of the image, as your example indicates. But the solution for me, as I discovered today, was to not use the full path but merely use *.jpg. In Windows terminal:

wsl /mnt/c/Users/surfacepro/Scripts/imagemagickcaption.sh *.jpg

And now it works.

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.016 seconds, 7 queries executed - Memory usage: 610.78 KiB (Peak: 627.63 KiB) ]