[SOLVED] Help with bash script using identify from imagemagick
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.