LinuxQuestions.org
Visit Jeremy's Blog.
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 11-01-2012, 09:38 AM   #1
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Rep: Reputation: Disabled
Bash: Image dimensions


Ive been trying to figure out a way to write some bash script that will record to a .txt file images over a certain amount of pixels. Ive been reading on ImageMagick but I was wanting to know if there is a better way to just code it out in bash?? I can write an if statement based on file size but I need it by dimensions. Can this be done and if so how??
 
Old 11-01-2012, 10:09 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Depending on what type of image they are, using 'file' may return the dimensions of the image.
 
Old 11-01-2012, 10:12 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
There are command line tools that can do this. Here's one that comes to mind:
Code:
$ identify image.jpg 
image.jpg JPEG 2000x2917 2000x2917+0+0 8-bit DirectClass 526KB 0.000u 0:00.000

# or more tailored to your demands:
$ identify -format "%w %h"  image.jpg
2000 2917
The format option is flexible, use the man page for other details.
 
1 members found this post helpful.
Old 11-01-2012, 10:49 AM   #4
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
There are command line tools that can do this. Here's one that comes to mind:
Code:
$ identify image.jpg 
image.jpg JPEG 2000x2917 2000x2917+0+0 8-bit DirectClass 526KB 0.000u 0:00.000

# or more tailored to your demands:
$ identify -format "%w %h"  image.jpg
2000 2917
The format option is flexible, use the man page for other details.
my apologies but its only my third week in bash and Linux but if I want to check the dimension of all jpegs would it be:

