Created
December 13, 2011 09:56
-
-
Save melborne/1471460 to your computer and use it in GitHub Desktop.
get resized image pixel data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "RMagick" | |
| include Magick | |
| class Integer | |
| def to_hex | |
| self.to_s(16)[/^../] | |
| end | |
| end | |
| class ImageUtil | |
| def initialize(path) | |
| @image = | |
| case path | |
| when Magick::Image; path | |
| else ImageList.new(path) | |
| end | |
| @col, @row = @image.columns, @image.rows | |
| end | |
| def resize(scale) | |
| img = @image.resize(@col*scale, @row*scale) | |
| ImageUtil.new(img) | |
| end | |
| # get pixels pos-x, pos-y and rgba info except base_color pixels | |
| def pixels(opt={}) | |
| opt = {:reject_color => "#ffffff0"}.merge(opt) | |
| pixels = @image.get_pixels(0,0,@col,@row) | |
| pixels.map.with_index { |p, i| | |
| x, y = i.divmod(@col) | |
| r, g, b, a = p.red.to_hex, p.green.to_hex, p.blue.to_hex, p.opacity | |
| { x:x, y:y, color:"##{r}#{g}#{b}#{a}" } | |
| } | |
| .reject { |h| h[:color] == opt[:reject_color] } | |
| end | |
| def save(path='out.jpg') | |
| @image.write(path) | |
| end | |
| end | |
| if __FILE__ == $0 | |
| img = ImageUtil.new("zelda.jpeg") | |
| p img.resize(0.25).pixels | |
| # img.resize(0.25).save('abc.jpg') | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment