imaging
| Language | Go |
|---|---|
| License | MIT |
| Repository | github.com/disintegration/imaging |
| Documentation | pkg.go.dev/github.com/disintegration/imaging |
| Depends on | golang.org/x/image (TIFF, BMP codecs) |
| Read / write | yes / yes, through x/image/tiff: first page only, Deflate-compressed output |
imaging is a pure Go package of image processing functions: resize, fit, crop, rotate, flip, blur, sharpen, adjust brightness, contrast and gamma. imaging.Open and imaging.Save pick the format from the file contents and extension, so TIFF works like JPEG or PNG. It uses x/image/tiff underneath, with the same limits: only the first page is read, no BigTIFF, no JPEG-in-TIFF, no tags.
The last release is v1.6.2 (2019). It’s still widely used and stable.
Installation#
Shell
go get github.com/disintegration/imaging
Basic usage: open a TIFF, resize, save as JPEG#
Go
package main
import (
"log"
"github.com/disintegration/imaging"
)
func main() {
img, err := imaging.Open("in.tif")
if err != nil {
log.Fatal(err)
}
small := imaging.Resize(img, 1200, 0, imaging.Lanczos) // 0 keeps the aspect ratio
if err := imaging.Save(small, "out.jpg", imaging.JPEGQuality(90)); err != nil {
log.Fatal(err)
}
}
How to convert a PNG or JPEG to TIFF#
The .tif or .tiff extension selects TIFF. imaging writes Deflate-compressed TIFF:
Go
img, err := imaging.Open("in.png")
if err != nil {
log.Fatal(err)
}
if err := imaging.Save(img, "out.tif"); err != nil {
log.Fatal(err)
}
How to make a thumbnail of a TIFF#
Go
thumb := imaging.Thumbnail(img, 256, 256, imaging.Lanczos) // crops to fill 256x256
err := imaging.Save(thumb, "thumb.png")
How to write TIFF to an io.Writer#
Go
var buf bytes.Buffer
if err := imaging.Encode(&buf, img, imaging.TIFF); err != nil {
log.Fatal(err)
}
Tips#
imaging.Open(path, imaging.AutoOrientation(true))applies the Exif orientation of JPEG files; TIFF orientation tags are not applied.- All processing functions return
*image.NRGBA: 8 bits per channel. 16-bit TIFF data is reduced to 8 bits.