Tag: RMagick
def concat_images(*images) # 画像を結合して出力 images_files = images[0..images.length - 2]; file_name = images.last image_final = Magick::ImageList.new(*images_files) result = image_final.montage result.write file_name end
def append_images(*images) # 画像を結合し間に空白を追加して出力 images_files = images[0..images.length - 2]; file_name = images.last image_final = Magick::ImageList.new(*images_files) result = image_final.append(false) # 結合方向 width = result.columns height = result.rows puts width puts height # 間にスペースを追加する padding = 10 if height > 1000 padding = 20 end result = result.splice(width / 2, 0, padding, 0) # 画像を10字に切るメソッドみたい # 結果をファイルに書き出す result.write file_name end
def append_images(*images) #結合元の画像ファイル名 image_files = images[0..images.length - 2]; #出力画像名 file_name = images.last #最終的な画像リスト result_image_list = Magick::ImageList.new image_files.each do |image_file| #background_colorとflatten_imagesを組み合わせて背景色を指定 image_list = Magick::ImageList.new(image_file) {self.background_color = 'white'} image = image_list.flatten_images result_image_list << image end #結果を結合 result = result_image_list.append(false) result.write file_name end append_images(*ARGV)
require "rmagick" module Converter def self.resize_and_pad(img, new_img_path, width, height, background=:transparent, gravity=::Magick::CenterGravity) img.resize_to_fit!(width, height) new_img = ::Magick::Image.new(width, height) if background == :transparent filled = new_img.matte_floodfill(1, 1) else filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background)) end # destroy_image(new_img) filled.composite!(img, gravity, ::Magick::OverCompositeOp) # destroy_image(img) # filled = yield(filled) if block_given? # filled filled.write new_img_path end end srcfile = ARGV[0] dstfile = ARGV[1] puts "srcfile=#{srcfile} dstfile=#{dstfile}" img = Magick::Image.read(srcfile).first #img = img.resize_and_pad(200, 100, 'red', Magick::CenterGravity) #img.write(dstfile) Converter::resize_and_pad(img, dstfile, 200, 100, 'red', Magick::CenterGravity)
$ brew uninstall imagemagick $ brew install imagemagick@6 $ PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig gem install rmagick
$ PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig gem install rmagick
/Users/sora/.anyenv/envs/rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': dlopen(/Users/sora/.anyenv/envs/rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/lib/RMagick2.bundle, 9): Library not loaded: /usr/local/opt/imagemagick@6/lib/libMagickWand-6.Q16.4.dylib (LoadError)
gem uninstall rmagick PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig gem install rmagick