Class: Falu::Image
- Inherits:
-
Object
- Object
- Falu::Image
- Defined in:
- lib/falu/image.rb
Instance Method Summary collapse
- #dither(colors = 3, filename: nil, scale: nil, unique: false) ⇒ Object
- #histogram ⇒ Object
- #image ⇒ Object
-
#initialize(image) ⇒ Image
constructor
A new instance of Image.
- #miro ⇒ Object
- #pixels(x = 0, y = 0, w = nil, h = nil, size: 10, sample: nil, &block) ⇒ Object
- #sample(x = 0, y = 0, w = nil, h = nil, size: 10, sample: false, &block) ⇒ Object
- #scale(scale, filename: nil) ⇒ Object
Constructor Details
#initialize(image) ⇒ Image
Returns a new instance of Image.
17 18 19 20 |
# File 'lib/falu/image.rb', line 17 def initialize(image) @raw_image = image @image = image end |
Instance Method Details
#dither(colors = 3, filename: nil, scale: nil, unique: false) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/falu/image.rb', line 114 def dither(colors=3, filename: nil, scale: nil, unique: false) filename ||= Tempfile.new(['dithered', '.png']).path scale ||= unique ? (colors * 10) : [width, height].max args = [ 'convert', path, '+dither', '-colors', colors, (unique ? '-unique-colors' : nil), '-scale', scale, filename ].compact run_command(*args) self.class.new(filename) end |
#histogram ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/falu/image.rb', line 27 def histogram #hist = run_command("convert", "#{path}", "-format", "%c", "histogram:info:").split("\n") #return hist.first run_command("convert", "#{path}", "-format", "%c", "histogram:info:").split("\n").map do |clr| next unless match = clr.match(/\s*(\d+):\s+\((\d+,\d+,\d+)\)\s+(#[\da-fA-F]{6})\s+(.*)/) color = { count: match[1].to_i, rgb: match[2], hex: match[3].downcase } if block_given? yield(*(color.values + [color])) else color end end.compact.to_enum end |
#image ⇒ Object
22 23 24 25 |
# File 'lib/falu/image.rb', line 22 def image @image = MiniMagick::Image.open(@image) unless @image.is_a?(MiniMagick::Image) @image end |
#miro ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/falu/image.rb', line 92 def miro colors = Miro::DominantColors.new(@raw_image) hex = colors.to_hex pct = colors.by_percentage hex.each_with_index.map do |clr,idx| [clr, pct[idx] * 100] end end |
#pixels(x = 0, y = 0, w = nil, h = nil, size: 10, sample: nil, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/falu/image.rb', line 46 def pixels(x=0, y=0, w=nil, h=nil, size: 10, sample: nil, &block) w ||= width h ||= height (w / size).times.map do |ww| pw = (ww * size) px = x + pw (h / size).times.map do |hh| ph = (hh * size) py = y + ph pixels = run_command("convert", "#{path}[#{size}x#{size}+#{px}+#{py}]", "-depth", '8', "txt:").split("\n") sample = 1 if sample == true pixels = pixels.sample(sample) if sample pixels.map do |pxl| next unless match = pxl.match(/(\d+),(\d+):\s+\((\d+,\d+,\d+)\)\s+(#[\da-fA-F]{6})\s+(.*)/) pixel = { x: match[1].to_i + pw, y: match[2].to_i + ph, rgb: match[3], hex: match[4].downcase, } if block_given? yield(*(pixel.values + [pixel])) else pixel end end end end.flatten.to_enum end |
#sample(x = 0, y = 0, w = nil, h = nil, size: 10, sample: false, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/falu/image.rb', line 82 def sample(x=0, y=0, w=nil, h=nil, size: 10, sample: false, &block) palette = {} pixels(x, y, w, h, size: size, sample: sample) do |x,y,rgb,hex| palette[hex] ||= 0 palette[hex] += 1 yield(hex) if block_given? end palette.to_a.to_enum end |
#scale(scale, filename: nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/falu/image.rb', line 101 def scale(scale, filename: nil) filename ||= Tempfile.new(['scaled', '.png']).path args = [ 'convert', path, '-scale', scale, filename ] run_command(*args) self.class.new(filename) end |