Code Monkey home page Code Monkey logo

auto-image-cropper's Introduction

auto-image-cropper

Crates.io Build Status

  • Removes extra white boundaries from images to correctly resize canvas.

  • Thanks @matthewkmayer for bringing down cropping times (see #1 and #2)!

Screenshots

The borders are just to represent the actual images tested with. (click to zoom)

Installation and Usage

$ cargo install auto-image-cropper

or if you like to live on the bleeding edge

$ git clone https://github.com/Ritiek/auto-image-cropper
$ cd auto-image-cropper
$ cargo build --release

Use ./target/release/autocrop to start using the tool.

USAGE:
    autocrop [OPTIONS] --input <LOCATION>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -i, --input <LOCATION>     Location of input image/directory
    -o, --output <LOCATION>    Location of output image/directory

For example:

$ ./target/release/autocrop -i benchmarking/face.jpg -o face.out.jpg

Python Bindings

This tool also provides Python bindings via PyO3 (using Rust FFI).

You need a Rust nightly toolchain to proceed (PyO3 does not support Rust stable at the moment). If you have rustup, run:

$ rustup default nightly

to switch to nightly channel.

This feature can then be enabled by passing --features "python-binding" to cargo when compiling.

For example:

$ cargo build --release --features "python-binding"

This will generate a dynamic library (*.so) on Linux machines with the name ./target/release/libauto_image_cropper.so.

Let's move this dynamic library into our current working directory:

$ mv target/release/libauto_image_cropper.so .

It can now be utilized via Python scripts using:

>>> import libauto_image_cropper

# Returns the optimal top-left and bottom-right corner
# coordinates for a given image to be cropped
>>> (top_left, bottom_right) = libauto_image_cropper.calculate_corners("benchmarking/face.jpg")
>>> print(top_left)
(442, 73)
>>> print(bottom_right)
(783, 536)

(I haven't checked this out on Windows or OSX, but should follow a similar procedure)

Benchmarks

  • This tool was hackishly re-written in Python to compare with Rust - just for fun.

  • The benchmarks were done on a MacBook Air running macOS Sierra 10.12.2.

Image Python Rust Times Faster
face.jpg 0.867s 0.155s 5.59
square.png 1.682s 0.142s 11.84
flowers.jpg 2.222s 0.476s 4.66
human.jpg 2.362s 0.294s 8.02
pets.jpg 5.366s 1.704s 3.14
agent47.jpg 51.559s 7.519s 6.85
  • Python struggles to find the optimal coordinates but is quick (quicker than Rust) when saving the cropped image back to disk. Rust really outperforms while finding the optimal coordinates.

[2020 / 03] UPDATE: These benchmarks were done in 2017. Rust and its image libraries have a come long way ahead and I believe should now offer even better performance! I'll update these benchmarks when I'm able to.

License

The MIT License

auto-image-cropper's People

Contributors

divyansh2512 avatar matthewkmayer avatar ritiek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.