Top Related Projects
Image processing in Python
Open Source Computer Vision Library
Python Imaging Library (Fork)
Datasets, Transforms and Models specific to Computer Vision
TensorFlow Graphics: Differentiable Graphics Layers for TensorFlow
Geometric Computer Vision Library for Spatial AI
Quick Overview
The disintegration/imaging
repository is a Go library that provides a set of functions and utilities for image processing and manipulation. It supports various image formats, including JPEG, PNG, GIF, and TIFF, and offers a wide range of image operations such as resizing, cropping, rotating, and applying filters.
Pros
- Extensive Functionality: The library provides a comprehensive set of image processing functions, allowing developers to perform a wide range of operations on images.
- Performance: The library is written in Go, which is known for its efficiency and performance, making it suitable for handling large or high-resolution images.
- Cross-Platform Compatibility: The library is designed to be cross-platform, allowing developers to use it on various operating systems, including Windows, macOS, and Linux.
- Active Development: The project is actively maintained, with regular updates and bug fixes, ensuring the library remains up-to-date and reliable.
Cons
- Limited Documentation: The project's documentation could be more comprehensive, which may make it challenging for new users to get started with the library.
- Lack of Advanced Features: While the library offers a wide range of basic image processing functions, it may lack some more advanced features or specialized algorithms that some users might require.
- Dependency on External Libraries: The library relies on several external dependencies, which could potentially introduce compatibility issues or increase the complexity of the project setup.
- Learning Curve: As with any new library or framework, there may be a learning curve for developers who are not familiar with Go or image processing concepts.
Code Examples
Here are a few examples of how to use the disintegration/imaging
library:
- Resizing an Image:
package main
import (
"fmt"
"log"
"os"
"github.com/disintegration/imaging"
)
func main() {
// Open the image file
src, err := imaging.Open("input.jpg")
if err != nil {
log.Fatalf("Failed to open image: %v", err)
}
// Resize the image to 200x200 pixels
dst := imaging.Resize(src, 200, 200, imaging.Lanczos)
// Save the resized image to a file
err = imaging.Save(dst, "output.jpg")
if err != nil {
log.Fatalf("Failed to save image: %v", err)
}
fmt.Println("Image resized and saved successfully.")
}
- Applying a Grayscale Filter:
package main
import (
"fmt"
"log"
"os"
"github.com/disintegration/imaging"
)
func main() {
// Open the image file
src, err := imaging.Open("input.jpg")
if err != nil {
log.Fatalf("Failed to open image: %v", err)
}
// Apply a grayscale filter
dst := imaging.Grayscale(src)
// Save the filtered image to a file
err = imaging.Save(dst, "output.jpg")
if err != nil {
log.Fatalf("Failed to save image: %v", err)
}
fmt.Println("Image filtered and saved successfully.")
}
- Cropping an Image:
package main
import (
"fmt"
"log"
"os"
"github.com/disintegration/imaging"
)
func main() {
// Open the image file
src, err := imaging.Open("input.jpg")
if err != nil {
log.Fatalf("Failed to open image: %v", err)
}
// Crop the image to a 200x200 rectangle starting from (100, 100)
dst := imaging.Crop(src, image.Rect(100, 100, 300, 300))
// Save the cropped image to a file
err = imaging.Save(dst, "output.jpg")
if err != nil {
log.Fatalf("Failed to save image: %v", err)
}
fmt.Println
Competitor Comparisons
Image processing in Python
Pros of scikit-image
- Extensive collection of image processing algorithms and functions
- Well-documented with comprehensive tutorials and examples
- Strong integration with the scientific Python ecosystem (NumPy, SciPy, etc.)
Cons of scikit-image
- Steeper learning curve for beginners due to its extensive feature set
- Slower performance for some operations compared to lower-level libraries
- Larger package size and more dependencies
Code Comparison
scikit-image:
from skimage import io, filters
image = io.imread('image.jpg')
edges = filters.sobel(image)
imaging:
import "github.com/disintegration/imaging"
img, _ := imaging.Open("image.jpg")
edges := imaging.Convolve3x3(img, imaging.SobelHorizontal)
Summary
scikit-image offers a comprehensive set of image processing tools with excellent documentation and integration with the scientific Python ecosystem. However, it may have a steeper learning curve and slower performance for some operations compared to imaging. The imaging library, written in Go, provides a simpler API and potentially faster execution for basic image processing tasks, but with a more limited set of features compared to scikit-image.
Open Source Computer Vision Library
Pros of OpenCV
- Extensive library with a wide range of computer vision algorithms and functions
- Supports multiple programming languages (C++, Python, Java)
- Large community and extensive documentation
Cons of OpenCV
- Larger footprint and more complex installation process
- Steeper learning curve for beginners
- May be overkill for simple image processing tasks
Code Comparison
OpenCV (Python):
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
cv2.imwrite('edges.jpg', edges)
Imaging (Go):
package main
import "github.com/disintegration/imaging"
func main() {
img, _ := imaging.Open("image.jpg")
gray := imaging.Grayscale(img)
imaging.Save(gray, "gray.jpg")
}
The OpenCV example demonstrates edge detection, while the Imaging example shows basic grayscale conversion. OpenCV offers more advanced functionality, but Imaging provides a simpler API for basic image processing tasks in Go.
Python Imaging Library (Fork)
Pros of Pillow
- Extensive Python ecosystem support and integration
- Wider range of image processing features and filters
- Larger community and more frequent updates
Cons of Pillow
- Slower performance for certain operations
- Limited support for newer image formats
Code Comparison
Pillow
from PIL import Image
img = Image.open("image.jpg")
img = img.rotate(45)
img = img.resize((300, 300))
img.save("output.png")
Imaging
package main
import "github.com/disintegration/imaging"
img, _ := imaging.Open("image.jpg")
img = imaging.Rotate(img, 45, nil)
img = imaging.Resize(img, 300, 300, imaging.Lanczos)
imaging.Save(img, "output.png")
Key Differences
- Pillow is a Python library, while Imaging is written in Go
- Imaging focuses on performance and simplicity
- Pillow offers more comprehensive image manipulation capabilities
- Imaging provides a more streamlined API for basic operations
Use Cases
- Choose Pillow for Python projects requiring extensive image processing
- Opt for Imaging in Go applications or when performance is critical
Datasets, Transforms and Models specific to Computer Vision
Pros of vision
- Part of the PyTorch ecosystem, offering seamless integration with deep learning workflows
- Extensive collection of pre-trained models and datasets for computer vision tasks
- Active development and support from a large community and Facebook AI Research
Cons of vision
- Heavier dependency footprint due to PyTorch requirement
- Steeper learning curve for users not familiar with PyTorch or deep learning concepts
- May be overkill for simple image processing tasks
Code Comparison
vision:
import torchvision.transforms as T
transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])
img_tensor = transform(image)
imaging:
m := imaging.Resize(img, 256, 0, imaging.Lanczos)
m = imaging.CenterCrop(m, 224, 224)
Summary
vision is a comprehensive computer vision library built on PyTorch, offering advanced features for deep learning tasks. imaging is a lightweight Go library focused on basic image processing operations. Choose vision for complex ML-based vision tasks, and imaging for simple, fast image manipulations in Go projects.
TensorFlow Graphics: Differentiable Graphics Layers for TensorFlow
Pros of TensorFlow Graphics
- Offers advanced 3D graphics capabilities and computer vision functionalities
- Integrates seamlessly with TensorFlow's machine learning ecosystem
- Provides GPU acceleration for improved performance
Cons of TensorFlow Graphics
- Steeper learning curve due to its complexity and integration with TensorFlow
- Larger library size and more dependencies
- May be overkill for simple image processing tasks
Code Comparison
Imaging:
img, err := imaging.Open("input.jpg")
resized := imaging.Resize(img, 800, 0, imaging.Lanczos)
err = imaging.Save(resized, "output.jpg")
TensorFlow Graphics:
import tensorflow_graphics as tfg
import tensorflow as tf
image = tf.io.read_file("input.jpg")
image = tf.image.decode_jpeg(image)
resized = tf.image.resize(image, [800, 0], method=tf.image.ResizeMethod.LANCZOS3)
tf.io.write_file("output.jpg", tf.image.encode_jpeg(resized))
Summary
Imaging is a lightweight Go library for basic image processing, while TensorFlow Graphics is a comprehensive Python library for advanced 3D graphics and computer vision tasks. Imaging is simpler to use and more suitable for basic image manipulations, whereas TensorFlow Graphics offers more advanced features but with increased complexity.
Geometric Computer Vision Library for Spatial AI
Pros of kornia
- Built on PyTorch, enabling GPU acceleration and deep learning integration
- Offers a wider range of computer vision operations, including differentiable modules
- Actively maintained with frequent updates and a larger community
Cons of kornia
- Steeper learning curve due to PyTorch dependency
- Potentially slower for simple image processing tasks not requiring GPU acceleration
- Larger library size and more dependencies
Code Comparison
kornia:
import torch
import kornia
input = torch.rand(1, 1, 5, 5)
kernel = torch.ones(3, 3)
output = kornia.filters.filter2d(input, kernel)
imaging:
package main
import "github.com/disintegration/imaging"
func main() {
src, _ := imaging.Open("input.jpg")
dst := imaging.Blur(src, 3.5)
imaging.Save(dst, "output.jpg")
}
Summary
kornia is a more comprehensive computer vision library built on PyTorch, offering GPU acceleration and deep learning capabilities. imaging is a simpler, pure Go library focused on basic image processing tasks. Choose kornia for complex computer vision projects, especially those involving deep learning, while imaging is better suited for lightweight image manipulation in Go applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Imaging
Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.).
All the image processing functions provided by the package accept any image type that implements image.Image
interface
as an input, and return a new image of *image.NRGBA
type (32bit RGBA colors, non-premultiplied alpha).
Installation
go get -u github.com/disintegration/imaging
Documentation
https://pkg.go.dev/github.com/disintegration/imaging
Usage examples
A few usage examples can be found below. See the documentation for the full list of supported functions.
Image resizing
// 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)
Imaging supports image resizing using various resampling filters. The most notable ones:
Lanczos
- A high-quality resampling filter for photographic images yielding sharp results.CatmullRom
- A sharp cubic filter that is faster than Lanczos filter while providing similar results.MitchellNetravali
- A cubic filter that produces smoother results with less ringing artifacts than CatmullRom.Linear
- Bilinear resampling filter, produces smooth output. Faster than cubic filters.Box
- Simple and fast averaging filter appropriate for downscaling. When upscaling it's similar to NearestNeighbor.NearestNeighbor
- Fastest resampling filter, no antialiasing.
The full list of supported filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine. Custom filters can be created using ResampleFilter struct.
Resampling filters comparison
Original image:
The same image resized from 600x400px to 150x100px using different resampling filters. From faster (lower quality) to slower (higher quality):
Filter | Resize result |
---|---|
imaging.NearestNeighbor | |
imaging.Linear | |
imaging.CatmullRom | |
imaging.Lanczos |
Gaussian Blur
dstImage := imaging.Blur(srcImage, 0.5)
Sigma parameter allows to control the strength of the blurring effect.
Original image | Sigma = 0.5 | Sigma = 1.5 |
---|---|---|
Sharpening
dstImage := imaging.Sharpen(srcImage, 0.5)
Sharpen
uses gaussian function internally. Sigma parameter allows to control the strength of the sharpening effect.
Original image | Sigma = 0.5 | Sigma = 1.5 |
---|---|---|
Gamma correction
dstImage := imaging.AdjustGamma(srcImage, 0.75)
Original image | Gamma = 0.75 | Gamma = 1.25 |
---|---|---|
Contrast adjustment
dstImage := imaging.AdjustContrast(srcImage, 20)
Original image | Contrast = 15 | Contrast = -15 |
---|---|---|
Brightness adjustment
dstImage := imaging.AdjustBrightness(srcImage, 20)
Original image | Brightness = 10 | Brightness = -10 |
---|---|---|
Saturation adjustment
dstImage := imaging.AdjustSaturation(srcImage, 20)
Original image | Saturation = 30 | Saturation = -30 |
---|---|---|
Hue adjustment
dstImage := imaging.AdjustHue(srcImage, 20)
Original image | Hue = 60 | Hue = -60 |
---|---|---|
FAQ
Incorrect image orientation after processing (e.g. an image appears rotated after resizing)
Most probably, the given image contains the EXIF orientation tag.
The standard image/*
packages do not support loading and saving
this kind of information. To fix the issue, try opening images with
the AutoOrientation
decode option. If this option is set to true
,
the image orientation is changed after decoding, according to the
orientation tag (if present). Here's the example:
img, err := imaging.Open("test.jpg", imaging.AutoOrientation(true))
What's the difference between imaging
and gift
packages?
imaging
is designed to be a lightweight and simple image manipulation package.
It provides basic image processing functions and a few helper functions
such as Open
and Save
. It consistently returns *image.NRGBA image
type (8 bits per channel, RGBA).
gift supports more advanced image processing, for example, sRGB/Linear color space conversions. It also supports different output image types (e.g. 16 bits per channel) and provides easy-to-use API for chaining multiple processing steps together.
Example code
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)
}
}
Output:
Top Related Projects
Image processing in Python
Open Source Computer Vision Library
Python Imaging Library (Fork)
Datasets, Transforms and Models specific to Computer Vision
TensorFlow Graphics: Differentiable Graphics Layers for TensorFlow
Geometric Computer Vision Library for Spatial AI
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot