LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-05-2024, 01:40 PM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
Help with bash script using identify from imagemagick


I want to get the number of different colours in each image from a folder with images.

identify will give me the sizes:

Quote:
pedro@pedro-HP:~/Downloads$ identify -format %k /home/pedro/Downloads/what_does_this_say.jpg
34089pedro@pedro-HP:~/Downloads$
The jpg what_does_this_say.jpg has 34089 different colours.

Now I want to save all the image names and sizes in an array, or better, a text file, each image and colour count on a new line.

I don't know how bash reads the files in, that is to say, I don't know which file would be first in a list and how the list is ordered, so an array name:colour count would be best.

I thought I could do this, but it doesn't work:

Quote:
path2files="/home/pedro/Downloads/"
for f in $path2files*.jpg;
do
echo "$f"
size = identify -format %k $f
echo "$size"
done
I get this error for each file: size: invalid option -- 'r'

Quote:
/home/pedro/Downloads/what_does_this_say.jpg
size: invalid option -- 'r'
Usage: size [option(s)] [file(s)]
Displays the sizes of sections inside binary files
If no input file(s) are specified, a.out is assumed
The options are:
-A|-B|-G --format={sysv|berkeley|gnu} Select output style (default is berkeley)
-o|-d|-x --radix={8|10|16} Display numbers in octal, decimal or hex
-t --totals Display the total sizes (Berkeley only)
--common Display total size for *COM* syms
--target=<bfdname> Set the binary file format
@<file> Read options from <file>
-h --help Display this information
-v --version Display the program's version

size: supported targets: elf64-x86-64 elf32-i386 elf32-iamcu elf32-x86-64 pei-i386 pe-x86-64 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big elf32-little elf32-big pe-bigobj-x86-64 pe-i386 srec symbolsrec verilog tekhex binary ihex plugin
Any tips please?
 
Old 02-05-2024, 01:58 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,706

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
Code:
size=$(identify -format %k "$f")
You can not have spaces between the = in a statement, use $() which is called command substitution and put quotes around the file name variable.

To check script syntax use https://www.shellcheck.net/

To save the file name:color you can use a redirect i.e.

echo "$f:$size" >> /path/to/file

The ">>" appends the line to the end of the file.
 
1 members found this post helpful.
Old 02-06-2024, 02:30 AM   #3
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks a lot!

I used this:

Quote:
#!/bin/bash
path2files="/home/pedro/Downloads/"
for f in $path2files*.jpg;
do
echo "$f"
# this works
colours=$(identify -format %k $f)
echo "$colours"
echo "$f:$colours" >> /home/pedro/Downloads/colours.txt
done
it worked great!

Is it possible to tell bash to rewrite the file from scratch if I do this again? Or create the file new each time?

The second time I tried, bash append to the existing file.

Later, I want to use Python PIL to get the colour count of each colour: read in the text file, split each line on : /home/pedro/Downloads/1695363058057.jpg:70419

Code:
data = line.split(':')
img = data.split[0]
num = data.split[1]
# now set maxcolors = num
im = Image.open(img).convert("RGB")
im1 = Image.Image.getcolors(im, maxcolors=num)# returns a list of tuples (count, colour)
 
Old 02-06-2024, 02:45 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,706

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
To truncate or create empty file add before loop

: > /path/to/colours.txt
 
1 members found this post helpful.
Old 02-07-2024, 04:16 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Each >> is an open+append+write+close
The following is smarter and also allows to overwrite with a >
Code:
path2files="/home/pedro/Downloads/"
for f in "$path2files"*.jpg
do
  echo "$f"
  colours=$(identify -format %k "$f")
  echo "$colours"
  echo "$f:$colours" >&3
done 3> home/pedro/Downloads/colours.txt
The loop block gets a descriptor 3 that is directed to the output file.
The output file is opened when the loop starts, and is closed when the loop ends.

BTW
echo "$colours" >&2
would write to descriptor 2 that is by default directed to stderr.
And
echo "$colours"
is like
echo "$colours" >&1
(descriptor 1, directed to stdout, is the default)

Last edited by MadeInGermany; 02-07-2024 at 04:33 AM.
 
1 members found this post helpful.
Old 02-17-2024, 12:48 PM   #6
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Sorry, only just saw your reply!

Thanks!

Your script works great, just a / missing before file path, no biggie, bash complained, I could see the problem, fixed it!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in bash script to identify files from last month blason Programming 5 02-25-2016 08:02 AM
LXer: Identify PCI and USB Wired and Wireless Driver in Linux – Identify PCI Driver. Ubuntu, Debian, LXer Syndicated Linux News 0 08-20-2014 07:21 AM
install ImageMagick-6.5.1-2 & ImageMagick-devel-6.5.1.2 in red hat mokkai Linux - Enterprise 4 04-16-2009 12:04 PM
How to read "identify" button press event, or state of "identify" blue led with IPMI? iav Linux - Server 0 01-27-2009 01:13 PM
Bash Script Help (ImageMagick) embsupafly Programming 2 07-11-2005 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:23 AM.

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