LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to block out text on an image (https://www.linuxquestions.org/questions/programming-9/script-to-block-out-text-on-an-image-4175640353/)

GPGAgent 10-14-2018 10:09 AM

Script to block out text on an image
 
This command works just fine:

convert T.jpg \( +clone -draw 'rectangle 32,20,178,55' \) -composite conv-T.jpg

It draws a black rectangle in the top left hand corner, 32px down and 20px across.

I want to use this in a script:

convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg

I've set the values and this is what happens

onk@XEON4 ~/TEST/PIC/PIC $ X=32
onk@XEON4 ~/TEST/PIC/PIC $ XX=178
onk@XEON4 ~/TEST/PIC/PIC $ Y=20
onk@XEON4 ~/TEST/PIC/PIC $ YY=55
convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg
convert: unable to open image `(+clone': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: non-conforming drawing primitive definition `rectangle' @ error/draw.c/DrawImage/3190.
convert: unable to parse expression `)' @ error/convert.c/ConvertImageCommand/601.
onk@XEON4 ~/TEST/PIC/PIC $


Any help much appreciated

Field95 10-14-2018 10:43 PM

Try using echo?

Code:

convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg
Code:

echo convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg
Fyi, single quotes ' are taken literally. so $X means $X.
Try using double quotes? This allows variables to parse
Code:

'rectangle $X,$Y,$XX,$YY'
Code:

"rectangle $X,$Y,$XX,$YY"

GPGAgent 10-15-2018 08:10 AM

Quote:

Originally Posted by Field95 (Post 5914859)
Try using echo?

Code:

convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg
Code:

echo convert T.jpg \(+clone -draw 'rectangle $X,$Y,$XX,$YY' \) -composite conv-T.jpg
Fyi, single quotes ' are taken literally. so $X means $X.
Try using double quotes? This allows variables to parse
Code:

'rectangle $X,$Y,$XX,$YY'
Code:

"rectangle $X,$Y,$XX,$YY"

Good idea but no this doesn't solve the problem - the problem being my lack of bash scripting knowledge.

Thanks for trying tho'.

GPGAgent 10-15-2018 08:16 AM

After a bit of digging around using DuckDuckGo (https://duckduckgo.com/) I figured this line out to blur a specified rectangle on an image.

convert Test.jpg -region ${W}x${H}+${x}+${y} -blur 0x8 conv-Test.jpg

Then I wrote this script - Blur-Image.sh

#!/bin/bash
renice 19 -p $$

bold=$(tput bold)
norm=$(tput sgr0)

# default="*.mts"
# read -p "Movie To Png [$default]: " FileName
# FileName=${FileName:-$default}

read -p "Files to process (name only): " FileName

default="jpg"
read -p "Files extension (${bold}$default${norm}|png|...): " Extension
Extension=${Extension:-$default}

default="0"
read -p "Width(${bold}$default${norm}): " W
W=${W:-$default}

default="0"
read -p "Height(${bold}$default${norm}): " H
H=${H:-$default}

default="0"
read -p "Top Corner from left hand side(${bold}$default${norm}): " x
x=${x:-$default}

default="0"
read -p "Top corner from top(${bold}$default${norm}): " y
y=${y:-$default}

convert $FileName*.$Extension -region ${W}x${H}+${x}+${y} -blur 0x8 conv-$FileName.$Extension

exit 0


Feel free to copy and use yourself.

Thanks everyone.

l0f4r0 10-15-2018 08:28 AM

Thanks for Blur-Image.sh.
Regarding your initial question, give the following a try:
Code:

convert T.jpg \(+clone -draw "rectangle '$X','$Y','$XX','$YY'"  \)  -composite conv-T.jpg

GPGAgent 10-15-2018 09:32 AM

Quote:

Originally Posted by l0f4r0 (Post 5915023)
Thanks for Blur-Image.sh.
Regarding your initial question, give the following a try:
Code:

convert T.jpg \(+clone -draw "rectangle '$X','$Y','$XX','$YY'"  \)  -composite conv-T.jpg

Nope doesn't work

Here's the screendump
onk@XEON4 ~/TEST/PIC/PIC $ ls *T*.*
T.jpg T.sh
onk@XEON4 ~/TEST/PIC/PIC $ echo $X $XX $Y $YY
30 178 15 55
onk@XEON4 ~/TEST/PIC/PIC $ convert T.jpg \(+clone -draw "rectangle '$X','$Y','$XX','$YY'" \) -composite conv-T.jpg
convert: unable to open image `(+clone': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: non-conforming drawing primitive definition `rectangle' @ error/draw.c/DrawImage/3190.
convert: unable to parse expression `)' @ error/convert.c/ConvertImageCommand/601.


Putting the actual values in and running your suggestion also does not work
onk@XEON4 ~/TEST/PIC/PIC $ convert T.jpg \(+clone -draw "rectangle 30,15,178,55" \) -composite conv-T.jpg
convert: unable to open image `(+clone': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to parse expression `)' @ error/convert.c/ConvertImageCommand/601.

This is what does work:
onk@XEON4 ~/TEST/PIC/PIC $ convert T.jpg \( +clone -draw 'rectangle 30,15,178,55' \) -composite conv-T.jpgjonk@XEON4 ~/TEST/PIC/PIC $

All I want to know is how to parameterise the rectangle co-ords in a bash script, it shouldn't be this hard surely?

l0f4r0 10-15-2018 10:11 AM

Quote:

Originally Posted by GPGAgent (Post 5915049)
Nope doesn't work

Okay, here is another suggestion (not sure it works):
Code:

coordinates="rectangle 30,15,178,55"
convert T.jpg \( +clone -draw "${coordinates}" \) -composite conv-T.jpg


GPGAgent 10-15-2018 12:42 PM

Quote:

Originally Posted by l0f4r0 (Post 5915058)
Okay, here is another suggestion (not sure it works):
Code:

coordinates="rectangle 30,15,178,55"
convert T.jpg \( +clone -draw "${coordinates}" \) -composite conv-T.jpg


Excellent- that works just fine, now to create a bash script to prompt for the x,y coords etc

I'll post the script later

Many thanks

GPGAgent 10-15-2018 12:59 PM

Quote:

Originally Posted by l0f4r0 (Post 5915058)
Okay, here is another suggestion (not sure it works):
Code:

coordinates="rectangle 30,15,178,55"
convert T.jpg \( +clone -draw "${coordinates}" \) -composite conv-T.jpg


Just simplified this a bit so a script should work

onk@XEON4 ~/TEST/PIC/PIC $X=30;XX=178;Y=15;YY=55
onk@XEON4 ~/TEST/PIC/PIC $ convert T.jpg \( +clone -draw "rectangle $X,$Y,$XX,$YY" \) -composite conv-T.jpg
onk@XEON4 ~/TEST/PIC/PIC $

GPGAgent 10-15-2018 01:18 PM

So here's a script that does work and draws a black rectangle of a specified height and width with the top left hand corner at a specified distance from the top left hand corner of the image

black-rectangle.sh
==================
#!/bin/bash
renice 19 -p $$

bold=$(tput bold)
norm=$(tput sgr0)

read -p "Files to process (name only): " FileName

default="jpg"
read -p "Files extension (${bold}$default${norm}|png|...): " Extension
Extension=${Extension:-$default}

default="0"
read -p "Width(${bold}$default${norm}): " W
W=${W:-$default}


default="0"
read -p "Height(${bold}$default${norm}): " H
H=${H:-$default}

default="0"
read -p "Top Corner from left hand side(${bold}$default${norm}): " x
x=${x:-$default}


default="0"
read -p "Top corner from top(${bold}$default${norm}): " y
y=${y:-$default}

convert $FileName.$Extension \( +clone -draw "rectangle $W,$H,$x,$y" \) -composite conv-$FileName.$Extension

exit 0


Feel free to use it

I would like to alter the colour of the rectangle tho', over to you smart fellers for that one....

l0f4r0 10-16-2018 02:51 AM

Quote:

Originally Posted by GPGAgent (Post 5915096)
Excellent- that works just fine

Glad it works :)

Quote:

Originally Posted by GPGAgent (Post 5915107)
I would like to alter the colour of the rectangle tho', over to you smart fellers for that one....

You need to use the -fill setting before "rectangle".

GPGAgent 10-16-2018 12:36 PM

Quote:

Originally Posted by l0f4r0 (Post 5915286)
Glad it works :)


You need to use the -fill setting before "rectangle".

Cheers, I actually found the -fill option elsewhere, but much appreciate your tip, here's the completed line

convert $FileName.$Extension \( +clone -fill grey59 -draw "rectangle $X,$Y,$XX,$YY" \) -composite conv-$FileName.$Extension

Works perefectly

astrogeek 10-16-2018 05:21 PM

Welcome to LQ GPGAgent!

Glad that you found help, and thanks to those who replied!

In future, please place your code and command snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Thanks!

GPGAgent 10-17-2018 12:47 PM

Quote:

Originally Posted by astrogeek (Post 5915617)
Welcome to LQ GPGAgent!

Glad that you found help, and thanks to those who replied!

In future, please place your code and command snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Thanks!

Hi No problem, didn't spot that one! :o

From now on my code will look like
Code:

CODE=10
And of course you can post a bit of PHP code
PHP Code:

Very useful

originally an acronym for Personal Home Pages - or is that an urban myth?

:D


All times are GMT -5. The time now is 08:55 AM.