Errors using the caption method in MiniMagick gem - ruby-on-rails

I'm trying to build a rails app that generates an image by overlaying a text over a default image.
I'm almost there - by using carrierwave + mini magick i have this method working :
def generate_image
message = self.description.upcase
image = MiniMagick::Image.open('public/base.jpg')
image.combine_options do |c|
c.size '400x500'
c.gravity 'NorthWest'
c.fill 'black'
c.strokewidth '2'
c.pointsize '48'
c.interline_spacing '-9'
c.font "#{Rails.root}/public/black.ttf"
c.draw "text 40,40 '#{message}'"
c.antialias
end
self.image = image
end
the problem is that i want my text to fit the size i've specified - but I had no luck !
reading the Image Magick documentation I know that I should use the "capition" method instead of draw - but when I do so it throws out an orrible error :) :
failed with error: mogrify: no encode delegate for this image format `CAPTION' #
error/constitute.c/WriteImage/1167.
any clues ?
thanks !

Related

Blur images with Shrine and image_processing gem

I am trying to blur images in my Ruby on Rails application, using the Shrine gem. This is my uploader file:
require "image_processing/mini_magick"
class ImageUploader < Shrine
Attacher.derivatives_processor do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
blurred: magick.append('-blur 0x8').resize_to_limit!(1024, 1024)
}
end
end
I set up my model, controller and form in the most basic way, the same as in the Shrine Getting Started tutorial - https://shrinerb.com/docs/getting-started.
When I try to save an image I get following error:
*** MiniMagick::Error Exception: convert /tmp/shrine20191112-4479-1xo3vgk.jpg -auto-orient -blur 0x5 -resize 1024x1024> -sharpen 0x1 /tmp/image_processing20191112-4479-1w094sa.jpg failed with error:
convert: unrecognized option `-blur 0x5' # error/convert.c/ConvertImageCommand/893.
"
Without the append('-blur 0x8') it works just fine, what am I doing wrong? My ImageMagick version is 7.0.7-11.
Btw I wouldn't mind blurring the image with libvips, I just have more experience with ImageMagick so that's what I went with.
You need to specify each command-line argument separately, in this case -blur and 0x8:
magick.append('-blur', '0x8').resize_to_limit!(1024, 1024)
You can also call the #blur method, which will get applied as -blur through the magic of method_missing:
magick.blur('0x8').resize_to_limit!(1024, 1024)
I think the libvips equivalent would be:
require "image_processing/vips"
class ImageUploader < Shrine
Attacher.derivatives_processor do |original|
vips = ImageProcessing::Vips.source(original)
{
blurred: vips.resize_to_limit(1024, 1024).gaussblur(2).call
}
end
end
Since unknown methods are simply delegated to ruby-vips.
If you can, put the resize first, it's a lot quicker. You'll get more consistent results too, since the degree of blur won't depend on how large the resize is.

MiniMagick Gem: How do I create a new blank image without a file?

I want to combine two images (one background image, one text image) into one big image.
I believe I have the background image done since it's just based off a file. However, I'm having trouble trying to create an image from scratch. Any tips?
image = MiniMagick::Image.open("public/text_response_bg.png")
image.combine_options do |i|
i.size "1024x512"
end
text = MiniMagick::Image.new #<-- does not work
text.combine_options do |i|
i.size "700x200"
i.gravity 'center'
i.fill 'white'
i.caption 'blahblahblah'
end
result = image.composite(text) do |c|
c.compose "Over"
c.geometry "+20+20"
end
Create an image from scratch with the following Ruby Code:
MiniMagick::Tool::Convert.new do |i|
i.size "700x200"
i.gravity "center"
i.xc "white"
i.caption "blablabla"
i << "test_image.jpg"
end
MiniMagick provide a method MiniMagick::Image.create to create a new image but seems not work with this issues
Use ImageMagick primary command you can create a pure color image like
convert -size 800x600 xc:"#ffffff" write.jpg
So if not mind using system command to create image you can do:
cmd = "convert -size 800x600 xc:'#ffffff' WRITE_IMAGE.jpg"
system(cmd)
UPDATE: I use MiniMagick 3.8.0, and see latest version 4.0.1 has a MiniMagick::Shell class, think it could run that custom ImageMagick command directly.

Photo Failed to manipulate with MiniMagick, maybe it is not an image? error

I have the following code, which resizes and image and then adds some hex background. On localhost this works, but on heroku i get the following error
version :big do
process :offer_resize_and_pad
end
def offer_resize_and_pad
img = resize_and_pad(600, nil, model.hex, 'Center')
img
end
"Photo Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: Command (\"mogrify -thumbnail \"600x>\" -background \"#fff\" -gravity \"Center\" -extent \"600x\" /tmp/mini_magick20130217-2-1hts61y.jpg\") failed: {:status_code=>1, :output=>\"mogrify: Empty JPEG image (DNL not supported)/tmp/mini_magick20130217-2-1hts61y.jpg' # jpeg.c/EmitMessage/232.\n\"
I 've used RMagick instead of Mini_magick, with some syntax changes , and it seems to work :) .
include CarrierWave::RMagick
def offer_resize_and_pad
img = resize_and_pad(600, 280, model.hex, ::Magick::CenterGravity)
img
end

Insert subimage into another image with MiniMagick

I'm looking to write the Carrierwave process to add a subimage into another image with MiniMagick, any ideas ?
This doc https://github.com/probablycorey/mini_magick say we can use all the mogrify commandline options of ImageMagick, but don't I need composite to do it ?
Thank you.
I have to open my eyes, this is written in the MiniMagick doc:
image = MiniMagick::Image.open("original.png")
result = image.composite(MiniMagick::Image.open("watermark.png", "jpg")) do |c|
c.gravity "center"
end
result.write "my_output_file.jpg"

MiniMagick Image.create method giving an ArgumentError

I'm creating a collage of thumbnails using Ruby 1.9.3. The thumbnails are being loaded as follows:
image1 = MiniMagick::Image.open("1.jpg")
image2 = MiniMagick::Image.open("2.jpg")
image2.rotate "-45>"
image3 = MiniMagick::Image.open("3.jpg")
image3.rotate "45>"
I've never used ImageMagick or MiniMagick before and I've got the code for compositing images from the minimagick GitHub page.
collage = MiniMagick::Image.create "jpg", false do |c|
c.size "1024x768"
c.canvas "white"
end
collage = collage.composite image1 do |c|
c.gravity "center"
end
collage = collage.composite image2 do |c|
c.gravity "east"
end
collage = collage.composite image3 do |c|
c.gravity "west"
end
collage.write("output.jpg")
The problem is coming up on the Image.create command. When I run the file using
$ ruby prog.rb
I get the following error
/Users/vinayshenoy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/tempfile.rb:250:in `size': wrong number of arguments (1 for 0) (ArgumentError)
from prog.rb:14:in `block in <main>'
from /Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/mini_magick-3.4/lib/mini_magick.rb:158:in `call'
from /Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/mini_magick-3.4/lib/mini_magick.rb:158:in `create'
from prog.rb:13:in `<main>'
The script file is the same directory as the images. I tested by writing image1, image2 and image3 to separate files and they all work. Please help.
The error message is a bit strange, but according to the documentation at http://www.imagemagick.org/script/command-line-options.php?#composite composite only takes 3 arguments - the two images you want to combine (the image you create in line 1 and the one called image 1), plus a gray scale 'mask' (image2 in this case).
In other words, it seems like you'll have to save after every single one of the three compositions.

Resources