View Full Version : Script to resize images to 2 Million Pixels


shorshe
08-13-2012, 06:42 PM
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:

The maximum recommended size is 2 million pixels per image. Larger images may not appear as expected in the iBooks app.


I also used platypus to convert it to an OSX-App.

Download App here:

http://mmsizr.googlecode.com/files/MMSizr.app.zip


Usage:

Unzip epub.
Start App and drop your images Folder on main window.
App searches for jpg, png, gif images an resizes to 2 Million pixels if necessary.

Image names must not contain whitespaces.

Original shell-Script:
#!/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


yes it's rough. Please feel free to improve:
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-scriptapp-to-resize-images-to-2-million-pixels/

AlPe
08-15-2012, 12:41 PM
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).

shorshe
08-15-2012, 02:34 PM
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?

frostschutz
08-15-2012, 02:53 PM
surely there's already an osx port for imagemagick?

AlPe
08-16-2012, 04:49 AM
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/7412208/imagemagick-and-os-x-lion-trouble

shorshe
08-16-2012, 11:00 AM
surely there's already an osx port for imagemagick?

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-server/viewtopic.php?t=13145
--------------

Apart from that I don't even know if the license of every dependency would allow that.

AlPe
08-20-2012, 07:17 AM
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.

Apart from that I don't even know if the license of every dependency would allow that.

I see. I am not familiar with OS X, so I cannot ponder the question throughfully. If "sips" is a standard utility of OS X (as it seems to me), then your script is a very adequate solution.

It is a shame it lacks many powerful options already included in imagemagick, though.

sueneu
06-07-2013, 11:36 PM
Wow. This script just made my night. Thanks.

shorshe
06-14-2013, 10:42 AM
nice
:thumbsup:

Lancelot
06-14-2013, 01:16 PM
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.

SBT
06-14-2013, 03:01 PM
Not sure if you consider this an improvement oh well...
#!/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")


($anzahl should be replaced by $pxlnumber, nicht so?)
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.

shorshe
06-21-2013, 03:56 AM
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!

Depends on how much time i'm able to invest and depending on the content. I usually export from ID in a very high dpi setting. I'm glad the images are already cropped, like in the original work.

But you are right. Quality can always be improved. As long as you get paid for it...

shorshe
06-21-2013, 04:10 AM
Not sure if you consider this an improvement – oh well...
Don't have a fruit machine, so I haven't been able to test it properly.

Thanks for your Update!
I just gave it a quick try on my fruitmachine and got


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 "* ")

SBT
06-21-2013, 06:11 AM
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
sed -n -e '${' -e 's/.* //' -e p -e '}'
or
sed -n '${
s/.* //
p
}'
may work. However, your original solution for these lines now definitely
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...

sueneu
06-28-2013, 01:35 PM
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.

Lancelot, Can you see the total number of pixels in Bridge's metadata view?