Skip to content

Commit

Permalink
Merge pull request #4 from mtking2/inversion
Browse files Browse the repository at this point in the history
Inversion Support
  • Loading branch information
mtking2 authored Oct 31, 2019
2 parents 6af9407 + 2c7f9d2 commit 79bf758
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
### v0.x.x (next)
### v1.0.x (next)

* Your contribution here.
* Your contribution here

### v1.0.0 (2019/10/31)

* [#4](https://github.com/mtking2/img2zpl/pull/4): Add ability to invert images - [@mtking2](https://github.com/mtking2).

### v0.1.3 (2019/10/16)

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ img = Img2Zpl::Image.open('foo.jpg')
zpl = img.to_zpl #=> "^GFA, ... ^FS"
```

#### Using ImageMagick commands

The `Img2Zpl::Image` class inherits from the `MiniMagick::Image` class provied by the [minimagick](https://github.com/minimagick/minimagick) gem. So you have the same control when it comes to modifying the image before converting to ZPL.

**Example**:
```ruby
img = Img2Zpl::Image.open('foo.jpg')

img.resize '100x100'
img.trim

zpl = img.to_zpl
```

#### Options

When calling the `.to_zpl` method there a number of optional parameters you can pass to further customize the resulting image:

- `black_threshold`: A value between 0 and 1 that sets the darkness threshold which determines how dark a pixel should be in order to become black in the resulting b/w image. Use larger value for a more saturated image and smaller value for a less saturated one. Default: `0.5`
- `invert`: set to `true` to invert which pixels are set to black and which are set to white.
- `compress`: set to `false` to not perform the ACSII compression in the resulting `^GF` string. For larger images the uncompressed string can become **very** long, so use caution.

**Example**:
```ruby
zpl = img.to_zpl black_threshold: 0.65, invert: true
```

### Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
5 changes: 3 additions & 2 deletions lib/img2zpl/image.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Img2Zpl
class Image < MiniMagick::Image

def to_zpl(black_threshold: 0.5, compress: true)
def to_zpl(black_threshold: 0.5, invert: false, compress: true)
bytes_per_row = (width % 8).positive? ? (width / 8) + 1 : (width / 8)
byte_count = bytes_per_row * height
data, line, previous_line, byte = '', '', '', ''

get_pixels.each do |row|
row.each_with_index do |column, i|
r, g, b = column.map(&:to_i)
byte << ((r + g + b) > (black_threshold * 765) ? '0' : '1')
b_dot, w_dot = invert ? %w(1 0) : %w(0 1)
byte << ((r + g + b) > (black_threshold * 765) ? b_dot : w_dot)
if (i % 8).zero?
line << byte.to_i(2).to_s(16).upcase.rjust(2, '0')
byte = ''
Expand Down
2 changes: 1 addition & 1 deletion lib/img2zpl/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Img2Zpl
VERSION = '0.1.3'
VERSION = '1.0.0'
end

0 comments on commit 79bf758

Please sign in to comment.