Class: Falu::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/falu/image.rb

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ Image

Returns a new instance of Image.



17
18
19
# File 'lib/falu/image.rb', line 17

def initialize(image)
  @image = image
end

Instance Method Details

#dither(colors = 3, filename: nil, scale: nil, unique: false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/falu/image.rb', line 104

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

#histogramObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/falu/image.rb', line 26

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

#imageObject



21
22
23
24
# File 'lib/falu/image.rb', line 21

def image
  @image = MiniMagick::Image.open(@image) unless @image.is_a?(MiniMagick::Image)
  @image
end

#pixels(x = 0, y = 0, w = nil, h = nil, size: 10, sample: nil, &block) ⇒ Object



45
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
# File 'lib/falu/image.rb', line 45

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



81
82
83
84
85
86
87
88
89
# File 'lib/falu/image.rb', line 81

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/falu/image.rb', line 91

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