Class: CuteRB::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(text, image, output) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cuterb.rb', line 11

def initialize(text, image, output)
  @qr = RQRCode::QRCode.new(text, :level => :h)
  @qr = @qr.qrcode if @qr.respond_to?(:qrcode)

  @image = Magick::ImageList.new(image).first
  short = [@image.columns, @image.rows].min
  raise "#{image} too small." if short < @qr.module_count

  # crop image
  if @image.columns != @image.rows
    begin
      tmp = Tempfile.new(['cuterb-canny', '.png'])
      system("convert #{image} -canny 0x1+10%+30% #{tmp.path}")
      raise 'convert error' unless $?.success?
      canny = Magick::ImageList.new(tmp.path).first
      start = Utils.contents_area(canny)
    rescue RuntimeError, Magick::ImageMagickError, Magick::FatalImageMagickError, Magick::DestroyedImageError => e
      start = 0
    end

    if @image.columns == short
      @image = @image.crop(0, start, short, start+short)
    else
      @image = @image.crop(start, 0, start+short, short)
    end
  end
  @image = @image.quantize(256, Magick::GRAYColorspace)

  bg = Magick::Image.new(@image.columns, @image.columns) { self.background_color = "white" }
  @image = bg.composite(@image, 0, 0, Magick::OverCompositeOp).normalize.contrast(true).contrast(true)
  @output = output
end

Instance Method Details

#runObject



44
45
46
47
48
49
# File 'lib/cuterb.rb', line 44

def run()
  pixel = qrpixel()
  overlay(pixel)
  @image.write(@output)
  return 0
end