Convert Figma logo to code with AI

muesli logosmartcrop

smartcrop finds good image crops for arbitrary crop sizes

1,824
113
1,824
9

Top Related Projects

10,369

Content aware image resize library

10,063

thumbor is an open-source photo thumbnail service by globo.com

A caching, resizing image proxy written in Go

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

2,112

An image resizing server written in Go

Quick Overview

Smartcrop is a Go library that implements content-aware image cropping. It analyzes the image content to find the most interesting or important areas and then crops the image to focus on these regions. This library is particularly useful for generating thumbnails or resizing images for various display purposes while preserving the most relevant parts of the image.

Pros

  • Content-aware cropping that focuses on the most important parts of the image
  • Supports various image formats (JPEG, PNG, GIF, BMP, TIFF)
  • Customizable with options for aspect ratio, resize dimensions, and crop size
  • Can be used as a standalone command-line tool or integrated into Go applications

Cons

  • May not always produce perfect results for all types of images
  • Processing large images can be computationally intensive
  • Limited to rectangular crops (no support for non-rectangular shapes)
  • Requires some understanding of image processing concepts for optimal use

Code Examples

  1. Basic usage:
import "github.com/muesli/smartcrop"

analyzer := smartcrop.NewAnalyzer()
crop, err := analyzer.FindBestCrop(img, 250, 250)
if err != nil {
    log.Fatal(err)
}
  1. Custom resizer:
import "github.com/muesli/smartcrop/nfnt"

resizer := nfnt.NewResizer()
analyzer := smartcrop.NewAnalyzer(resizer)
crop, err := analyzer.FindBestCrop(img, 250, 250)
  1. Using with custom options:
import "github.com/muesli/smartcrop"

analyzer := smartcrop.NewAnalyzer()
crop, err := analyzer.FindBestCrop(img, 250, 250, smartcrop.TopCrop)
if err != nil {
    log.Fatal(err)
}

Getting Started

To use Smartcrop in your Go project, follow these steps:

  1. Install the library:

    go get github.com/muesli/smartcrop
    
  2. Import the library in your Go code:

    import "github.com/muesli/smartcrop"
    
  3. Use the library to find the best crop for an image:

    analyzer := smartcrop.NewAnalyzer()
    crop, err := analyzer.FindBestCrop(img, width, height)
    if err != nil {
        log.Fatal(err)
    }
    // Use the crop rectangle to create your cropped image
    

Competitor Comparisons

10,369

Content aware image resize library

Pros of caire

  • Offers content-aware image resizing (seam carving) in addition to content-aware image cropping
  • Provides face detection functionality for better preservation of important features
  • Supports both CLI and library usage, offering more flexibility

Cons of caire

  • Generally slower processing time compared to smartcrop
  • May produce artifacts in some cases, especially with complex images
  • Requires more dependencies and setup

Code Comparison

smartcrop:

analyzer := smartcrop.NewAnalyzer()
crop, err := analyzer.FindBestCrop(img, 250, 250)
if err != nil {
    log.Fatal(err)
}

caire:

processor := caire.NewCarver(width, height)
processor.FaceDetect = true
processor.NewWidth = newWidth
processor.NewHeight = newHeight
err := processor.Process(input, output)

Both libraries offer content-aware image processing, but caire provides more advanced features like seam carving and face detection. smartcrop focuses primarily on finding the best crop area, while caire allows for more flexible image resizing. smartcrop is generally faster and simpler to use, making it a good choice for basic cropping needs. caire offers more comprehensive image manipulation capabilities but may require more setup and processing time.

10,063

thumbor is an open-source photo thumbnail service by globo.com

Pros of Thumbor

  • Full-featured image processing server with extensive capabilities beyond just cropping
  • Supports real-time image processing and manipulation via URL parameters
  • Offers a wide range of image filters and effects

Cons of Thumbor

  • More complex setup and configuration compared to Smartcrop
  • Requires running a separate server, which may increase infrastructure complexity
  • Potentially higher resource usage for simple cropping tasks

Code Comparison

Smartcrop (Go):

analyzer := smartcrop.NewAnalyzer()
crop, err := analyzer.FindBestCrop(img, 250, 250)

Thumbor (Python):

from thumbor.url_builder import UrlBuilder

url = UrlBuilder('http://example.com/image.jpg').width(250).height(250).smart().build()

Summary

Smartcrop is a focused library for intelligent image cropping, while Thumbor is a comprehensive image processing server. Smartcrop is simpler to integrate for basic cropping needs, but Thumbor offers more extensive features for complex image manipulation tasks. The choice between them depends on the specific requirements of your project and the level of image processing functionality needed.

A caching, resizing image proxy written in Go

Pros of imageproxy

  • More comprehensive image processing capabilities, including resizing, cropping, and format conversion
  • Built-in caching and support for multiple remote image sources
  • Designed as a standalone proxy server, offering more flexibility in deployment

Cons of imageproxy

  • More complex setup and configuration compared to smartcrop
  • Requires running a separate service, which may increase infrastructure overhead
  • Less focused on content-aware cropping, which is smartcrop's primary feature

