Batch process images using OS X SIPS (scriptable image processing system) to downsize images only? - command-line

I'd like to batch process several folders of 1000's of images to downsize any images with a long side greater than 1440 pixels down to 1440 while ignoring any files that are already smaller than that.
I was looking at sips and can't tell if it skips upsizing by default or if there is a way to filter it using getProperty perhaps? (I'm not the best at deciphering CLI options from man pages).
I was thinking maybe I could use a find or sips query first and then pipe it into another sips to resize, I'm not sure exactly how though and don't think find can search by image size.
(also open to something other than sips to handle this, just seemed the quickest way)

Using spotlight to filter results to to images larger than a particular size works perfectly:
mdfind -0 -onlyin . "kMDItemPixelHeight > 1440 || kMDItemPixelWidth > 1440" | xargs -0 sips -Z 1440
This find images recursively from the current directory with width OR height greater than 1440 pixels and resizes them down to 1440. Files under 1440 get left alone.

Related

Speed up reducing size of large JPEG image using ImageMagick

I have a very large JPEG image (10800x7497) that I want to resize down to 50% of its resolution. I have already reduced the image colors down to 64 colors using -define jpeg:colors=64, but when I try resizing the image, ImageMagick takes very long to process the image, probably 20 minutes or more (since I have stopped the process when no output image is saved within 20 minutes, although Task Manager shows ImageMagick processing the image). How can I speed up the resizing of this large image? I have tried the following codes but still it takes too long:
magick -define jpeg:size=10800x7500 "image1.jpg" -resize 5400x3750 "image1-resized.jpg"
magick -define jpeg:size=5400x3750 "image1.jpg" -resize 5400x3750 "image1-resized.jpg"
magick -depth 5 "image1.jpg" -resize 50% "image1-resized.jpg"
It sounds like your imagemagick is swapping to disc. You probably need to adjust your policy.xml.
This is the file containing the limits for the amount of memory and disc magick is allowed to use. The magick docs have some notes, but check /etc/ImageMagick-7/policy.xml and have a look for lines like eg.:
<policy domain="resource" name="memory" value="256MiB"/>
256mb memory use is far too small -- change it something like:
<policy domain="resource" name="memory" value="8GiB"/>
You'll see quite a few other similar lines, adjust them to fit your hardware.
I would simply use resize. The -define hint will turn on jpeg shrink-on-load, which will lose quality and cause (probably) noticeable moire fringing. Plus for a 50% shrink there's no speed benefit.
$ identify big.jpg
big.jpg JPEG 10800x7497 10800x7497+0+0 8-bit sRGB 13.4733MiB 0.000u 0:00.000
$ /usr/bin/time -f %M:%e convert big.jpg -resize 5400x3750 x.jpg
1130340:0.92
So 0.92s and 1.1gb of memory with imagemagick6. imagemagick7 is usually about half the speed and twice the memory use, so I'd expect about 2s and 2GB.
As Mark says, vipsthumbnail is likely to be quicker. I see:
$ /usr/bin/time -f %M:%e vipsthumbnail big.jpg -s 5400 -o x.jpg
295460:0.69
So 300mb of memory and 0.7s. This PC has a stupid number of cores (32!) and you really can't get that much parallelism out of basic JPEG shrinking, so you see a useful speedup and lower memory use if you turn the number of threads down:
$ /usr/bin/time -f %M:%e vipsthumbnail big.jpg -s 5400 -o x.jpg --vips-concurrency=3
77744:0.43
78mb of memory and 0.43s.

JPEG to PNG conversion with 300 DPI

Unable to convert a JPEG image into a 300 DPI PNG image using ImageMagick.
After conversion the PNG image is 72 DPI only. I'm using ImageMagick 6.9.0-0 Q16 x86 and Ghostscript v9.15.
Below is the line I use in my Perl script:
system("\"$imagemagick\" -set units PixelsPerInch -density 300 \"$jpg\" \"$png\"");
Adjusting the units & density will not alter the underlining image data, but updates meta info for rendering libraries. Important for vector to raster, but not very useful for raster to raster. To adjust the DPI of an image, use the -resample operation.
convert source.jpg -resample 300 out.png
You verify the DPI resolution with the following...
identify -format "%[resolution.x] %[resolution.y]\n" out.png
I'm wondering where the 72dpi is coming from. Assuming you are using X and some kind of Unix, ImageMagick defaults to using the screen resolution (72 dpi). I'm not sure what it does under OSX/XQuartz but it's likely similar. Is your screen resolution set to 72dpi (!?).
I'm with #emcconville #ikegami - just do this straight from ImageMagick on the commandline - passing the right options to be sure.
There are image manipulation modules that you can use from perl without having to resort to system commands as well such as Imager::Transformations, Image::Magick, and GD. Here's how to convert with GD.
perl -MGD -E 'my $imgjpg = GD::Image->newFromJpeg("img.jpg");
open my $imgpng, ">", "img.png" or die; print $imgpng $imgjpg->png();'
With most image manipulation packages the original resolution show be maintained during conversion - though some (including GD) will default to lower color depths (8 bit) unless passed a Truecolor flag.
e.g. GD::Image->newFromJpeg("img.jpg", 1);

