remove_bg 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +2 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +21 -3
- data/README.md +71 -35
- data/lib/remove_bg.rb +17 -0
- data/lib/remove_bg/account_info.rb +5 -1
- data/lib/remove_bg/api_client.rb +19 -0
- data/lib/remove_bg/composite_result.rb +10 -0
- data/lib/remove_bg/configuration.rb +14 -3
- data/lib/remove_bg/error.rb +9 -1
- data/lib/remove_bg/http_connection.rb +2 -0
- data/lib/remove_bg/image_composer.rb +18 -0
- data/lib/remove_bg/request_options.rb +32 -0
- data/lib/remove_bg/result.rb +16 -1
- data/lib/remove_bg/version.rb +1 -1
- data/remove_bg.gemspec +4 -0
- metadata +58 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47f8cd273ddfe8639badf0fd59050342af16e542a508162f77fbec7b7a2a5dd4
|
4
|
+
data.tar.gz: 5adcc396f765f7a4858e7c7c4d82d7610b5d42e2200401be29b5ea543f5fdc88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ad4065335850911ee8b45604ed0de465742bdf183078d0ae4e9de52fb1f77519d4d9bd5aa7a042d04e9c0960a8af4f3d0e84cfdb49c6ae48e773626c6ad9639
|
7
|
+
data.tar.gz: a265d257a9e77bd9d87b9ef42c2aff4b0d7b8b7b3482a8717a44b4d3192c5064b0e4e67e62b9d513ecbb67e317c8b470db19194b350ade66d73b602b6ec9a7c8
|
data/.circleci/config.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## Unreleased
|
4
|
+
|
5
|
+
## 1.5.0
|
6
|
+
|
7
|
+
- Auto-detect any available image processing libraries
|
8
|
+
- Reduce bandwidth usage by using ZIP format where possible
|
9
|
+
|
3
10
|
## 1.4.1
|
4
11
|
|
5
12
|
- Fixes binary encoding issue - via [#15](https://github.com/remove-bg/ruby/pull/15)
|
data/Gemfile.lock
CHANGED
@@ -11,7 +11,7 @@ GIT
|
|
11
11
|
PATH
|
12
12
|
remote: .
|
13
13
|
specs:
|
14
|
-
remove_bg (1.
|
14
|
+
remove_bg (1.5.0)
|
15
15
|
faraday (>= 0.15, < 2)
|
16
16
|
image_processing (>= 1.9, < 2)
|
17
17
|
rubyzip (>= 2.0, < 3)
|
@@ -21,18 +21,24 @@ GEM
|
|
21
21
|
specs:
|
22
22
|
addressable (2.7.0)
|
23
23
|
public_suffix (>= 2.0.2, < 5.0)
|
24
|
+
codecov (0.1.16)
|
25
|
+
json
|
26
|
+
simplecov
|
27
|
+
url
|
24
28
|
coderay (1.1.2)
|
25
29
|
crack (0.4.3)
|
26
30
|
safe_yaml (~> 1.0.0)
|
27
31
|
diff-lcs (1.3)
|
32
|
+
docile (1.3.2)
|
28
33
|
dotenv (2.7.5)
|
29
34
|
faraday (1.0.1)
|
30
35
|
multipart-post (>= 1.2, < 3)
|
31
|
-
ffi (1.
|
36
|
+
ffi (1.13.1)
|
32
37
|
hashdiff (1.0.1)
|
33
38
|
image_processing (1.11.0)
|
34
39
|
mini_magick (>= 4.9.5, < 5)
|
35
40
|
ruby-vips (>= 2.0.17, < 3)
|
41
|
+
json (2.3.0)
|
36
42
|
method_source (1.0.0)
|
37
43
|
mini_magick (4.10.1)
|
38
44
|
multipart-post (2.1.1)
|
@@ -47,7 +53,7 @@ GEM
|
|
47
53
|
rspec-mocks (~> 3.9.0)
|
48
54
|
rspec-core (3.9.2)
|
49
55
|
rspec-support (~> 3.9.3)
|
50
|
-
rspec-expectations (3.9.
|
56
|
+
rspec-expectations (3.9.2)
|
51
57
|
diff-lcs (>= 1.2.0, < 2.0)
|
52
58
|
rspec-support (~> 3.9.0)
|
53
59
|
rspec-mocks (3.9.1)
|
@@ -62,12 +68,20 @@ GEM
|
|
62
68
|
ffi (~> 1.9)
|
63
69
|
rubyzip (2.3.0)
|
64
70
|
safe_yaml (1.0.5)
|
71
|
+
simplecov (0.18.5)
|
72
|
+
docile (~> 1.1)
|
73
|
+
simplecov-html (~> 0.11)
|
74
|
+
simplecov-html (0.12.2)
|
65
75
|
thor (1.0.1)
|
76
|
+
url (0.3.2)
|
66
77
|
vcr (5.1.0)
|
78
|
+
vcr_better_binary (0.1.0)
|
79
|
+
vcr (~> 5.0)
|
67
80
|
webmock (3.8.3)
|
68
81
|
addressable (>= 2.3.6)
|
69
82
|
crack (>= 0.3.2)
|
70
83
|
hashdiff (>= 0.4.0, < 2.0.0)
|
84
|
+
yard (0.9.25)
|
71
85
|
|
72
86
|
PLATFORMS
|
73
87
|
ruby
|
@@ -75,6 +89,7 @@ PLATFORMS
|
|
75
89
|
DEPENDENCIES
|
76
90
|
appraisal!
|
77
91
|
bundler (~> 2.0)
|
92
|
+
codecov
|
78
93
|
dotenv
|
79
94
|
pry
|
80
95
|
rake
|
@@ -82,8 +97,11 @@ DEPENDENCIES
|
|
82
97
|
rspec (~> 3.8)
|
83
98
|
rspec-with_params
|
84
99
|
rspec_junit_formatter
|
100
|
+
simplecov
|
85
101
|
vcr
|
102
|
+
vcr_better_binary
|
86
103
|
webmock
|
104
|
+
yard
|
87
105
|
|
88
106
|
BUNDLED WITH
|
89
107
|
2.1.4
|
data/README.md
CHANGED
@@ -2,16 +2,58 @@
|
|
2
2
|
|
3
3
|
# Ruby library
|
4
4
|
|
5
|
-
[![CircleCI](https://circleci.com/gh/remove-bg/ruby/tree/master.svg?style=shield)](https://circleci.com/gh/remove-bg/ruby/tree/master) [![Gem Version](https://badge.fury.io/rb/remove_bg.svg)](https://rubygems.org/gems/remove_bg)
|
5
|
+
[![CircleCI](https://circleci.com/gh/remove-bg/ruby/tree/master.svg?style=shield)](https://circleci.com/gh/remove-bg/ruby/tree/master) [![Gem Version](https://badge.fury.io/rb/remove_bg.svg)](https://rubygems.org/gems/remove_bg) [![codecov](https://codecov.io/gh/remove-bg/ruby/branch/master/graph/badge.svg)](https://codecov.io/gh/remove-bg/ruby)
|
6
6
|
|
7
|
-
##
|
7
|
+
## Quickstart installation
|
8
8
|
|
9
|
-
|
10
|
-
- Or install it yourself as: `gem install remove_bg`
|
9
|
+
Add the gem to your `Gemfile` and run `bundle install`:
|
11
10
|
|
12
|
-
|
11
|
+
```
|
12
|
+
gem "remove_bg"
|
13
|
+
```
|
14
|
+
|
15
|
+
Or run `gem install remove_bg` to install globally.
|
16
|
+
|
17
|
+
Please note the base configuration has the following resolution limits:
|
18
|
+
|
19
|
+
| Output format | Resolution limit |
|
20
|
+
|---------------|------------------|
|
21
|
+
| PNG | 10 megapixels |
|
22
|
+
| JPG | 25 megapixels |
|
23
|
+
|
24
|
+
## Full installation
|
25
|
+
|
26
|
+
For best performance and quality the gem requires an image processing library.
|
27
|
+
Please install one of the following libraries:
|
28
|
+
|
29
|
+
- [ImageMagick](https://www.imagemagick.org/)
|
30
|
+
- [GraphicsMagick](http://www.graphicsmagick.org/)
|
31
|
+
- [libvips](http://libvips.github.io/libvips/)
|
32
|
+
|
33
|
+
The gem will auto-detect any image processing libraries present. However you may
|
34
|
+
prefer to explicitly configure which library to use:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
RemoveBg.configure do |config|
|
38
|
+
config.image_processor = :minimagick # For ImageMagick or GraphicsMagick
|
39
|
+
# or
|
40
|
+
config.image_processor = :vips
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
The full installation has the following resolution limits:
|
45
|
+
|
46
|
+
| Output format | Resolution limit |
|
47
|
+
|---------------|------------------|
|
48
|
+
| PNG | 25 megapixels |
|
49
|
+
| JPG | 25 megapixels |
|
50
|
+
| ZIP | 25 megapixels |
|
13
51
|
|
14
|
-
|
52
|
+
# Usage
|
53
|
+
|
54
|
+
For more in-depth documentation please see [RubyDoc](https://www.rubydoc.info/gems/remove_bg)
|
55
|
+
|
56
|
+
## Configuring an API key
|
15
57
|
|
16
58
|
To configure a global API key (used by default unless overridden per request):
|
17
59
|
|
@@ -25,7 +67,7 @@ It's not recommended to commit your API key to version control. You may want to
|
|
25
67
|
read the API key from an environment variable (e.g.
|
26
68
|
`ENV.fetch("REMOVE_BG_API_KEY")`) or find an alternative method.
|
27
69
|
|
28
|
-
|
70
|
+
## Removing the background from an image
|
29
71
|
|
30
72
|
Currently the gem supports removing the background from a file or a URL:
|
31
73
|
|
@@ -34,7 +76,7 @@ RemoveBg.from_file("image.png")
|
|
34
76
|
RemoveBg.from_url("http://example.com/image.png")
|
35
77
|
```
|
36
78
|
|
37
|
-
|
79
|
+
## Request options
|
38
80
|
|
39
81
|
The processing options outlined in the [API reference](https://www.remove.bg/api)
|
40
82
|
can be specified per request:
|
@@ -49,7 +91,7 @@ The API key can also be specified per request:
|
|
49
91
|
RemoveBg.from_file("image.png", api_key: "<api-key>")
|
50
92
|
```
|
51
93
|
|
52
|
-
|
94
|
+
## Handling the result
|
53
95
|
|
54
96
|
Background removal requests return a result object which includes the processed
|
55
97
|
image data and the metadata about the operation.
|
@@ -69,30 +111,13 @@ result.save("processed/image.png")
|
|
69
111
|
result.save("image.png", overwrite: true) # Careful!
|
70
112
|
```
|
71
113
|
|
72
|
-
|
73
|
-
|
74
|
-
The Remove.bg API provides a [ZIP format](https://www.remove.bg/api#zip-format)
|
75
|
-
which includes all the information required to efficiently composite a large
|
76
|
-
image with transparency.
|
114
|
+
## Producing transparent images over 10 megapixels
|
77
115
|
|
78
|
-
|
79
|
-
|
116
|
+
After configuring a full installation (detailed above) you can process images
|
117
|
+
over 10 megapixels with a transparent output.
|
80
118
|
|
81
|
-
|
82
|
-
|
83
|
-
[libvips]: http://libvips.github.io/libvips/
|
84
|
-
|
85
|
-
Please configure the image processing library you'd like to use:
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
RemoveBg.configure do |config|
|
89
|
-
config.image_processor = :minimagick # For ImageMagick or GraphicsMagick
|
90
|
-
# or
|
91
|
-
config.image_processor = :vips
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
Then process images specifying the `zip` format:
|
119
|
+
Process images with either the `png` or `zip` format. If you specify the `zip`
|
120
|
+
format it's possible to save the archive and handle composition yourself.
|
96
121
|
|
97
122
|
```ruby
|
98
123
|
result = RemoveBg.from_file("large-image.jpg", format: "zip")
|
@@ -102,7 +127,7 @@ result.save("result-with-transparency.png")
|
|
102
127
|
result.save_zip("result.zip") # If you want to handle composition yourself
|
103
128
|
```
|
104
129
|
|
105
|
-
|
130
|
+
## Rate limits
|
106
131
|
|
107
132
|
The [API has rate limits][rate-limits]. Image processing results include the
|
108
133
|
rate limit information:
|
@@ -118,7 +143,7 @@ raised. This also contains further information via the `#rate_limit` method.
|
|
118
143
|
|
119
144
|
[rate-limits]: https://www.remove.bg/api#rate-limit
|
120
145
|
|
121
|
-
|
146
|
+
## Fetching account information
|
122
147
|
|
123
148
|
To display the [account information][account-info] for the currently configured
|
124
149
|
API key:
|
@@ -150,11 +175,22 @@ Bug reports and pull requests are welcome on GitHub at [remove-bg/ruby](https://
|
|
150
175
|
|
151
176
|
## Development
|
152
177
|
|
178
|
+
### Setup
|
179
|
+
|
153
180
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
154
181
|
`rake spec` to run the tests.
|
155
182
|
|
156
|
-
|
157
|
-
release a new version, update the version number in `version.rb`, and then run
|
183
|
+
### Releasing a new version
|
184
|
+
To release a new version, update the version number in `version.rb`, and then run
|
158
185
|
`bundle exec rake release`, which will create a git tag for the version, push
|
159
186
|
git commits and tags, and push the `.gem` file to
|
160
187
|
[rubygems.org](https://rubygems.org).
|
188
|
+
|
189
|
+
### Documentation
|
190
|
+
|
191
|
+
To preview the [YARD documentation](https://yardoc.org/) locally run:
|
192
|
+
|
193
|
+
```
|
194
|
+
bundle exec yard server --reload
|
195
|
+
open http://localhost:8808/
|
196
|
+
```
|
data/lib/remove_bg.rb
CHANGED
@@ -4,21 +4,38 @@ require "remove_bg/configuration"
|
|
4
4
|
require "remove_bg/request_options"
|
5
5
|
|
6
6
|
module RemoveBg
|
7
|
+
# Removes the background from an image on the local file system
|
8
|
+
# @param image_path [String] Path to the input image
|
9
|
+
# @param options [Hash<Symbol, Object>] Image processing options (see API docs)
|
10
|
+
# @return [RemoveBg::Result|RemoveBg::CompositeResult] a processed image result
|
11
|
+
#
|
7
12
|
def self.from_file(image_path, raw_options = {})
|
8
13
|
options = RemoveBg::RequestOptions.new(raw_options)
|
9
14
|
ApiClient.new.remove_from_file(image_path, options)
|
10
15
|
end
|
11
16
|
|
17
|
+
# Removes the background from the image at the URL specified
|
18
|
+
# @param image_url [String] Absolute URL of the input image
|
19
|
+
# @param options [Hash<Symbol, Object>] Image processing options (see API docs)
|
20
|
+
# @return [RemoveBg::Result|RemoveBg::CompositeResult] A processed image result
|
21
|
+
#
|
12
22
|
def self.from_url(image_url, raw_options = {})
|
13
23
|
options = RemoveBg::RequestOptions.new(raw_options)
|
14
24
|
ApiClient.new.remove_from_url(image_url, options)
|
15
25
|
end
|
16
26
|
|
27
|
+
# Fetches account information for the globally configured API key, or a
|
28
|
+
# specific API key if provided
|
29
|
+
# @param options [Hash<Symbol, Object>]
|
30
|
+
# @return [RemoveBg::AccountInfo]
|
31
|
+
#
|
17
32
|
def self.account_info(raw_options = {})
|
18
33
|
options = RemoveBg::BaseRequestOptions.new(raw_options)
|
19
34
|
ApiClient.new.account_info(options)
|
20
35
|
end
|
21
36
|
|
37
|
+
# Yields the global Remove.bg configuration
|
38
|
+
# @yield [RemoveBg::Configuration]
|
22
39
|
def self.configure
|
23
40
|
yield RemoveBg::Configuration.configuration
|
24
41
|
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module RemoveBg
|
2
2
|
class AccountInfo
|
3
|
-
|
3
|
+
# @return [RemoveBg::AccountInfo::ApiInfo]
|
4
|
+
attr_reader :api
|
5
|
+
|
6
|
+
# @return [RemoveBg::AccountInfo::CreditsInfo]
|
7
|
+
attr_reader :credits
|
4
8
|
|
5
9
|
def initialize(attributes)
|
6
10
|
@api = ApiInfo.new(**attributes.fetch(:api))
|
data/lib/remove_bg/api_client.rb
CHANGED
@@ -16,21 +16,40 @@ module RemoveBg
|
|
16
16
|
class ApiClient
|
17
17
|
include RemoveBg::Api
|
18
18
|
|
19
|
+
# @param connection [Faraday::Connection]
|
20
|
+
#
|
19
21
|
def initialize(connection: RemoveBg::HttpConnection.build)
|
20
22
|
@connection = connection
|
21
23
|
end
|
22
24
|
|
25
|
+
# Removes the background from an image on the local file system
|
26
|
+
# @param image_path [String]
|
27
|
+
# @param options [RemoveBg::RequestOptions]
|
28
|
+
# @return [RemoveBg::Result|RemoveBg::CompositeResult]
|
29
|
+
# @raise [RemoveBg::Error]
|
30
|
+
#
|
23
31
|
def remove_from_file(image_path, options)
|
24
32
|
data = options.data.merge(image_file: Upload.for_file(image_path))
|
25
33
|
request_remove_bg(data, options.api_key)
|
26
34
|
end
|
27
35
|
|
36
|
+
# Removes the background from the image at the URL specified
|
37
|
+
# @param image_url [String]
|
38
|
+
# @param options [RemoveBg::RequestOptions]
|
39
|
+
# @return [RemoveBg::Result|RemoveBg::CompositeResult]
|
40
|
+
# @raise [RemoveBg::Error]
|
41
|
+
#
|
28
42
|
def remove_from_url(image_url, options)
|
29
43
|
RemoveBg::UrlValidator.validate(image_url)
|
30
44
|
data = options.data.merge(image_url: image_url)
|
31
45
|
request_remove_bg(data, options.api_key)
|
32
46
|
end
|
33
47
|
|
48
|
+
# Fetches account information
|
49
|
+
# @param options [RemoveBg::BaseRequestOptions]
|
50
|
+
# @return [RemoveBg::AccountInfo]
|
51
|
+
# @raise [RemoveBg::Error]
|
52
|
+
#
|
34
53
|
def account_info(options)
|
35
54
|
request_account_info(options.api_key)
|
36
55
|
end
|
@@ -1,7 +1,17 @@
|
|
1
1
|
require_relative "result"
|
2
2
|
|
3
3
|
module RemoveBg
|
4
|
+
# Handles image composition for larger images (over 10MP) where transparency is
|
5
|
+
# required.
|
6
|
+
# @see RemoveBg::Result
|
7
|
+
#
|
4
8
|
class CompositeResult < Result
|
9
|
+
# Saves the ZIP archive containing the alpha.png and color.jpg files
|
10
|
+
# (useful if you want to handle composition yourself)
|
11
|
+
# @param file_path [string]
|
12
|
+
# @param overwrite [boolean] Overwrite any existing file at the specified path
|
13
|
+
# @return [nil]
|
14
|
+
#
|
5
15
|
def save_zip(file_path, overwrite: false)
|
6
16
|
if File.exist?(file_path) && !overwrite
|
7
17
|
raise FileOverwriteError.new(file_path)
|
@@ -1,13 +1,24 @@
|
|
1
|
+
require_relative "image_composer"
|
2
|
+
|
1
3
|
module RemoveBg
|
2
4
|
class Configuration
|
3
|
-
attr_accessor :api_key, :image_processor
|
5
|
+
attr_accessor :api_key, :image_processor, :auto_upgrade_png_to_zip
|
4
6
|
|
5
7
|
def self.configuration
|
6
|
-
@configuration ||= Configuration.new
|
8
|
+
@configuration ||= Configuration.new.tap do |config|
|
9
|
+
config.image_processor = ImageComposer.detect_image_processor
|
10
|
+
|
11
|
+
# Upgrade to ZIP where possible to save bandwith
|
12
|
+
config.auto_upgrade_png_to_zip = true
|
13
|
+
end
|
7
14
|
end
|
8
15
|
|
9
16
|
def self.reset
|
10
|
-
@configuration =
|
17
|
+
@configuration = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def can_process_images?
|
21
|
+
!image_processor.nil?
|
11
22
|
end
|
12
23
|
end
|
13
24
|
end
|
data/lib/remove_bg/error.rb
CHANGED
@@ -2,7 +2,11 @@ module RemoveBg
|
|
2
2
|
class Error < StandardError; end
|
3
3
|
|
4
4
|
class HttpError < Error
|
5
|
-
|
5
|
+
# @return [Faraday::Response]
|
6
|
+
attr_reader :http_response
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
attr_reader :http_response_body
|
6
10
|
|
7
11
|
def initialize(message, http_response, http_response_body)
|
8
12
|
@http_response = http_response
|
@@ -11,9 +15,13 @@ module RemoveBg
|
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
18
|
+
# Raised for all HTTP 4XX errors
|
14
19
|
class ClientHttpError < HttpError; end
|
20
|
+
|
21
|
+
# Raised for all HTTP 5XX errors
|
15
22
|
class ServerHttpError < HttpError; end
|
16
23
|
|
24
|
+
# Raised for HTTP 429 status code
|
17
25
|
class RateLimitError < ClientHttpError
|
18
26
|
attr_reader :rate_limit
|
19
27
|
|
@@ -1,7 +1,25 @@
|
|
1
1
|
require_relative "error"
|
2
2
|
|
3
3
|
module RemoveBg
|
4
|
+
# Combines alpha.png and color.jpg files to produce a full-sized transparent PNG.
|
5
|
+
# An image processing library (ImageMagick, GraphicsMagick, or libvips) must
|
6
|
+
# be available on the system.
|
7
|
+
# @see RemoveBg::CompositeResult
|
8
|
+
#
|
4
9
|
class ImageComposer
|
10
|
+
DEFAULT_BINARY_DETECTOR = lambda do |binary_name|
|
11
|
+
system("which", binary_name, out: File::NULL)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.detect_image_processor(detector: DEFAULT_BINARY_DETECTOR)
|
15
|
+
case
|
16
|
+
when detector.call("magick"), detector.call("convert"), detector.call("gm")
|
17
|
+
:minimagick
|
18
|
+
when detector.call("vips")
|
19
|
+
:vips
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
5
23
|
def compose(color_file:, alpha_file:, destination_path:)
|
6
24
|
image = case configured_image_processor
|
7
25
|
when :vips
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require_relative "base_request_options"
|
2
2
|
|
3
3
|
module RemoveBg
|
4
|
+
# Options for image processing requests. Arbitary options are passed directly to the API.
|
5
|
+
#
|
4
6
|
class RequestOptions < BaseRequestOptions
|
5
7
|
SIZE_REGULAR = "regular"
|
6
8
|
SIZE_MEDIUM = "medium"
|
@@ -15,10 +17,40 @@ module RemoveBg
|
|
15
17
|
CHANNELS_RGBA = "rgba"
|
16
18
|
CHANNELS_ALPHA = "alpha"
|
17
19
|
|
20
|
+
FORMAT_PNG = "png"
|
21
|
+
FORMAT_ZIP = "zip"
|
22
|
+
FORMAT_JPG = "jpg"
|
23
|
+
|
18
24
|
def initialize(raw_options = {})
|
19
25
|
options = raw_options.dup
|
20
26
|
options[:size] ||= SIZE_AUTO
|
27
|
+
|
28
|
+
if options.key?(:format)
|
29
|
+
options[:format] = optimize_format(options[:format])
|
30
|
+
end
|
31
|
+
|
21
32
|
super(options)
|
22
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Save bandwidth where possible
|
38
|
+
def optimize_format(requested_format)
|
39
|
+
requested_png = requested_format.to_s.casecmp?(FORMAT_PNG)
|
40
|
+
|
41
|
+
if requested_png && optimization_enabled? && can_process_images?
|
42
|
+
FORMAT_ZIP
|
43
|
+
else
|
44
|
+
requested_format
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def can_process_images?
|
49
|
+
RemoveBg::Configuration.configuration.can_process_images?
|
50
|
+
end
|
51
|
+
|
52
|
+
def optimization_enabled?
|
53
|
+
RemoveBg::Configuration.configuration.auto_upgrade_png_to_zip
|
54
|
+
end
|
23
55
|
end
|
24
56
|
end
|
data/lib/remove_bg/result.rb
CHANGED
@@ -5,10 +5,17 @@ require_relative "error"
|
|
5
5
|
require_relative "image_composer"
|
6
6
|
|
7
7
|
module RemoveBg
|
8
|
+
# Provides convenience methods to save the processed image, read the image data,
|
9
|
+
# and access metadata such as the image height/width and credits charged.
|
10
|
+
#
|
8
11
|
class Result
|
9
12
|
extend ::Forwardable
|
10
13
|
|
11
|
-
|
14
|
+
# @return [RemoveBg::ResultMetadata]
|
15
|
+
attr_reader :metadata
|
16
|
+
|
17
|
+
# @return [RemoveBg::RateLimitInfo]
|
18
|
+
attr_reader :rate_limit
|
12
19
|
|
13
20
|
def_delegators :metadata, :type, :width, :height, :credits_charged
|
14
21
|
|
@@ -18,6 +25,11 @@ module RemoveBg
|
|
18
25
|
@rate_limit = rate_limit
|
19
26
|
end
|
20
27
|
|
28
|
+
# Saves the processed image to the path specified
|
29
|
+
# @param file_path [string]
|
30
|
+
# @param overwrite [boolean] Overwrite any existing file at the specified path
|
31
|
+
# @return [nil]
|
32
|
+
#
|
21
33
|
def save(file_path, overwrite: false)
|
22
34
|
if File.exist?(file_path) && !overwrite
|
23
35
|
raise FileOverwriteError.new(file_path)
|
@@ -26,6 +38,9 @@ module RemoveBg
|
|
26
38
|
FileUtils.cp(image_file, file_path)
|
27
39
|
end
|
28
40
|
|
41
|
+
# Returns the binary data of the processed image
|
42
|
+
# @return [String]
|
43
|
+
#
|
29
44
|
def data
|
30
45
|
image_file.rewind
|
31
46
|
image_file.read
|
data/lib/remove_bg/version.rb
CHANGED
data/remove_bg.gemspec
CHANGED
@@ -28,12 +28,16 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_dependency "rubyzip", ">= 2.0", "< 3"
|
29
29
|
|
30
30
|
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "codecov"
|
31
32
|
spec.add_development_dependency "dotenv"
|
32
33
|
spec.add_development_dependency "pry"
|
33
34
|
spec.add_development_dependency "rake"
|
34
35
|
spec.add_development_dependency "rspec_junit_formatter"
|
35
36
|
spec.add_development_dependency "rspec-with_params"
|
36
37
|
spec.add_development_dependency "rspec", "~> 3.8"
|
38
|
+
spec.add_development_dependency "simplecov"
|
39
|
+
spec.add_development_dependency "vcr_better_binary"
|
37
40
|
spec.add_development_dependency "vcr"
|
38
41
|
spec.add_development_dependency "webmock"
|
42
|
+
spec.add_development_dependency "yard"
|
39
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remove_bg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oliver Peate
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -84,6 +84,20 @@ dependencies:
|
|
84
84
|
- - "~>"
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '2.0'
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: codecov
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
type: :development
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
87
101
|
- !ruby/object:Gem::Dependency
|
88
102
|
name: dotenv
|
89
103
|
requirement: !ruby/object:Gem::Requirement
|
@@ -168,6 +182,34 @@ dependencies:
|
|
168
182
|
- - "~>"
|
169
183
|
- !ruby/object:Gem::Version
|
170
184
|
version: '3.8'
|
185
|
+
- !ruby/object:Gem::Dependency
|
186
|
+
name: simplecov
|
187
|
+
requirement: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - ">="
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
type: :development
|
193
|
+
prerelease: false
|
194
|
+
version_requirements: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
199
|
+
- !ruby/object:Gem::Dependency
|
200
|
+
name: vcr_better_binary
|
201
|
+
requirement: !ruby/object:Gem::Requirement
|
202
|
+
requirements:
|
203
|
+
- - ">="
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
type: :development
|
207
|
+
prerelease: false
|
208
|
+
version_requirements: !ruby/object:Gem::Requirement
|
209
|
+
requirements:
|
210
|
+
- - ">="
|
211
|
+
- !ruby/object:Gem::Version
|
212
|
+
version: '0'
|
171
213
|
- !ruby/object:Gem::Dependency
|
172
214
|
name: vcr
|
173
215
|
requirement: !ruby/object:Gem::Requirement
|
@@ -196,6 +238,20 @@ dependencies:
|
|
196
238
|
- - ">="
|
197
239
|
- !ruby/object:Gem::Version
|
198
240
|
version: '0'
|
241
|
+
- !ruby/object:Gem::Dependency
|
242
|
+
name: yard
|
243
|
+
requirement: !ruby/object:Gem::Requirement
|
244
|
+
requirements:
|
245
|
+
- - ">="
|
246
|
+
- !ruby/object:Gem::Version
|
247
|
+
version: '0'
|
248
|
+
type: :development
|
249
|
+
prerelease: false
|
250
|
+
version_requirements: !ruby/object:Gem::Requirement
|
251
|
+
requirements:
|
252
|
+
- - ">="
|
253
|
+
- !ruby/object:Gem::Version
|
254
|
+
version: '0'
|
199
255
|
description:
|
200
256
|
email:
|
201
257
|
- team@remove.bg
|