Skip to content

Instantly share code, notes, and snippets.

@jar-o
Last active April 10, 2018 15:40
Show Gist options
  • Save jar-o/39142dcdf26df430512470f0dd03d36b to your computer and use it in GitHub Desktop.
Save jar-o/39142dcdf26df430512470f0dd03d36b to your computer and use it in GitHub Desktop.
Simple go app that uses ImageMagick to compress multipage TIFFs without losing the pages.
/*
# This werks in OSX ... need to build the C deps locally, then run your go app
# with the appropriate CGO* flags.
http://www.imagemagick.org/download/releases/ImageMagick-7.0.6-10.tar.xz
tar xvzf ImageMagick-7.0.6-10.tar.xz
cd ImageMagick-7.0.6-10
mkdir dist
export CGO_IMAGEMAGICK_PREFIX=`pwd`/dist
# The .../Cellar reference is to (maybe) pick up any libs already installed by brew that it needs *shrug*
CFLAGS=-I/usr/local/Cellar ./configure --prefix=$CGO_IMAGEMAGICK_PREFIX --with-quantum-depth=16 --disable-dependency-tracking --without-perl
make && make install
CGO_CFLAGS="-I $CGO_IMAGEMAGICK_PREFIX/include/ImageMagick-7/" CGO_LDFLAGS="-L $CGO_IMAGEMAGICK_PREFIX/lib/ -lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI" go run compress-multipage-tiff.go
*/
package main
import (
"gopkg.in/gographics/imagick.v3/imagick"
"log"
)
func tiffCompress(infile string, outfile string) {
imagick.Initialize()
defer imagick.Terminate()
mw := imagick.NewMagickWand()
aw := imagick.NewMagickWand()
defer mw.Destroy()
defer aw.Destroy()
err := mw.ReadImage(infile)
if err != nil {
log.Fatal(err)
return
}
for i := 0; i < int(mw.GetNumberImages()); i++ {
log.Printf("i=%d\n", i)
mw.SetIteratorIndex(i)
tw := mw.GetImage()
aw.AddImage(tw)
tw.Destroy()
}
aw.SetImageFormat("TIFF")
aw.SetImageCompression(imagick.COMPRESSION_LZW)
aw.WriteImages(outfile, true)
}
func main() {
tiffCompress("input1.tif", "compressed1.tif")
tiffCompress("input2.tif", "compressed2.tif")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment