Bläddra i källkod

Image merge command for matchup

Neiro 6 år sedan
förälder
incheckning
1b4786755b
5 ändrade filer med 97 tillägg och 6 borttagningar
  1. 1 0
      Gemfile
  2. 2 0
      Gemfile.lock
  3. 57 0
      app/models/image_merger.rb
  4. 37 6
      bot.rb
  5. BIN
      images/Type Double.png

+ 1 - 0
Gemfile

@@ -9,6 +9,7 @@ gem 'activerecord'
 gem 'discordrb', '~> 3.3.0'
 gem 'pg'
 gem 'terminal-table'
+gem 'rmagick'
 
 group :development do
   gem 'dotenv'

+ 2 - 0
Gemfile.lock

@@ -52,6 +52,7 @@ GEM
       http-cookie (>= 1.0.2, < 2.0)
       mime-types (>= 1.16, < 4.0)
       netrc (~> 0.8)
+    rmagick (4.0.0)
     terminal-table (1.8.0)
       unicode-display_width (~> 1.1, >= 1.1.1)
     thread_safe (0.3.6)
@@ -76,6 +77,7 @@ DEPENDENCIES
   dotenv
   pg
   pry
+  rmagick
   terminal-table
 
 RUBY VERSION

+ 57 - 0
app/models/image_merger.rb

@@ -0,0 +1,57 @@
+require 'rmagick'
+include Magick
+
+def append_image(image1_in, images2_in, image_out)
+  i = Magick::ImageList.new
+  i = Magick::ImageList.new(image1_in,images2_in)
+
+  u = i.append(true);
+  u.write('images/Type Double.png');
+end
+
+def crop_image()
+  images = ['images/Type Bug.png', 'images/Type Dark.png', 'images/Type Dark - Copy.png']
+
+  img = Magick::Image.read(images[0]).first # path of Orignal image that has to be worked upon
+  puts img.inspect
+
+  def try(x,y,width,height)
+    images = ['images/Type Bug.png', 'images/Type Dark.png', 'images/Type Dark - Copy.png']
+
+    # converting x,y , width and height to integer values in next four statements
+    x= x.to_i
+    y= y.to_i
+    width= width.to_i
+    height= height.to_i
+
+    # Demonstrate the Image#crop method
+    @st = images[0] # path of image written after changing size or not changing also
+    img = Magick::Image.read(@st)[0]
+
+    # Crop the specified rectangle out of the img.
+    chopped = img.crop(x, y, width,height)
+
+    # Go back to the original and highlight the area
+    # corresponding to the retained rectangle.
+    rect = Magick::Draw.new
+    rect.stroke('transparent')
+    rect.fill('black')
+    rect.fill_opacity(1.0)
+    rect.rectangle(x, y, 100+x, 10+y)
+    rect.draw(img)
+
+    img.write(images[1]) #path of image written before cropping
+
+    # Create a image to use as a background for
+    # the “after” image.
+    bg = Magick::Image.new(img.columns, img.rows)
+
+    # Composite the the “after” (chopped) image on the background
+    bg = bg.composite(chopped, 38,81, Magick::OverCompositeOp)
+
+    bg.write(images[2]) # path of image written after cropping the desired part
+    exit
+  end
+
+  try(100, 50, 150, 100)
+end

+ 37 - 6
bot.rb

@@ -3,6 +3,7 @@ require 'erb'
 require 'yaml'
 require 'json'
 require 'terminal-table'
+require 'rmagick'
 
 BOT_ENV = ENV.fetch('BOT_ENV') { 'development' }
 Bundler.require(:default, BOT_ENV)
@@ -97,17 +98,38 @@ help = Command.new(:help, desc, opts) do |event, command|
   end
 end
 
-opts = { "type" => "" }
+opts = { 
+  "primary" => "Looks up a primary type",
+  "primary | secondary" => "Looks up a primary and secondary type"}
 desc = "Displays a chart of effectiveness for the given type"
-matchup = Command.new(:matchup, desc, opts) do |event, type|
+matchup = Command.new(:matchup, desc, opts) do |event, primary, secondary|
   channel = event.channel.id
-  file = "images/Type #{type.capitalize}.png"
 
-  if File.exists?(file)
+  primary_file = 'nil'
+  secondary_file = 'nil'
+  image_out = 'images/Type Double.png';
+
+  if secondary
+    primary_file = "images/Type #{primary.capitalize}.png"
+    secondary_file = "images/Type #{secondary.capitalize}.png"
+  elsif primary
+    primary_file = "images/Type #{primary.capitalize}.png"
+  else
+    command_error_embed("There was an error processing your type matchup!", matchup)
+  end
+
+  if File.exists?(primary_file) and File.exists?(secondary_file)  
+    append_image(primary_file, secondary_file, image_out)
+    file = image_out
     bot.send_file(channel, File.open(file, 'r'))
+  elsif File.exists?(primary_file) and secondary_file != 'nil' 
+    command_error_embed("There was an error processing your type matchup!", matchup)
+  elsif File.exists?(primary_file)
+    bot.send_file(channel, File.open(primary_file, 'r'))
   else
-    bot.respond("I do not know this pokemon type! Please try again!")
+    command_error_embed("There was an error processing your type matchup!", matchup)
   end
+
 end
 
 opts = {
@@ -385,6 +407,14 @@ rescue ActiveRecord::RecordNotFound => e
   error_embed("Record Not Found!", e.message)
 end
 
+opts = { "Test" => ""}
+desc = "Fucked if I know"
+merge = Command.new(:merge, desc, opts) do |event|
+  append_image("images/Type Bug.png", "images/Type Dark.png")
+  #merge_image()
+
+end
+
 # ---
 
 commands = [
@@ -394,7 +424,8 @@ commands = [
   help,
   poll,
   raffle,
-  member
+  member,
+  merge
 ]
 
 # This will trigger on every message sent in discord

BIN
images/Type Double.png