DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 3 Weeks Ago
demifiend's Avatar
demifiend demifiend is offline
Real Name: Matthew Graybosch
Silent Protagonist
 
Join Date: Oct 2017
Location: Harrisburg, PA
Posts: 29
Default bash script to resize and optimize images using ImageMagick and jpegoptim

GarryR requested that I share this in the "Your Favorite Programing Language" thread, so here it is. There's some code for PNG files that also uses pngcrush, but it still needs work.

Code:
#!/usr/bin/env ksh

# © 2017 Matthew Graybosch (public@matthewgraybosch.com)
# Released under the 3-Clause BSD License
# See https://opensource.org/licenses/BSD-3-Clause for details

# NEVER RUN THIS AS PART OF THE BUILD PROCESS ON TRAVIS CI! 
# IT TAKES TOO LONG AND CAUSES TIMEOUTS.
# RUN THIS SCRIPT LOCALLY, COMMIT, AND PUSH TO MASTER.

# We need to resize and compress images for display. 
# Script based on "Efficient Image Resizing With ImageMagick" by Dave Newton
# https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/

function processJPG {
  for img in ${1}*.jpg
  do
    convert $img -resize ${2} -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -define jpeg:fancy-upsampling=off -colorspace sRGB $img;
    jpegoptim --all-progressive --strip-all $img;
  done
}

function processPNG {
  for img in ${1}*.png
  do
	convert $img -resize ${2} -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -define png:fancy-upsampling=off $img;
	pngcrush -d ${3} -brute -reduce $img;
  done
}

echo "STARTED image processing..."
export INPUT_PATH=./images/incoming/
export ORIGINALS=./images/originals/
export TEMP=./images/tmp/
export OUTPUT_PATH=./assets/images/
export OUTPUT_WIDTH=768

echo "Copying images to processing directories..."
cp ${INPUT_PATH}* $OUTPUT_PATH
cp ${INPUT_PATH}* $TEMP
mv ${INPUT_PATH}* $ORIGINALS

echo "Resizing images..."
processJPG $OUTPUT_PATH $OUTPUT_WIDTH;
processPNG $TEMP $OUTPUT_WIDTH $OUTPUT_PATH;

echo "FINISHED image processing..."
__________________
OpenBSD 6.2-RELEASE on Thinkpad T430s, ThinkCentre M92P, and Clevo W24AEU
Web | Keybase | Mastodon | GitHub
"What happens in the TARDIS stays in the TARDIS."

Last edited by demifiend; 3 Weeks Ago at 02:26 AM. Reason: Corrected JPG resize command. Fixed pngcrush command. Changed shell to ksh.
Reply With Quote
  #2   (View Single Post)  
Old 3 Weeks Ago
GarryR's Avatar
GarryR GarryR is offline
Real Name: Garry Ricketson
Package Pilot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 190
Default

Thanks,
Some others might find it use full as well. I will let you know how it works out. Looks like a pretty simple
script.
__________________
My best friends are parrots
Reply With Quote
  #3   (View Single Post)  
Old 3 Weeks Ago
Oko's Avatar
Oko Oko is offline
Rc.conf Instructor
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 1,101
Default

Quote:
Originally Posted by demifiend View Post
GarryR requested that I share this in the "Your Favorite Programing Language" thread, so here it is. There's some code for PNG files that also uses pngcrush, but it still needs work.
I hate to be the spoiler but most BSD users believe that bash stands for "broken again shell" with despicable security record. Also when it comes to batch image processing GraphicsMagick beats ImageMagick by a mile. Personally I consider ImageMagick a viral GNU dependency just like bash, gmake, autoconf, and CUPS.
Reply With Quote
  #4   (View Single Post)  
Old 3 Weeks Ago
demifiend's Avatar
demifiend demifiend is offline
Real Name: Matthew Graybosch
Silent Protagonist
 
Join Date: Oct 2017
Location: Harrisburg, PA
Posts: 29
Default

Quote:
Originally Posted by GarryR View Post
Thanks,
Some others might find it use full as well. I will let you know how it works out. Looks like a pretty simple
script.
You might want to get the current version. I made some adjustments.
__________________
OpenBSD 6.2-RELEASE on Thinkpad T430s, ThinkCentre M92P, and Clevo W24AEU
Web | Keybase | Mastodon | GitHub
"What happens in the TARDIS stays in the TARDIS."
Reply With Quote
  #5   (View Single Post)  
Old 3 Weeks Ago
demifiend's Avatar
demifiend demifiend is offline
Real Name: Matthew Graybosch
Silent Protagonist
 
Join Date: Oct 2017
Location: Harrisburg, PA
Posts: 29
Default

Quote:
Originally Posted by Oko View Post
I hate to be the spoiler but most BSD users believe that bash stands for "broken again shell" with despicable security record.
That's probably a fair assessment. However, as I explained in the other thread, I originally wrote this on a Linux machine. I've since updated the script to use ksh since it works in that shell, too.

Quote:
Originally Posted by Oko View Post
Also when it comes to batch image processing GraphicsMagick beats ImageMagick by a mile.
Thanks for the tip. I'll post an updated version of this script once I've taken time to RTFM. It seems gm convert doesn't have exactly the same options as convert.

Quote:
Originally Posted by Oko View Post
Personally I consider ImageMagick a viral GNU dependency just like bash, gmake, autoconf, and CUPS.
I never worried too much about that when I was using Linux, but now that I've switched to BSD I suppose I should be more careful. Thanks for taking time to examine my little script.
__________________
OpenBSD 6.2-RELEASE on Thinkpad T430s, ThinkCentre M92P, and Clevo W24AEU
Web | Keybase | Mastodon | GitHub
"What happens in the TARDIS stays in the TARDIS."

Last edited by demifiend; 3 Weeks Ago at 02:39 AM. Reason: Forgot that [code] can't be used inline.
Reply With Quote
  #6   (View Single Post)  
Old 3 Weeks Ago
Oko's Avatar
Oko Oko is offline
Rc.conf Instructor
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 1,101
Default

Quote:
Originally Posted by demifiend View Post
I never worried too much about that when I was using Linux, but now that I've switched to BSD I suppose I should be more careful. Thanks for taking time to examine my little script.
You have probably used wrong Linux distro. Hint: Alpine Linux
Reply With Quote
  #7   (View Single Post)  
Old 3 Weeks Ago
demifiend's Avatar
demifiend demifiend is offline
Real Name: Matthew Graybosch
Silent Protagonist
 
Join Date: Oct 2017
Location: Harrisburg, PA
Posts: 29
Default

Quote:
Originally Posted by Oko View Post
You have probably used wrong Linux distro. Hint: Alpine Linux
Probably? Try definitely. However, I'll give Alpine a shot the next time I need a Linux machine. Thanks again.
__________________
OpenBSD 6.2-RELEASE on Thinkpad T430s, ThinkCentre M92P, and Clevo W24AEU
Web | Keybase | Mastodon | GitHub
"What happens in the TARDIS stays in the TARDIS."
Reply With Quote
  #8   (View Single Post)  
Old 3 Weeks Ago
GarryR's Avatar
GarryR GarryR is offline
Real Name: Garry Ricketson
Package Pilot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 190
Default

The 1st script worked ok for me, using ksh. I don't have GraphicsMagick installed but may give it a try some time.
Something I wonder, how could I get it to add tmb to the name of the resized image.
So for example: parrot.jpg is parrot-tmb.jpg , the original still being parrot.jpg.
On one image at a time , it is easy enough. But with several images, I am not sure how
to tell it to add tmb to the resized images. Is that possible, ?
Thanks
__________________
My best friends are parrots
Reply With Quote
  #9   (View Single Post)  
Old 3 Weeks Ago
demifiend's Avatar
demifiend demifiend is offline
Real Name: Matthew Graybosch
Silent Protagonist
 
Join Date: Oct 2017
Location: Harrisburg, PA
Posts: 29
Default

Quote:
Originally Posted by GarryR View Post
Something I wonder, how could I get it to add tmb to the name of the resized image.
So for example: parrot.jpg is parrot-tmb.jpg , the original still being parrot.jpg.
On one image at a time , it is easy enough. But with several images, I am not sure how
to tell it to add tmb to the resized images. Is that possible, ?
It should be possible. ksh(1) has a "Parameters" section that details how to manipulate strings such as file names. I'll have to tinker with it later.
__________________
OpenBSD 6.2-RELEASE on Thinkpad T430s, ThinkCentre M92P, and Clevo W24AEU
Web | Keybase | Mastodon | GitHub
"What happens in the TARDIS stays in the TARDIS."
Reply With Quote
Old 3 Weeks Ago
Beastie Beastie is offline
Daemonology student
 
