08-13-2012, 06:42 PM | #1 |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
Script to resize images to 2 Million Pixels
Hi all!
A friend an I cobbled together a very rough script for OSX to resize large images down to 2 Million Pixels to conform with iBookstore Asset Guide 4.7 Rev.3:
I also used platypus to convert it to an OSX-App. Download App here: http://mmsizr.googlecode.com/files/MMSizr.app.zip Usage:
Original shell-Script: Code:
#!/bin/sh MAXsize=1900000; for v in `find $1/*.jpeg $1/*.jpg $1/*.gif $1/*.png`; do h=`sips -g pixelHeight $v | tail -1 | sed "s/.* //"` w=`sips -g pixelWidth $v | tail -1 | sed "s/.* //"` pxlnmbr=`echo "$h*$w" | bc`; echo "Number of Pixels in $v ($w x $h) is $pxlnmbr"; if [[ $anzahl -gt $MAXsize ]]; then factor=`echo "scale=5 ; sqrt($MAXsize / $pxlnmbr) " | bc`; echo "factor: $factor"; newWidth=`echo "scale=5 ; $w * $factor" | bc`; newHeight=`echo "scale=5 ; $h * $factor" | bc`; echo "$v new: $newWidth x $newHeight"; sips -z $newHeight $newWidth $v echo " "; fi done http://mmsizr.googlecode.com/files/mmsizr.sh.zip Project page: https://code.google.com/p/mmsizr/ --edit-- Download App here: http://mmsizr.googlecode.com/files/MMSizr.app.zip Consider Flattering this article, if you liked this app: http://suntoucher.de/mmsizr-a-script...illion-pixels/ Last edited by shorshe; 09-05-2012 at 06:53 AM. |
08-15-2012, 12:41 PM | #2 |
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
imagemagick provides the Pixel Area Count Limit ('@' flag), that is useful in combination with the -resize command:
http://www.imagemagick.org/Usage/resize/#pixel and it automatically takes care of the scaling issues (for your intended use, combine it with the '>' flag). |
Advert | |
|
08-15-2012, 02:34 PM | #3 |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
Yeah imagemagick would be great for this. But unfortunately it's not easy to install or bundle in an app. I needed a "ready to use out of the box" solution.
I tried to do a static build of imagemagick's "convert" for osx. No luck so far. Do you have any experience in this field? |
08-15-2012, 02:53 PM | #4 |
Linux User
Posts: 2,279
Karma: 6123806
Join Date: Sep 2010
Location: Heidelberg, Germany
Device: none
|
surely there's already an osx port for imagemagick?
|
08-16-2012, 04:49 AM | #5 |
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
Sorry, I do not own a Mac OS box.
There are several discussions in sites like StackOverflow about compiling imagemagick from sources on OS X. You might find useful the following: http://stackoverflow.com/questions/7...x-lion-trouble |
Advert | |
|
08-16-2012, 11:00 AM | #6 |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
Yes there is a port, which kann be installed via "homebrew" packet manager.
But I would love to give the users a self-contained app, without any extra installs. This would require to statically compile imagemagick and bundle it inside the app. A Static compile of imagemagick seems to be a tricky task: --- Edit ----- http://www.imagemagick.org/discourse...ic.php?t=13145 -------------- Apart from that I don't even know if the license of every dependency would allow that. Last edited by shorshe; 08-22-2012 at 09:24 AM. |
08-20-2012, 07:17 AM | #7 | |
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
Quote:
It is a shame it lacks many powerful options already included in imagemagick, though. |
|
06-07-2013, 11:36 PM | #8 |
eBook Designer
Posts: 26
Karma: 10
Join Date: Jun 2010
Location: New Jersey
Device: ipad, nook, kindle
|
Wow. This script just made my night. Thanks.
|
06-14-2013, 10:42 AM | #9 |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
nice
nice
|
06-14-2013, 01:16 PM | #10 |
Enthusiast
Posts: 28
Karma: 5010
Join Date: Apr 2011
Device: none
|
Why do you need a script for this? Can you control the re-sampling quality? Image quality is horrible in most epubs. I understand the need for automation but you give up so much on quality. I especially hate how InDesign export images!
My process is simply create a Photoshop action (all the quality control I want) and then check it again quickly via Bridge in metadata view. |
06-14-2013, 03:01 PM | #11 |
Fanatic
Posts: 580
Karma: 810184
Join Date: Sep 2010
Location: Norway
Device: prs-t1, tablet, Nook Simple, assorted kindles, iPad
|
Not sure if you consider this an improvement – oh well...
Code:
#!/bin/bash MAXsize=1900000; while read v; do h=`sips -g pixelHeight "$v" | sed -n '${s/.* //;p}'` w=`sips -g pixelWidth "$v" | sed -n '${s/.* //;p}'` pxlnmbr=$(( $h * $w)); echo "Number of Pixels in $v ($w x $h) is $pxlnmbr"; if [[ $pxlnmbr -gt $MAXsize ]]; then factor=`echo "sqrt($MAXsize *100^2/ $pxlnmbr) " | bc`; echo "factor: $factor%"; newWidth=$(( $w * $factor / 100 )) newHeight=$(( $w * $factor / 100 )) echo "$v new: $newWidth x $newHeight"; sips -z $newHeight $newWidth "$v" echo " "; fi done < <(find $1/ -name "*.jpeg" -or -name "*.jpg" -or -name "*.gif" -or -name "*.png") By using a while loop instead of a for loop, file names with spaces are permitted (can't remember if they're permitted according to ePub specs, though), and enclose $v in "". Sticking the find clause at the end is for a very good reason, but a very complicated technical one that I cannot recall just at the moment... None of the rest can strictly be called improvements, rather just a demonstration of "there's more than one way to do it", and my own preferences (or prejudices?) for making scripts compact (or do I mean unreadable?) For simple integer arithmetic, this can be done directly in the shell within $(( ... )) parenthesis, I therefore express the resizing factor as a %; two-digit accuracy should be sufficient for the intended purpose. Don't have a fruit machine, so I haven't been able to test it properly. |
06-21-2013, 03:56 AM | #12 | |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
Quote:
But you are right. Quality can always be improved. As long as you get paid for it... Last edited by shorshe; 06-24-2013 at 03:55 AM. |
|
06-21-2013, 04:10 AM | #13 | |
Enthusiast
Posts: 28
Karma: 10
Join Date: Aug 2012
Location: Munich, Germany
Device: iPad
|
Quote:
I just gave it a quick try on my fruitmachine and got Code:
sed: 1: "${s/.* //;p}": extra characters at the end of p command sed: 1: "${s/.* //;p}": extra characters at the end of p command mmsizr_new.sh: line 6: * : syntax error: operand expected (error token is "* ") |
|
06-21-2013, 06:11 AM | #14 |
Fanatic
Posts: 580
Karma: 810184
Join Date: Sep 2010
Location: Norway
Device: prs-t1, tablet, Nook Simple, assorted kindles, iPad
|
Ah, the delights of non-standard standards...
Apparently, OSX sed, unlike GNU sed, does not interpret ';' as a newline. To get it to work, either Code:
sed -n -e '${' -e 's/.* //' -e p -e '}' Code:
sed -n '${ s/.* // p }' win out in terms of elegance. Oh well, I've demonstrated there's more than one way to do it, at any rate. And you can do some mighty cool stuff with multi-line sed... |
06-28-2013, 01:35 PM | #15 | |
eBook Designer
Posts: 26
Karma: 10
Join Date: Jun 2010
Location: New Jersey
Device: ipad, nook, kindle
|
Photoshop Action
Quote:
|
|
Tags |
2 million pixels, apple, apple ibookstore, epub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Looking for freeware to batch resize images. | GoldThreader | Workshop | 10 | 04-23-2012 01:58 PM |
resize images ? | cybmole | Sigil | 5 | 04-03-2011 09:27 AM |
Linux script for images to Kindle Screensaver | soymicmic | Kindle Developer's Corner | 4 | 01-28-2011 12:20 PM |
Images Folder - Resize files | Fossil | Sigil | 9 | 07-24-2010 12:36 PM |