Code Comparison

smartcrop (Go):

analyzer := smartcrop.NewAnalyzer()
crop, err := analyzer.FindBestCrop(img, 250, 250)

imageproxy (Go):

params := imageproxy.Params{Width: 250, Height: 250, SmartCrop: true}
img, err := proxy.Transform(sourceImage, params)

Both libraries offer image cropping functionality, but smartcrop focuses specifically on content-aware cropping, while imageproxy provides a broader range of image processing options. smartcrop's API is simpler and more focused, while imageproxy offers more flexibility and features at the cost of increased complexity.

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

Pros of Imaginary

  • Offers a wide range of image processing operations beyond just cropping
  • Provides a microservice architecture with HTTP API for easy integration
  • Supports multiple storage backends (local, S3, Google Cloud)

Cons of Imaginary

  • More complex setup and configuration compared to Smartcrop
  • Requires running a separate service, which may increase infrastructure overhead
  • Potentially higher resource usage due to its broader feature set

Code Comparison

Smartcrop (Go):

topCrop, err := smartcrop.SmartCrop(&img, 250, 250)
if err != nil {
    log.Fatal(err)
}

Imaginary (HTTP API):

GET /crop?width=250&height=250&file=image.jpg HTTP/1.1
Host: localhost:8088

Summary

Smartcrop focuses specifically on content-aware image cropping, while Imaginary provides a more comprehensive image processing solution. Smartcrop is simpler to integrate directly into Go applications, whereas Imaginary offers a microservice approach with broader functionality. The choice between the two depends on the specific requirements of your project, such as the need for additional image processing features or preference for a standalone service versus a library.

2,112

An image resizing server written in Go

Pros of picfit

  • Offers a complete image processing server with HTTP API
  • Supports multiple storage backends (file system, S3, etc.)
  • Provides more image manipulation options (resize, crop, rotate, etc.)

Cons of picfit

  • More complex setup and configuration
  • Requires running a separate server
  • Potentially higher resource usage for full server functionality

Code comparison

smartcrop (Go):

type Analyzer interface {
    Score(img image.Image) Score
}

func SmartCrop(img image.Image, width, height int) (image.Rectangle, error) {
    // ... implementation
}

picfit (Go):

type Engine struct {
    // ... fields
}

func (e *Engine) Transform(ctx context.Context, operation string, qs url.Values) (*image.NRGBA, error) {
    // ... implementation
}

Summary

smartcrop is a focused library for content-aware image cropping, while picfit is a full-featured image processing server. smartcrop is simpler to integrate into existing Go applications for basic smart cropping, whereas picfit offers a broader range of image manipulation features through an HTTP API. The choice between them depends on the specific requirements of the project, such as the need for a standalone server, variety of image operations, or integration complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

smartcrop

Latest Release Build Status Coverage Status Go ReportCard GoDoc

smartcrop finds good image crops for arbitrary sizes. It is a pure Go implementation, based on Jonas Wagner's smartcrop.js

Example Image: https://www.flickr.com/photos/usfwspacific/8182486789 by Washington Dept of Fish and Wildlife, originally licensed under CC-BY-2.0 when the image was imported back in September 2014

Example Image: https://www.flickr.com/photos/endogamia/5682480447 by Leon F. Cabeiro (N. Feans), licensed under CC-BY-2.0

Installation

Make sure you have a working Go environment (Go 1.12 or higher is required). See the install instructions.

To install smartcrop, simply run:

go get github.com/muesli/smartcrop

To compile it from source:

git clone https://github.com/muesli/smartcrop.git
cd smartcrop
go build

Example

package main

import (
	"fmt"
	"image"
	_ "image/png"
	"os"

	"github.com/muesli/smartcrop"
	"github.com/muesli/smartcrop/nfnt"
)

func main() {
	f, _ := os.Open("image.png")
	img, _, _ := image.Decode(f)

	analyzer := smartcrop.NewAnalyzer(nfnt.NewDefaultResizer())
	topCrop, _ := analyzer.FindBestCrop(img, 250, 250)

	// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
	fmt.Printf("Top crop: %+v\n", topCrop)

	type SubImager interface {
		SubImage(r image.Rectangle) image.Image
	}
	croppedimg := img.(SubImager).SubImage(topCrop)
	// ...
}

Also see the test cases in smartcrop_test.go and cli application in cmd/smartcrop/ for further working examples.

Simple CLI application

go install github.com/muesli/smartcrop/cmd/smartcrop

Usage of smartcrop:
  -height int
        crop height
  -input string
        input filename
  -output string
        output filename
  -quality int
        jpeg quality (default 85)
  -resize
        resize after cropping (default true)
  -width int
        crop width

Example: smartcrop -input examples/gopher.jpg -output gopher_cropped.jpg -width 300 -height 150

Sample Data

You can find a bunch of test images for the algorithm here.

Feedback

Got some feedback or suggestions? Please open an issue or drop me a note!