Join Date: Jan 2009
Location: /dev/earth0
Posts: 292
Default

Quote:
Originally Posted by GarryR View Post
Something I wonder, how could I get it to add tmb to the name of the resized image.
So for example: parrot.jpg is parrot-tmb.jpg , the original still being parrot.jpg.
Code:
for file in *jpg; do
    new_file=`echo $file | sed 's/.jpg$//'`
    convert ... $file $new_file-tmb.jpg
    jpegoptim ... $new_file-tmb.jpg
done
__________________
May the source be with you!
Reply With Quote
Old 3 Weeks Ago
GarryR's Avatar
GarryR GarryR is offline
Real Name: Garry Ricketson
Package Pilot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 190
Default

Thanks, I was trying various things my self, I did a search and found something that
add the date and time, but when I tried to modify it to just add "tmb" it all ways error.
I see now, what you did makes sense,..
Thanks a bunch
__________________
My best friends are parrots
Reply With Quote
Old 3 Weeks Ago
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Mostly Harmless
 
Join Date: Dec 2015
Location: London
Posts: 73
Default

Quote:
Originally Posted by demifiend View Post
Code:
#!/usr/bin/env ksh
I do believe that could be replaced with a /bin/sh shebang to make the script more portable, there don't seem to be any bashisms.
__________________
Linux is for people who hate Windows. BSD is for people who love UNIX.
Reply With Quote
Old 2 Weeks Ago
GarryR's Avatar
GarryR GarryR is offline
Real Name: Garry Ricketson
Package Pilot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 190
Default

I don't usually even use the "shebang", here is a script I use to load my background image:
Code:
display -window root /home/garry/Images/backgrounds/b-background.jpg
As long as I am in my /home/garry dir, I just type in "background" and it loads the pre set image.
I do realize with the "shebang" I could make it where it runs no matter what dir I am in, but
any way, I am wondering if maybe "demifiend" or "Beastie", or anyone else for that matter,
Wondering if anyone knows how to maybe add some input option, so that it prompts me to
put in the name of the image I want as a background.
I know, maybe if I study enough on writing shell scripts, and some searching I might figure it out one day, but any way, quite often when some one shows me a actual script I learn from that as well.
Ideally, this would prompt "enter name of image" or something like that, if nothing is
entered, it would just load the image previously entered.
Thanks
__________________
My best friends are parrots
Reply With Quote
Old 2 Weeks Ago
hanzer's Avatar
hanzer hanzer is offline
Real Name: Adam Jensen
just passing through
 
Join Date: Oct 2013
Location: EST USA
Posts: 267
Default

Quote:
Originally Posted by GarryR View Post
[snip]
Wondering if anyone knows how to maybe add some input option, so that it prompts me to
put in the name of the image I want as a background.
[snip]
This isn't what you asked for - it doesn't simulate a dialog - but the method might get the job done in a more straightforward manner...

How can I pass a command line argument into a shell script?

Example:
$HOME/bin/test.ksh
Code:
#!/bin/ksh
echo $0
echo $1
echo $2
Demo:
Code:
hanzer@lucidrine:/home/hanzer:859 $ test.ksh                                                              
/home/hanzer/bin/test.ksh


hanzer@lucidrine:/home/hanzer:860 $ test.ksh foo
/home/hanzer/bin/test.ksh
foo

hanzer@lucidrine:/home/hanzer:861 $ test.ksh foo bar
/home/hanzer/bin/test.ksh
foo
bar
hanzer@lucidrine:/home/hanzer:862 $ test.ksh foo bar baz
/home/hanzer/bin/test.ksh
foo
bar
hanzer@lucidrine:/home/hanzer:863 $
Reply With Quote
Old 2 Weeks Ago
hanzer's Avatar
hanzer hanzer is offline
Real Name: Adam Jensen
just passing through
 
Join Date: Oct 2013
Location: EST USA
Posts: 267
Default