How to compress the image using gifsicle tool or any other tool on ubuntu

I have some images in .GIF format which i want to compress. But i am getting the output either same or hardly 2-5% compression.I need higher compression ratio so that the web pages can be loaded fastly. Currently i am using the gifsicle tool but hardly i am finding much difference in size of the generated gif images.
I opted this tool from yahoo smush it.
gifsicle -O3 gifimage1.gif -o new-gifimage1.gif
UPDATE:
The giflossy fork has been merged into gifsicle now, so you will be able to use the --lossy flag with gifsicle now (with the latest version), no need to install giflossy separately.
If you want to enable lossy compression which reduces the size considerably, you can use giflossy fork of gifsicle. Once you have installed it you can use the lossey option as below
gifsicle -O3 --lossy=80 gifimage1.gif -o new-gifimage1.gif
Installation:
On Mac : brew install giflossy
On Unix : Building Gifsicle on UNIX
You can use below command to tweak your compression options
gifsicle -O3 --colors=64 --use-col=web --lossy=100 --scale 0.8 gifimage1.gif -o new-gifimage1.gif
Gifsicle's --optimize option will only attempt lossless reduction of an image's file size. What you probably have* is an animated gif where each frame contains random dithering, so most of the pixels will change from one frame to the next.
If your original GIF image had used pattern dithering, you would be able to compress it a lot more. But if that's not an option, I suggest you try either reducing the dimensions of the image (e.g., --scale 0.5), or reducing the number of colours in it (e.g., --colors 16).
* (I'm only guessing, since you didn't bother to share your image)

Libreoffice Draw Export Resolution makes no sense

I am attempting to make a very simple label using Libreoffice Draw v 4.0.2.2. The label has not much more to it than regularly spaced lines of centered text
This image will be printed, and I have a fixed size/ppi requirement to ensure appropriate print quality.
I set the page size to my specs, and layout the text as I desire. The print shop takes several image formats including .tiff and .png. When I export the image, a dialog pops up that asks for the image size/ resolution. The given ppi is very low (~40) and I require a minimum of 180ppi. When I enter this, the image size adjusts itself and results in an image that is far too small.
The only solution that appears to be viable is to explode the page size and the drawing text size so it gets shrunk upon export. This is a very imprecise and illogical feature (bug?) of the program that I really wish is a result of my ignorance.
I found a thread in the mailing list which describes this issue exactly. The only answer that is given is essentially "yes, this is ridiculous and doesn't help anybody".
Can anyone give some advice to this? Or at least shed some light on who might need this "feature"?
There is something off about the Export tool of LibreOffice in general. It has been years since it is broken. Taking a screenshot is an alternative, but obviously you cannot control the resolution.
So, a better work around is exporting to SVG, and then convert the SVG to PNG with Inkscape. Once downloaded, convert the file with the following command:
inkscape -z -e out.png -w 1024 in.svg
If you are in Windows (x64), you will need to indicate the full path:
"C:/Program Files/Inkscape/inkscape.exe" -z -e out.png -w 1024 in.svg
If you install the 32 bit version, this should work:
"C:\Program Files (x86)/Inkscape/inkscape.exe" -z -e out.png -w 1024 in.svg
This can be done from inside Libre Office, there is no need to use any external tool. The Export dialog is very confusing, yes; you have to realize that both size and resolution can be set independently.
Select File -> Export -> choose the desired format. The export dialog should appear.
TAKE NOTE of Width and Height. Set the desired resolution; notice how Width and Height change (?). Don't worry, restore Width and Height to your saved values. And that's it. You get a high resolution image with the desired size and DPI.
Libre Draw (the one I'm using anyway) is a vector drawing app - have you asked the print shop if they can use vector formats like eps, pdf? Most should be able to in my experience. Then resolution becomes irrelevant.
-Terry

How can I convert TIFF files of various dimensions to uniformly sized PDF files with page size 8.5″×11″ without losing quality?

Currently I'm trying to use Perl/ImageMagick and/or Ghostscript to convert scanned text documents stored as TIFFs into an 8.5″×11″ (ANSI A “Letter” size) PDF file.
I've tried many of the ImageMagick filters with resize and still find that some files perfectly legible before are now illegible. Often these images are at 72 dpi and when converted to be 8.5″×11″, it ends up with something like 612×792 pixels. The original was 1700×2200; as you can see there are quite a bit of pixels lost in the re-size.
Should I be using something else besides resize? Could it be something like ImageMagick is reporting the image is 72 dpi when it's really something like 200 dpi? Would re-sampling the image into the highest dpi that would fit in the 8.5″×11″ area help?
Does anyone have any other options to ultimately create a PDF file with all pages being 8.5″×11″?
(Mantra: 'Use the right tool for the job...')
You possibly shouldn't use ImageMagick for the job, but rather LibTIFF's tiff2pdf commandline utility:
tiff2pdf \
-z \
-o output.pdf \
-p letter \
-F \
input.tiff
-z is for (lossless) Zip/Flate compression.
-o defines the output filename.
-p sets the media size.
-F fills the page.