|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
||||
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; 27th October 2017 at 02:26 AM. Reason: Corrected JPG resize command. Fixed pngcrush command. Changed shell to ksh. |
|
||||
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 |
|
||||
You might want to get the current version. I made some adjustments.
|
|
||||
Quote:
Quote:
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; 27th October 2017 at 02:39 AM. Reason: Forgot that [code] can't be used inline. |
|
||||
Probably? Try definitely. However, I'll give Alpine a shot the next time I need a Linux machine. Thanks again.
|
|
||||
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 |
|
||||
Quote:
|
|
|||
Quote:
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! |
|
||||
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 |
|
||||
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.
__________________
Are you infected with Wetiko? |
|
||||
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 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 |
|
||||
Quote:
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 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 $ |
|
||||
Quote:
$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 |
|
||||
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 $ Code:
#print "Your welcome, ${name}." #elif [ $name = "Hanna" ];then #print "Hello, ${name}, who are you?" #else 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 PapaParrot; 1st November 2017 at 05:49 PM. |
|
||||
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 |
Tags |
bash, image processing, imagemagic, jpegoptim |
Thread Tools | |
Display Modes | |
|
|
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 |