Quote:
Originally Posted by GarryR View Post
I don't usually even use the "shebang", here is a script I use to load my background image:
Code:
display -window root /home/garry/Images/backgrounds/b-background.jpg
As long as I am in my /home/garry dir, I just type in "background" and it loads the pre set image.
I do realize with the "shebang" I could make it where it runs no matter what dir I am in, but
any way, I am wondering if maybe "demifiend" or "Beastie", or anyone else for that matter,
Wondering if anyone knows how to maybe add some input option, so that it prompts me to
put in the name of the image I want as a background.
I know, maybe if I study enough on writing shell scripts, and some searching I might figure it out one day, but any way, quite often when some one shows me a actual script I learn from that as well.
Ideally, this would prompt "enter name of image" or something like that, if nothing is
entered, it would just load the image previously entered.
Thanks
This might get you started:

$HOME/bin/background
Code:
#!/bin/ksh
PS3='Pick one of the above: '
TMOUT=10
select i in list choose quit
do      case $i in 
        list)   ls ~/Images/backgrounds;;
        choose) read fname; display -window root ~/Images/backgrounds/${fname};;    
        quit)   break;;
        "")     print -u2 You must select one of the above;;
        esac
done
Reply With Quote
Old 2 Weeks Ago
GarryR's Avatar
GarryR GarryR is offline
Real Name: Garry Ricketson
Package Pilot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 190
Default

Thanks Hanzer,
The first script you showed, was not close, and I couldn't make much sense of it,...
this second one, is very close to what I just now did, using this as a guide:
http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html
Code:
ls /home/garry/Images/backgrounds
print -n "Background file Name: "; read name; print ""
if [ $name = "" ];then
print "Your welcome, ${name}."
elif [ $name = "Hanna" ];then
print "Hello, ${name}, who are you?"
else
display  -window root /home/garry/Images/backgrounds/${name}
fi
Code:
$ input 
a-guako-background.jpg        a-openbsd-busy.gif            background-3.jpg
a-guako-background1.jpg       a-openbsd-notbusy.gif         background-4-plain.gif
a-guako-background2.jpg       b-background.jpg
a-guako-background3.jpg       background-2.jpg
Background file Name: b-background.jpg

$
Does what I want, how ever a really odd thing, or at least to me, these lines:
Code:
#print "Your welcome, ${name}."
#elif [ $name = "Hanna" ];then
#print "Hello, ${name}, who are you?"
#else
Don't appear to do anything, but when I removed them, it does not work. The background does not change when I paste in a different one.
I am going to try yours now.
thanks
=============edited====
Yours works perfect, and does not have any unneeded lines, thanks
__________________
My best friends are parrots

Last edited by GarryR; 2 Weeks Ago at 05:49 PM.
Reply With Quote
Old 2 Weeks Ago
hanzer's Avatar
hanzer hanzer is offline
Real Name: Adam Jensen
just passing through
 
Join Date: Oct 2013
Location: EST USA
Posts: 267
Default

It seems to me like a bizarre way to do things but, hey, to each his own. Maybe something like this is the intention:
Code:
ls /home/garry/Images/backgrounds
print -n "Background file Name: "; read name
if [[ $name = "" ]]; then
    print "No change, exiting."
else
    display  -window root /home/garry/Images/backgrounds/${name}
fi
Reply With Quote
Old 2 Weeks Ago
hanzer's Avatar
hanzer hanzer is offline
Real Name: Adam Jensen
just passing through
 
Join Date: Oct 2013
Location: EST USA
Posts: 267
Default

Quote:
Originally Posted by GarryR View Post
=============edited====
Yours works perfect, and does not have any unneeded lines, thanks
Cool. I noticed that TMOUT=10 doesn't seem to work as expected when using /bin/ksh but it does work when using /usr/local/bin/ksh93.
Reply With Quote
Reply

Tags
bash, image processing, imagemagic, jpegoptim

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script cannot execute - Is a directory guitarscn Programming 8 6th November 2010 10:25 PM
[Solved] How to make 2 separate arguments in 1 bash script? guitarscn Programming 1 31st August 2010 09:12 PM
ImageMagick-6.2.9.8 carpman FreeBSD Ports and Packages 4 20th September 2008 03:48 PM
How to optimize FreeBSD 7.0 ? Sunsawe FreeBSD General 17 2nd June 2008 08:24 PM
libxcb, ImageMagick... lumiwa FreeBSD Ports and Packages 2 15th May 2008 11:47 PM


All times are GMT. The time now is 11:28 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick