Gentoo Forums
Gentoo Forums
Quick Search: in
Bash Script - Imagemagick Resizing for Digital Pictu[solved]
View unanswered posts
View posts from last 24 hours

rackathon
 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
ColeSlaw
Apprentice
Apprentice


Joined: 19 Sep 2003
Posts: 169
Location: Kearney, NE USA

PostPosted: Sun Apr 27, 2008 10:21 am    Post subject: Bash Script - Imagemagick Resizing for Digital Pictu[solved] Reply with quote

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
View user's profile Send private message
jcat
Veteran
Veteran


Joined: 26 May 2006
Posts: 1304

PostPosted: Sun Apr 27, 2008 12:39 pm    Post subject: Reply with quote

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
View user's profile Send private message
The Unknown
Guru
Guru


Joined: 28 Feb 2007
Posts: 335
Location: Minnesota, U.S.A

PostPosted: Sun Apr 27, 2008 1:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
Earthwings
Administrator
Administrator


Joined: 14 Apr 2003
Posts: 7419
Location: Karlsruhe, Germany

PostPosted: Sun Apr 27, 2008 2:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
ColeSlaw
Apprentice
Apprentice


Joined: 19 Sep 2003
Posts: 169
Location: Kearney, NE USA

PostPosted: Sun Apr 27, 2008 2:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
waltercool
n00b
n00b


Joined: 27 Jan 2008
Posts: 33

PostPosted: Mon Apr 28, 2008 12:29 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum