Top Related Projects
Imaging is a simple image processing package for Go
Image processing algorithms in pure Go
Go Graphics - 2D rendering in Go with a simple API.
smartcrop finds good image crops for arbitrary crop sizes
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
Content aware image resize library
Quick Overview
nfnt/resize is a Go library for image resizing. It provides various interpolation methods for resizing images, including NearestNeighbor, Bilinear, Bicubic, MitchellNetravali, and Lanczos2/3 algorithms. The library is designed to be simple to use while offering high-quality resizing options.
Pros
- Easy to use with a simple API
- Supports multiple interpolation methods for different quality/speed trade-offs
- Pure Go implementation with no external dependencies
- Handles both upscaling and downscaling of images
Cons
- Limited to image resizing operations only
- May be slower than some specialized C libraries for image processing
- Not actively maintained (last commit was in 2020)
- Lacks advanced features like batch processing or concurrent resizing
Code Examples
- Basic image resizing:
import (
"github.com/nfnt/resize"
"image/jpeg"
"os"
)
func main() {
file, _ := os.Open("input.jpg")
img, _ := jpeg.Decode(file)
file.Close()
resized := resize.Resize(800, 0, img, resize.Lanczos3)
out, _ := os.Create("output.jpg")
jpeg.Encode(out, resized, nil)
out.Close()
}
- Resizing with a specific interpolation method:
resized := resize.Resize(1024, 768, img, resize.Bilinear)
- Preserving aspect ratio:
// Resize to width 300px, height will be automatically calculated
resized := resize.Resize(300, 0, img, resize.Lanczos2)
// Resize to height 500px, width will be automatically calculated
resized := resize.Resize(0, 500, img, resize.Bicubic)
Getting Started
To use nfnt/resize in your Go project, follow these steps:
-
Install the library:
go get github.com/nfnt/resize
-
Import the library in your Go code:
import "github.com/nfnt/resize"
-
Use the
resize.Resize()
function to resize your images:resized := resize.Resize(width, height, inputImage, resize.Lanczos3)
Replace width
and height
with your desired dimensions, inputImage
with your image object, and choose an interpolation method (e.g., resize.Lanczos3
, resize.Bilinear
, etc.).
Competitor Comparisons
Imaging is a simple image processing package for Go
Pros of imaging
- Offers a wider range of image processing functions beyond resizing
- Supports more image formats, including GIF
- Provides additional features like drawing and color manipulation
Cons of imaging
- May have slightly slower performance for basic resizing operations
- Larger codebase and dependencies, potentially increasing complexity
- Less focused on a single task, which could be overkill for simple resizing needs
Code Comparison
resize:
m := resize.Resize(300, 0, img, resize.Lanczos3)
imaging:
m := imaging.Resize(img, 300, 0, imaging.Lanczos)
Both libraries offer similar syntax for basic resizing operations. The main difference lies in the broader functionality provided by imaging.
imaging allows for more complex operations in a single chain:
m := imaging.Resize(imaging.Crop(img, image.Rect(100, 100, 500, 500)), 300, 0, imaging.Lanczos)
This demonstrates imaging's ability to combine multiple operations, which is not directly possible with resize.
Overall, resize is more focused and lightweight, while imaging offers a comprehensive set of image processing tools at the cost of increased complexity.
Image processing algorithms in pure Go
Pros of bild
- Offers a wider range of image processing operations beyond resizing
- Provides more advanced color manipulation functions
- Includes additional features like drawing and blending operations
Cons of bild
- May have slightly slower performance for basic resizing operations
- Less focused on optimizing memory usage during resizing
- Potentially more complex API for simple resizing tasks
Code Comparison
resize:
m := resize.Resize(100, 0, img, resize.Lanczos3)
bild:
resized := transform.Resize(img, 100, 0, transform.Linear)
Both libraries offer simple one-line resizing functions, but bild provides more options for interpolation methods and additional transformation capabilities.
Summary
resize is a focused library specifically for image resizing, while bild is a more comprehensive image processing library. resize may be preferable for projects that primarily need efficient resizing, while bild offers a broader range of image manipulation tools at the cost of potentially increased complexity and slightly lower performance for basic resizing tasks.
Go Graphics - 2D rendering in Go with a simple API.
Pros of gg
- More comprehensive 2D graphics library with additional features beyond just image resizing
- Supports vector graphics and text rendering
- Active development with recent updates
Cons of gg
- Larger and more complex codebase, potentially harder to integrate for simple resizing tasks
- May have more dependencies and a larger footprint
Code Comparison
resize:
m := resize.Resize(width, height, img, resize.Lanczos3)
gg:
dc := gg.NewContext(width, height)
dc.DrawImage(img, 0, 0)
dc.Scale(scaleX, scaleY)
dc.DrawImage(img, 0, 0)
Key Differences
- resize focuses solely on image resizing with various interpolation methods
- gg is a more feature-rich 2D graphics library that includes resizing capabilities
- resize may be simpler to use for basic image resizing tasks
- gg offers more flexibility for complex graphics operations beyond resizing
Use Cases
- Choose resize for straightforward image resizing needs
- Opt for gg when requiring additional 2D graphics capabilities or vector support
Community and Maintenance
- resize has a longer history but less recent activity
- gg shows more recent commits and active maintenance
Performance
- resize may have better performance for simple resizing operations
- gg's performance can vary depending on the complexity of graphics operations
smartcrop finds good image crops for arbitrary crop sizes
Pros of smartcrop
- Intelligent content-aware cropping for better image composition
- Supports face detection for improved results with portraits
- Offers more advanced image processing capabilities beyond simple resizing
Cons of smartcrop
- More complex to use and integrate compared to resize
- Potentially slower processing time due to advanced algorithms
- May require additional dependencies for face detection features
Code Comparison
resize:
m := resize.Resize(300, 0, img, resize.Lanczos3)
smartcrop:
analyzer := smartcrop.NewAnalyzer()
topCrop, _ := analyzer.FindBestCrop(img, 300, 300)
croppedImg := imaging.Crop(img, topCrop)
Summary
resize is a straightforward library for image resizing, while smartcrop offers more advanced content-aware cropping. resize is simpler to use and likely faster, but smartcrop provides better results for maintaining important image content during cropping. The choice between the two depends on the specific requirements of your project, balancing simplicity and speed against intelligent cropping capabilities.
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
Pros of imaginary
- Offers a wide range of image processing operations beyond resizing
- Provides a HTTP microservice for easy integration and scalability
- Supports multiple storage backends (local, S3, Google Cloud)
Cons of imaginary
- More complex setup and configuration compared to resize
- Higher resource usage due to its comprehensive feature set
- Potential overkill for simple resizing tasks
Code comparison
resize:
m := resize.Resize(300, 0, img, resize.Lanczos3)
imaginary:
curl -O "http://localhost:8088/resize?width=300&url=http://example.com/image.jpg"
Key differences
resize is a pure Go library focused solely on image resizing, while imaginary is a full-featured image processing microservice. resize is simpler to use for basic resizing tasks within Go applications, whereas imaginary offers more flexibility and features but requires running as a separate service.
resize is ideal for developers who need a lightweight, in-process resizing solution. imaginary is better suited for complex image processing workflows or when a standalone service is preferred.
Both projects have their merits, and the choice between them depends on the specific requirements of your application and infrastructure.
Content aware image resize library
Pros of caire
- Offers content-aware image resizing using seam carving algorithm
- Provides face detection to preserve important features during resizing
- Supports both image resizing and content removal
Cons of caire
- More complex to use due to additional features and options
- Slower processing time compared to basic resizing operations
- Requires more dependencies and setup
Code Comparison
resize:
m := resize.Resize(300, 0, img, resize.Lanczos3)
caire:
p := &caire.Processor{
NewWidth: 300,
NewHeight: 0,
Faces: true,
SeamCarver: true,
}
err := p.Process(input, output)
Summary
resize is a simpler library focused on basic image resizing operations, while caire offers more advanced content-aware resizing features. resize is easier to use and faster for basic resizing tasks, but caire provides better results for complex resizing scenarios where preserving image content is crucial. The choice between the two depends on the specific requirements of your project and the level of control you need over the resizing process.
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
This package is no longer being updated! Please look for alternatives if that bothers you.
Resize
Image resizing for the Go programming language with common interpolation methods.
Installation
$ go get github.com/nfnt/resize
It's that easy!
Usage
This package needs at least Go 1.1. Import package with
import "github.com/nfnt/resize"
The resize package provides 2 functions:
resize.Resize
creates a scaled image with new dimensions (width
,height
) using the interpolation functioninterp
. If eitherwidth
orheight
is set to 0, it will be set to an aspect ratio preserving value.resize.Thumbnail
downscales an image preserving its aspect ratio to the maximum dimensions (maxWidth
,maxHeight
). It will return the original image if original sizes are smaller than the provided dimensions.
resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image
resize.Thumbnail(maxWidth, maxHeight uint, img image.Image, interp resize.InterpolationFunction) image.Image
The provided interpolation functions are (from fast to slow execution time)
NearestNeighbor
: Nearest-neighbor interpolationBilinear
: Bilinear interpolationBicubic
: Bicubic interpolationMitchellNetravali
: Mitchell-Netravali interpolationLanczos2
: Lanczos resampling with a=2Lanczos3
: Lanczos resampling with a=3
Which of these methods gives the best results depends on your use case.
Sample usage:
package main
import (
"github.com/nfnt/resize"
"image/jpeg"
"log"
"os"
)
func main() {
// open "test.jpg"
file, err := os.Open("test.jpg")
if err != nil {
log.Fatal(err)
}
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(1000, 0, img, resize.Lanczos3)
out, err := os.Create("test_resized.jpg")
if err != nil {
log.Fatal(err)
}
defer out.Close()
// write new image to file
jpeg.Encode(out, m, nil)
}
Caveats
- Optimized access routines are used for
image.RGBA
,image.NRGBA
,image.RGBA64
,image.NRGBA64
,image.YCbCr
,image.Gray
, andimage.Gray16
types. All other image types are accessed in a generic way that will result in slow processing speed. - JPEG images are stored in
image.YCbCr
. This image format stores data in a way that will decrease processing speed. A resize may be up to 2 times slower than withimage.RGBA
.
Downsizing Samples
Downsizing is not as simple as it might look like. Images have to be filtered before they are scaled down, otherwise aliasing might occur. Filtering is highly subjective: Applying too much will blur the whole image, too little will make aliasing become apparent. Resize tries to provide sane defaults that should suffice in most cases.
Artificial sample
Original image
Nearest-Neighbor |
Bilinear |
---|---|
Bicubic |
Mitchell-Netravali |
Lanczos2 |
Lanczos3 |
Real-Life sample
Original image
Nearest-Neighbor |
Bilinear |
---|---|
Bicubic |
Mitchell-Netravali |
Lanczos2 |
Lanczos3 |
License
Copyright (c) 2012 Jan Schlicht janschlicht@gmail.com Resize is released under a MIT style license.
Top Related Projects
Imaging is a simple image processing package for Go
Image processing algorithms in pure Go
Go Graphics - 2D rendering in Go with a simple API.
smartcrop finds good image crops for arbitrary crop sizes
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
Content aware image resize library
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