项目作者: remove-bg

项目描述 :
Official ruby gem for the remove.bg API
高级语言: Ruby
项目地址: git://github.com/remove-bg/ruby.git
创建时间: 2019-03-09T18:46:50Z
项目社区:https://github.com/remove-bg/ruby

开源协议:MIT License

下载


RemoveBG

remove.bg Ruby Gem



Gem Version
Gem Total Downloads
CircleCI
GitHub License
Codecov
Sonar Quality Gate
Violations
Dependencies

Requirements

This gem is compatible with Ruby 3.1+ and can be used with
Faraday version 2 and above

An API key (free) from remove.bg is required.

Quickstart Installation

Add the gem to your Gemfile and run bundle install:

  1. gem "remove_bg"

Or run gem install remove_bg to install globally.

Please note the base configuration has the following resolution limits:

Output format Resolution limit
PNG 10 megapixels
JPG 50 megapixels

Full installation

For best performance and quality the gem requires an image processing library.
Please install one of the following libraries:

The gem will auto-detect any image processing libraries present. However, you can
also explicitly configure which library to use:

  1. RemoveBg.configure do |config|
  2. config.image_processor = :minimagick # For ImageMagick or GraphicsMagick
  3. # or
  4. config.image_processor = :vips
  5. end

The full installation has the following resolution limits:

Output format Resolution limit
PNG 10 megapixels
JPG 50 megapixels
ZIP 50 megapixels

Usage

For more in-depth documentation please see RubyDoc

Configuring an API key

To configure a global API key (used by default unless overridden per request):

  1. RemoveBg.configure do |config|
  2. config.api_key = "<api-key>"
  3. end

It’s not recommended to commit your API key to version control. You may want to
read the API key from an environment variable (e.g.
ENV.fetch("REMOVE_BG_API_KEY")) or find an alternative method.

Removing the background from an image

Currently the gem supports removing the background from a file or a URL:

  1. RemoveBg.from_file("image.png")
  2. RemoveBg.from_url("http://example.com/image.png")

Request options

The processing options outlined in the API reference
can be specified per request:

  1. RemoveBg.from_file("image.png", size: "hd", type: "product", channels: "rgba")

The API key can also be specified per request:

  1. RemoveBg.from_file("image.png", api_key: "<api-key>")

Handling the result

Background removal requests return a result object which includes the processed
image data and the metadata about the operation.

  1. result = RemoveBg.from_file("image.png")
  2. result.data # => "\x89PNG..."
  3. result.height # => 333
  4. result.width # => 500
  5. result.credits_charged # => 1.0

There’s also a #save convenience method:

  1. result.save("processed/image.png")
  2. # Overwrite any existing file
  3. result.save!("processed/image.png")

Producing transparent images over 10 megapixels

After configuring a full installation (detailed above) you can process images
over 10 megapixels with a transparent output.

Process images with either the png or zip format. If you specify the zip
format it’s possible to save the archive and handle composition yourself.

  1. result = RemoveBg.from_file("large-image.jpg", format: "zip")
  2. result.save("result-with-transparency.png")
  3. # or
  4. result.save_zip("result.zip") # If you want to handle composition yourself

Rate limits

The API has rate limits. Image processing results include the
rate limit information:

  1. result = RemoveBg.from_file("image.jpg")
  2. result.rate_limit.to_s
  3. # => <RateLimit reset_at='2020-05-20T12:00:00Z' total=500 remaining=499 retry_after_seconds=nil>

If you exceed the rate limit a RemoveBg::RateLimitError exception will be
raised. This also contains further information via the #rate_limit method.

Fetching account information

To display the account information for the currently configured
API key:

  1. account = RemoveBg.account_info # If an API key is set via RemoveBg.configuration
  2. # or
  3. account = RemoveBg.account_info(api_key: "<api_key>")
  4. account.api.free_calls # => 50
  5. account.credits.total # => 200

Examples

License

The gem is available as open source under the terms of the MIT License.

Contributing

Bug reports and pull requests are welcome on GitHub at remove-bg/ruby.

Development

Setup

After checking out the repo, run bin/setup to install dependencies. Then, run
rake spec to run the tests.

Releasing a new version

To release a new version, update the version number in version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push
git commits and tags, and push the .gem file to
rubygems.org.

Documentation

To preview the YARD documentation locally run:

  1. bundle exec yard server --reload
  2. open http://localhost:8808/