daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Mar 18th, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'RMagick'
  2. require 'aws-sdk'
  3. include Magick
  4.  
  5. class MosaicImage
  6.  
  7.     def perform
  8.         canvas = mosaic()
  9.  
  10.         # Resize
  11.         width = 800
  12.         height = 1200
  13.         canvas = canvas.resize_to_fit(width,height)
  14.  
  15.         box(canvas)
  16.         text(canvas)
  17.  
  18.         canvas
  19.     end
  20.  
  21.     private
  22.  
  23.     def box(canvas)
  24.         w = canvas.columns
  25.         h = canvas.rows
  26.  
  27.         gc = Magick::Draw.new
  28.  
  29.         x1 = (w/2) - (w/4)
  30.         y1 = (h/2) - (h * 0.05)
  31.         x2 = x1 + (w/2)
  32.         y2 = y1 + (h * 0.05)
  33.  
  34.         gc.stroke_width(1)
  35.         gc.stroke('gray50')
  36.         gc.fill('white')
  37.  
  38.         gc.rectangle(x1, y1, x2, y2)
  39.         gc.draw(canvas)
  40.     end
  41.  
  42.     def text(canvas)
  43.         w = canvas.columns
  44.         h = canvas.rows
  45.         # puts(h)
  46.         text_label = "SEE THE OUTFITS"
  47.  
  48.         pt = h / 30
  49.         # puts(pt)
  50.  
  51.         gc = Magick::Draw.new
  52.         gc.stroke('transparent')
  53.         gc.font_family('Helvetica')
  54.  
  55.         gc.font_weight(Magick::NormalWeight)
  56.         gc.pointsize(pt)
  57.  
  58.         gc.font_style(Magick::NormalStyle)
  59.         gc.text_align(Magick::CenterAlign)
  60.  
  61.         metrics = gc.get_type_metrics(text_label)
  62.         # puts(metrics)
  63.         # gc.text(w/2, h/2 + ((metrics.pixels_per_em.y / (metrics.bounds.y2 - metrics.bounds.y1) ) / 2).round, text_label)
  64.         gc.text(w/2, h/2 - ((metrics.max_advance + metrics.descent) /2), text_label)
  65.  
  66.         gc.draw(canvas)
  67.     end
  68.  
  69.     def mosaic()
  70.         gc = Magick::ImageList.new
  71.  
  72.         img = 1
  73.         5.times do
  74.             gc.read("img"+img.to_s+".jpg")
  75.             img += 1
  76.         end
  77.  
  78.         b = Magick::ImageList.new
  79.         page = Magick::Rectangle.new(0,0,0,0)
  80.         gc.scene = 0
  81.         3.times do |i|
  82.             3.times do |j|
  83.                 b << gc.scale(1)
  84.                 page.x = j * b.columns
  85.                 page.y = i * b.rows
  86.                 b.page = page
  87.                 (gc.scene += 1) rescue gc.scene = 0
  88.             end
  89.         end
  90.  
  91.         b.mosaic
  92.     end
  93.  
  94. end
  95.  
  96. # Generate image and write to s3
  97. canvas = MosaicImage.new().perform
  98. s3 = Aws::S3::Resource.new
  99. bucket = s3.bucket('my-bucket')
  100. bucket.object('test/canvas.png').put(body: canvas.to_blob)
  101.  
  102. # write locally as well
  103. canvas.write("canvas.png")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top