Code:
$ identify -format "%w %h"  /*.jpg >>results.txt
>> to print to .txt
 
Old 11-01-2012, 11:17 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
This should work:
Code:
identify -format "%f %w %h\n" *jpg > results.txt

# or if you want to append to the results.txt file:

identify -format "%f %w %h\n" *jpg >> results.txt
i've used 2 extra format options: %f reflect the full file name, \n makes sure every entry is on its own line (newline).

Have a look here for all the format options: http://www.imagemagick.org/script/escape.php
 
1 members found this post helpful.
Old 11-01-2012, 11:40 AM   #6
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
If i wanted to write in a loop for the sizes is this printing them as variables or integers??
 
Old 11-01-2012, 11:52 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by graphicsmanx1 View Post
If i wanted to write in a loop for the sizes is this printing them as variables or integers??
What do you mean by "printing as variables"? identify outputs the results as text.
 
Old 11-01-2012, 12:07 PM   #8
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
What do you mean by "printing as variables"? identify outputs the results as text.
yes I wanted to write a loop and echo the results if the images are more than a total number of pixels. When I research about loops it shows me two different options with either integers or strings

What Im trying to do is figure out how to use imageMagick to write bash script to tell me what images are to large based on pixels. I know how to do this with size but that's why I asked the question how to tell what the dimension is and if you echo what is it echoing an integer or string.

My idea is pull the height and width if they can be echoed as integers and times the integers. Run an >= on the size i want to flag.
 
Old 11-01-2012, 12:20 PM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by graphicsmanx1 View Post
My idea is pull the height and width if they can be echoed as integers and times the integers. Run an >= on the size i want to flag.
bash does not differentiate between integers and strings. Every variable is treated as a string, unless in math context. You can't make any program to output values "as integers". stdout is a character stream.

example:
Code:
#!/bin/bash

maxsize="2000000"
while read -r width height fname; do 
    ((width*height>maxsize)) && echo "$fname";
done < <(identify -format "%w %h %f\n" *jpg)
 
2 members found this post helpful.
Old 11-01-2012, 12:51 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what i do is resize the images regardless of size because it is easier than identifying those that are too big then going back and selectively resize certain files:
Code:
[schneidz@hyper ~]$ cat /var/www/html/estella-farm/pic-import.ksh 
#!/bin/bash

mkdir photos/bak; cp photos/*.* photos/bak
mogrify -resize 400x300 photos/*.* 

cp index.html index.bak
head -n 15 index.bak  > index.html

i=0
for pic in photos/*.*
do
 echo \<li\>\<img src=$pic width=500 height=300 alt=$pic /\>\</li\> >> index.html
 i=`expr $i + 1`
done

echo 			\</ul\> >> index.html
echo		\</div\> >> index.html
echo		\<div class=\"sliderbutton\"\>\<img src=\"images/right.gif\" width=\"32\" height=\"38\" alt=\"Next\" onclick=\"slideshow.move\(1\)\" /\>\</div\> >> index.html
echo	\</div\> >> index.html
echo	\<ul id=\"pagination\" class=\"pagination\"\> >> index.html

x=0
while [ $x -le $i ]
do
 echo  \<li onclick=\"slideshow.pos\($x\)\"\>`expr $x + 1`\</li\> >> index.html
 x=`expr $x + 1`
done

echo	\</ul\> >> index.html
echo \</div\> >> index.html
echo \<script type=\"text/javascript\"\> >> index.html
echo var slideshow=new TINY.slider.slide\(\'slideshow\',\{ >> index.html
echo	id:\'slider\', >> index.html
echo	auto:3, >> index.html
echo	resume:true, >> index.html
echo	vertical:false, >> index.html
echo	navid:\'pagination\', >> index.html
echo	activeclass:\'current\', >> index.html
echo	position:0 >> index.html
echo \}\)\; >> index.html
echo \</script\> >> index.html
echo \</body\> >> index.html
echo \</html\> >> index.html
 
Old 11-01-2012, 01:33 PM   #11
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
what i do is resize the images regardless of size because it is easier than identifying those that are too big then going back and selectively resize certain files:
Code:
[schneidz@hyper ~]$ cat /var/www/html/estella-farm/pic-import.ksh 
#!/bin/bash

mkdir photos/bak; cp photos/*.* photos/bak
mogrify -resize 400x300 photos/*.* 

cp index.html index.bak
head -n 15 index.bak  > index.html

i=0
for pic in photos/*.*
do
 echo \<li\>\<img src=$pic width=500 height=300 alt=$pic /\>\</li\> >> index.html
 i=`expr $i + 1`
done

echo 			\</ul\> >> index.html
echo		\</div\> >> index.html
echo		\<div class=\"sliderbutton\"\>\<img src=\"images/right.gif\" width=\"32\" height=\"38\" alt=\"Next\" onclick=\"slideshow.move\(1\)\" /\>\</div\> >> index.html
echo	\</div\> >> index.html
echo	\<ul id=\"pagination\" class=\"pagination\"\> >> index.html

x=0
while [ $x -le $i ]
do
 echo  \<li onclick=\"slideshow.pos\($x\)\"\>`expr $x + 1`\</li\> >> index.html
 x=`expr $x + 1`
done

echo	\</ul\> >> index.html
echo \</div\> >> index.html
echo \<script type=\"text/javascript\"\> >> index.html
echo var slideshow=new TINY.slider.slide\(\'slideshow\',\{ >> index.html
echo	id:\'slider\', >> index.html
echo	auto:3, >> index.html
echo	resume:true, >> index.html
echo	vertical:false, >> index.html
echo	navid:\'pagination\', >> index.html
echo	activeclass:\'current\', >> index.html
echo	position:0 >> index.html
echo \}\)\; >> index.html
echo \</script\> >> index.html
echo \</body\> >> index.html
echo \</html\> >> index.html
um wow.. I guess I need to research what you are doing. Way out from what Ive learned yet
 
Old 11-01-2012, 01:41 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
the significant line is highlighted in red.

the rest of it basically automatedly creates an index.html file for my webserver updating links to the new photo names.
 
Old 11-01-2012, 01:48 PM   #13
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
the significant line is highlighted in red.

the rest of it basically automatedly creates an index.html file for my webserver updating links to the new photo names.
but I dont want to resize I want to detect and print to file or as they call it echo

Last edited by graphicsmanx1; 11-01-2012 at 01:55 PM.
 
Old 11-01-2012, 02:15 PM   #14
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
ok, i misunderstood, druunas/ millgates advice seems appropriate.

Last edited by schneidz; 11-01-2012 at 02:22 PM.
 
Old 11-01-2012, 02:21 PM   #15
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
ok, i misunderstood, druuna's advice seems appropriate.
yes druuna and mills work great. Mills is exactly what I was looking for. Now I need to understand whats going on so I can learn from 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with my Dell Dimensions 2400 pc jberry46 Linux - Newbie 3 03-19-2008 09:12 PM
need video dimensions shanenin Programming 6 11-21-2005 06:54 PM
Coldfusion - getting image dimensions Locura Programming 2 02-15-2005 04:52 PM
Java arrays and dimensions pycoucou Programming 5 04-07-2004 01:59 PM
terminal dimensions and lilo nautilus_1987 Linux - General 1 10-22-2003 01:20 PM

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

All times are GMT -5. The time now is 01:09 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