imaging

repository·master·Indexed 26 days ago

https://github.com/disintegration/imaging

A lightweight Go package for basic image processing tasks. It provides functionality for resizing (using various ResampleFilters like Lanczos and CatmullRom), rotating, cropping, and color adjustments including gamma, contrast, brightness, saturation, and hue. The library also supports Gaussian blur, sharpening, grayscale conversion, color inversion, 3x3 and 5x5 convolution kernels, and EXIF orientation correction during image decoding.

Tokens
4.7K
Snippets
16
Records
46
Agent score
90%

What's inside imaging

  1. Fix incorrect image orientation using AutoOrientation

    master

    If images appear rotated after processing, they likely contain EXIF orientation tags. Use the imaging.AutoOrientation(true) option when calling imaging.Open to automatically correct the orientation during decoding.

    img, err := imaging.Open("test.jpg", imaging.AutoOrientation(true))
  2. Complete image processing workflow example

    master

    This example demonstrates opening an image, cropping, resizing, applying effects (blur, grayscale, contrast, sharpening, inversion, convolution), creating a new canvas, pasting images, and saving the result.

    package main
    
    import (
    	"image"
    	"image/color"
    	"log"
    
    	"github.com/disintegration/imaging"
    )
    
    func main() {
    	// Open a test image.
    	src, err := imaging.Open("testdata/flowers.png")
    	if err != nil {
    		log.Fatalf("failed to open image: %v", err)
    	}
    
    	// Crop the original image to 300x300px size using the center anchor.
    	src = imaging.CropAnchor(src, 300, 300, imaging.Center)
    
    	// Resize the cropped image to width = 200px preserving the aspect ratio.
    	src = imaging.Resize(src, 200, 0, imaging.Lanczos)
    
    	// Create a blurred version of the image.
    	img1 := imaging.Blur(src, 5)
    
    	// Create a grayscale version of the image with higher contrast and sharpness.
    	img2 := imaging.Grayscale(src)
    	img2 = imaging.AdjustContrast(img2, 20)
    	img2 = imaging.Sharpen(img2, 2)
    
    	// Create an inverted version of the image.
    	img3 := imaging.Invert(src)
    
    	// Create an embossed version of the image using a convolution filter.
    	img4 := imaging.Convolve3x3(
    		src,
    		[9]float64{
    			-1, -1, 0,
    			-1, 1, 1,
    			0, 1, 1,
    		},
    		nil,
    	)
    
    	// Create a new image and paste the four produced images into it.
    	dst := imaging.New(400, 400, color.NRGBA{0, 0, 0, 0})
    	dst = imaging.Paste(dst, img1, image.Pt(0, 0))
    	dst = imaging.Paste(dst, img2, image.Pt(0, 200))
    	dst = imaging.Paste(dst, img3, image.Pt(200, 0))
    	dst = imaging.Paste(dst, img4, image.Pt(200, 200))
    
    	// Save the resulting image as JPEG.
    	err = imaging.Save(dst, "testdata/out_example.jpg")
    	if err != nil {
    		log.Fatalf("failed to save image: %v", err)
    	}
    }
  3. Adjust Gamma, Contrast, Brightness, Saturation, and Hue

    master

    The package provides several adjustment functions:

    • imaging.AdjustGamma(img, gamma): Adjusts gamma correction.
    • imaging.AdjustContrast(img, contrast): Adjusts contrast.
    • imaging.AdjustBrightness(img, brightness): Adjusts brightness.
    • imaging.AdjustSaturation(img, saturation): Adjusts saturation.
    • imaging.AdjustHue(img, hue): Adjusts hue.
    dstImage := imaging.AdjustGamma(srcImage, 0.75)
    dstImage := imaging.AdjustContrast(srcImage, 20)
    dstImage := imaging.AdjustBrightness(srcImage, 20)
    dstImage := imaging.AdjustSaturation(srcImage, 20)
    dstImage := imaging.AdjustHue(srcImage, 20)
  4. Resize images with various filters

    master

    Use imaging.Resize, imaging.Fit, or imaging.Fill to change image dimensions. These functions accept a ResampleFilter to control quality and speed.

    Resampling Filters:

    • imaging.Lanczos: High-quality, sharp results for photographic images.
    • imaging.CatmullRom: Sharp cubic filter, faster than Lanczos.
    • imaging.MitchellNetravali: Smooth cubic filter with less ringing than CatmullRom.
    • imaging.Linear: Fast bilinear resampling.
    • imaging.Box: Fast averaging filter, good for downscaling.
    • imaging.NearestNeighbor: Fastest, no antialiasing.

    Other supported filters include Hermite, BSpline, Gaussian, Hann, Hamming, Blackman, Bartlett, Welch, and Cosine.

    // Resize srcImage to size = 128x128px using the Lanczos filter.
    dstImage128 := imaging.Resize(srcImage, 128, 128, imaging.Lanczos)
    
    // Resize srcImage to width = 800px preserving the aspect ratio.
    dstImage800 := imaging.Resize(srcImage, 800, 0, imaging.Lanczos)
    
    // Scale down srcImage to fit the 800x600px bounding box.
    dstImageFit := imaging.Fit(srcImage, 800, 600, imaging.Lanczos)
    
    // Resize and crop the srcImage to fill the 100x100px area.
    dstImageFill := imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos)
  5. Resize an image to specific dimensions

    master

    Use imaging.Resize to change an image's width and height. If either width or height is set to 0, the function will preserve the original aspect ratio. If both are 0, it returns an empty *image.NRGBA.

    dstImage := imaging.Resize(srcImage, 800, 600, imaging.Lanczos)