| View previous topic :: View next topic |
| Author |
Message |
ColeSlaw Apprentice


Joined: 19 Sep 2003 Posts: 169 Location: Kearney, NE USA
|
Posted: Sun Apr 27, 2008 10:21 am Post subject: Bash Script - Imagemagick Resizing for Digital Pictu[solved] |
|
|
I'm trying to write a bash script, and I can't figure out what I need to do here.
I have a directory for images of varying sizes that are displayed on my personal webpage with linpha. They are several different sizes and orientations. I recently bought a digital picture frame that takes an SD card. I want to copy all the images in my "picture" directory to the SD card, but the digital frame doesn't display resolutions larger then 640x480.
The goal of my bash script is to list all the jpgs under the main directory, identify if they are vertical or horizonal, convert them to 640x480 or 480x640 accordingly, and then place them in a directory under my home folder for easy copying to an SD card. Here is the code I have so far:
| Code: | #!/bin/sh
for picture in `find -name "*.[jJ][pP][gG]"`
do
W=`identify -format "%w" $picture`
H=`identify -format "%h" $picture`
echo $picture
echo $W
echo $H
if [ "$W" -gt "$H" ]
then
# echo "Horizontal"
convert -resize 640x480> $picture "/home/cbrodine/Digital_Frame/${picture/.jpg}"
else
# echo "Vertical"
convert -resize 480x640> $picture "/home/cbrodine/Digital_Frame/${picture/.jpg}"
fi
done |
I get this output when I try to run it:
| Code: | ./IMG_0940.JPG
1600
1200
convert: missing an image filename `/home/cbrodine/Digital_Frame/./IMG_0940.JPG'.
./IMG_0941.JPG
1200
1600
convert: missing an image filename `/home/cbrodine/Digital_Frame/./IMG_0941.JPG'.
./IMG_0942.JPG
1600
1200
convert: missing an image filename `/home/cbrodine/Digital_Frame/./IMG_0942.JPG'.
./IMG_0943.JPG
1600
1200 |
It looks like it is trying to leave the "./" in the filename, but I just can't figure out how to get rid of it there.
Any help you can provide would be greatly appreciated. Please be gentle. I'm a scripting n00b. _________________ My Folding@home Stats!
Join the GLUE folding Team!
Last edited by ColeSlaw on Mon Apr 28, 2008 4:18 pm; edited 1 time in total |
|
| Back to top |
|
 |
jcat Veteran


Joined: 26 May 2006 Posts: 1304
|
Posted: Sun Apr 27, 2008 12:39 pm Post subject: |
|
|
There are 101 ways doing this.
As a quick and dirty fix you could use cut.
So instead of
| Code: | | for picture in `find -name "*.[jJ][pP][gG]"` |
you try
| Code: | | for picture in `find -name "*.[jJ][pP][gG]" |cut -c3-` |
That prints everything from charecter 3 onwards, in other words it chops off the first 2 characters.
I'm sure you'll get a few more (and better) suggestions!
Cheers,
jcat |
|
| Back to top |
|
 |
The Unknown Guru

Joined: 28 Feb 2007 Posts: 335 Location: Minnesota, U.S.A
|
Posted: Sun Apr 27, 2008 1:53 pm Post subject: |
|
|
Here's another way
| Code: | | convert -resize '640x480>' $picture "/home/cbrodine/Digital_Frame/${picture##*/}" |
The ${picture##*/} will strip the maximum amount of prefix matching pattern "*/" _________________ Doing what you like is freedom.
Liking what you do is happiness. |
|
| Back to top |
|
 |
Earthwings Administrator


Joined: 14 Apr 2003 Posts: 7419 Location: Karlsruhe, Germany
|
Posted: Sun Apr 27, 2008 2:08 pm Post subject: |
|
|
| Code: | #!/bin/bash
find . -name "*.[jJ][pP][gG]" -print0 | while read -rd $'\0' picture
do
size=( $(identify -format "%w %h" "${picture}") )
echo $picture
echo ${size[0]}
echo ${size[1]}
if [ ${size[0]} -gt ${size[1]} ]
then
# echo "Horizontal"
convert -resize '640x480>' "${picture}" "/home/cbrodine/Digital_Frame/$(basename "${picture}")"
else
# echo "Vertical"
convert -resize '480x640>' "${picture}" "/home/cbrodine/Digital_Frame/$(basename "${picture}")"
fi
done |
Couple of changes -- uses bash for the read parameter (it won't crap out on files with funny names then) and uses basename to the filename. _________________ KDE 4.2 - Get It While It's Hot!
Last edited by Earthwings on Sun Apr 27, 2008 2:09 pm; edited 1 time in total |
|
| Back to top |
|
 |
ColeSlaw Apprentice


Joined: 19 Sep 2003 Posts: 169 Location: Kearney, NE USA
|
Posted: Sun Apr 27, 2008 2:09 pm Post subject: |
|
|
| The Unknown wrote: | Here's another way
| Code: | | convert -resize '640x480>' $picture "/home/cbrodine/Digital_Frame/${picture##*/}" |
The ${picture##*/} will strip the maximum amount of prefix matching pattern "*/" |
I tried it out, and it works great! Thanks for the help!
My skills in bash have be increased by +1 thanks to you. _________________ My Folding@home Stats!
Join the GLUE folding Team! |
|
| Back to top |
|
 |
waltercool n00b

Joined: 27 Jan 2008 Posts: 33
|
Posted: Mon Apr 28, 2008 12:29 pm Post subject: |
|
|
Hi there
I have created my bash image resize script.
You can use a specific width and heigth, and cannot be lower.
Ex: If you want 1024x768, and the image is 2048x768, the size will not change, because cannot be lower than heigth, same for width. Resize is proportional.
Here is the script: http://weblogs.inf.udp.cl/waltercool/2008/04/28/65/en/ _________________ EEE PC 701 White Powered By Gentoo, designed for KDE
900Mhz, 2Gb RAM, Intel 915 with 256 shared 4Gb SDHC + 160Gb HDD |
|
| Back to top |
|
 |
|