LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2018, 10:09 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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
 
Old 10-14-2018, 10:43 PM   #2
Field95
LQ Newbie
 
Registered: Sep 2018
Location: xmpp:zemri@dismail.de
Posts: 13

Rep: Reputation: Disabled
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"
 
Old 10-15-2018, 08:10 AM   #3
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs down

Quote:
Originally Posted by Field95 View Post
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'.
 
Old 10-15-2018, 08:16 AM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

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.
 
Old 10-15-2018, 08:28 AM   #5
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
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
 
1 members found this post helpful.
Old 10-15-2018, 09:32 AM   #6
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs down

Quote:
Originally Posted by l0f4r0 View Post
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?
 
Old 10-15-2018, 10:11 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by GPGAgent View Post
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
 
1 members found this post helpful.
Old 10-15-2018, 12:42 PM   #8
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by l0f4r0 View Post
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
 
Old 10-15-2018, 12:59 PM   #9
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by l0f4r0 View Post
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 $
 
Old 10-15-2018, 01:18 PM   #10
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Wink

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....
 
Old 10-16-2018, 02:51 AM   #11
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by GPGAgent View Post
Excellent- that works just fine
Glad it works

Quote:
Originally Posted by GPGAgent View Post
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".
 
Old 10-16-2018, 12:36 PM   #12
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by l0f4r0 View Post
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
 
Old 10-16-2018, 05:21 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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!
 
Old 10-17-2018, 12:47 PM   #14
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by astrogeek View Post
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!

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?

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to add text on image files in bash script bal_nair1 Programming 10 07-26-2012 05:55 AM
Inserting a block of text into a text file on system boot krptodr Linux - Newbie 5 02-14-2012 07:11 PM
Need a script to break a block of text on to different lines. Thaidog Programming 13 12-17-2010 04:46 AM
Script that puts text on an image? JohnGraham Linux - Software 2 08-27-2010 07:46 AM
LXer: Audio / Video / Image / Text / ISO Converter Nautilus Script LXer Syndicated Linux News 0 06-14-2009 06:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration