MiniMagick wrap text on image - ruby-on-rails

On Rails 5.2.1, I am attempting to use MiniMagick to write text on an image. The problem is, it overflows beyond the bounds of the image's width for long texts.
I have attempted to use draw, label, annotation and caption methods from here but none give me the right result. caption does not even add the text to the image.
This is my code:
temp_img = MiniMagick::Image.open(url_for(#post.image))
img_width = temp_img[:width]
temp_img.combine_options do |c|
c.gravity 'North'
c.draw "text 0, 0 '#{top_txt}'"
#c.annotate '0,0', "'#{top_txt}'" (same result)
#c.caption "'#{top_txt}'" (same result)
#c.label "'#{top_txt}'" (same result)
c.gravity 'South'
c.draw "text 0, 0 '#{bot_txt}'"
#c.annotate '0,0', "'#{bot_txt}'" (same result)
#c.caption "'#{bot_txt}'" (same result)
#c.label "'#{bot_txt}'" (same result)
c.stroke('#000000')
c.strokewidth 1
c.fill('#FFFFFF')
c.size "#{img_width}x"
c.pointsize '40'
c.font "#{Rails.root.join('public', 'font',
'Franklin_Gothic_Heavy_Regular.ttf')}"
end
This is my result:
The closest I have seen to a solution is this but is too messy.
Could there be a better way?

Sorry, I do not know Minimagick. But in ImageMagick command line there is label: that automatically fits text to some dimension such as the image width. See https://imagemagick.org/Usage/text/#label. If MiniMagick does not support that directly, then perhaps you could use Ruby/Rmagick commands. See https://github.com/minimagick/minimagick (methods section), assuming RMagick supports a label: like method. Note that with label: and caption: you have to create a new image with with your text on a transparent background and then composite it over your original image.
Here is an example:
Input:
convert image.png \( -size 639x -background none -font Arial -fill black label:"The quick brown fox jumps over the lazy dog" \) -gravity center -compose over -composite result.png

You need to use Convert with MiniMagick and the syntax is a little tricky because there isn't many examples with Ruby for it.
caption_string = "caption: #{top_txt}"
MiniMagick::Tool::Convert.new do |img|
img.gravity 'north'
img.stroke '#000000'
img.strokewidth 1
img.fill '#FFFFFF'
img.size "#{img_width}x"
img.pointsize '40' #don't add this if you want it to adjust according to size
img.font "#{Rails.root.join('public', 'font',
'Franklin_Gothic_Heavy_Regular.ttf')}"
img << caption_string
img << url_for(#post.image)
end

Related

Imagemagick how to apply gravity and extent to only short images

I have a web page where I list images that should be 600px by 600px (width & height). So I resize all big images to 600x600 using imagemagick, which works fine.
However, some images, when I resize them (in order to keep their aspect ratio), get resized to a size smaller than 600px in height (for example 600x450). So I tried to fill the needed height by using imagemagick options (gravity, background, and extent) like so :
image.combine_options do |img|
img.crop('1200x1200+0+0')
img.resize '600x600'
img.background 'white'
img.extent '600x600'
img.gravity 'center'
end
That gave me what I want for these kind of images (short images). However, this effect of centering is applied to the other images (bigger ones) as well ! Is there any way to prevent that ?
Update:
Just to show you what I mean, I made up this example... if I don't add extent and gravity the image will start from the top left corner
but if I add extent and gravity (center) the image will start from the center, something like this :
I want only images that result on resized images smaller than 600x600 to be centered, but in the example as you see even an image that get resized to exact 600x600 get centered !! that's not what I want
the solution :
I end up using shell command using system function in ruby
system "convert #{image.path} -crop 1024x1024+0+0 -resize 600x600 -background white -gravity center -extent 600x600 output.png"
and that worked fine! I don't know why minimagick wasn't working correctly, but I just get rid of that gem from my gemfile which is fine too.
Here is your command in command line Imagemagick with proper resetting of the virtual canvas after the crop.
convert image -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result
Here are two results with slightly different resize arguments. It looks like your commands are using the equivalent of the ^ flag set on the resize argument.
Input:
Proper result without the ^ flag: (Note padded with white)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result1.png
Result with the ^ flag: (Note cropped)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600^ -background white -gravity center -extent 600x600 result2.png
The above is Unix syntax. For Windows double the ^ to ^^, since ^ is an escape character in Windows.
Perhaps your issue is with minimagick and not Imagemagick. You can check by testing my command line.

Cropping transparent pixels with carrierwave

Using Carrierwave on Ruby 5 with MiniMagick, is it possible to trim transparent pixels ?
Suppose a user uploads a 500x500 image but only the inner 250x250 pixels are indeed filled, the rest is transparent. Is there a processing command that would help detect and trim the image to 250x250 before additional processing ?
I found https://www.imagemagick.org/discourse-server/viewtopic.php?t=12127 and it seems there is a trim transparent command on Imagemagick but I'm not sure how to use it with the Ruby wrapper Minimagick ?
The MiniMagick::Image.trim is all that's needed. Without a pixel-iterator, it would be simplest to apply trim on a clone image, and act on the smallest result.
require 'mini_magick'
def trimed_image(path)
image = MiniMagick::Image.open(path)
test_image = image.clone
test_image.trim
if test_image.width < image.width || test_image.height < image.height
test_image
else
image
end
end
Test case with convert rose: -resize x100 rose.png
rose = trimed_image("rose.png")
rose.write("rose_output.png")
No change expected.
Test transparent image with convert -size 100x100 gradient: -background black -extent 200x200-50-50 -alpha copy trim.png
trim = trimed_image("trim.png")
trim.write("trim_output.png")
Trim expected.

How can I overlay a larger watermark/frame image on a smaller one?

I have an overlay image, which is like a watermark/logo, which needs to be overlayed on top of the source image (while preserving alpha channel, etc)
When overlay is the same or smaller dimension as the source image - things are easy:
composite.exe -alpha on -gravity center logo.png in_image.jpg out_image.jpg
However, when logo.png is larger than in_image.jpg - above call truncates the logo, and out_image.jpg has the same dimensions as in_image.jpg
I would like the resulting image to be the largest of either the logo.png or in_image.jpg so I can do things like artistic frames around the photos.
Below image demonstrates the end result I want to be able to get this:
Desired Result
Note, here, the png with skulls has larger dims than the kiddo's image. The alpha channel needs to be preserved.
Edit: more clarity through examples
Here is another desired result
Here, the png file is opaque on the sides, has a clear window in the middle, and half-translucent bubbles. the JPG file is just a regular JPG from a camera.
Would love to add original and logo files that result in it, but lack reputation to add more than 2 links (or to add images)
Updated Answer
If you have v7 of ImageMagick, you can get it to do the maths for you all in one line using -fx to determine the dimensions of the larger of the two images:
magick background.jpg overlay.png -background none -gravity center -extent '%[fx:u.w>v.w?u.w:v.w]x%[fx:u.h>v.h?u.h:v.h]' -composite result.png
That basically says... "Extend the two images as follows. If the width of the first image (u.w) is greater than that of the second image (v.w), then use the width of the first, else use that of the second. Same for height.".
Original Answer
I believe you want this. Get width of whatever is wider of background and overlay. Get height of whatever is taller of background and overlay. Extend both background and overlay with transparent pixels to new dimensions. Overlay.
So, if we start with this as background (300x50):
And this as overlay (122x242) - which is a tall blue rectangle surrounded with transparency then that is bordered in black to show the extent of it:
You would run this, which is actually very simple but it is full of comments and debug output so you can see what is going on:
#!/bin/bash
# Get background width and height
read wb hb < <(convert background.jpg -format "%w %h" info: )
echo "Background: " $wb $hb
# Get overlay width and height
read wo ho < <(convert overlay.png -format "%w %h" info: )
echo "Overlay: " $wo $ho
# Get wider of the two
w=$wb
[ $wo -gt $w ] && w=$wo
# Get taller of the two
h=$hb
[ $ho -gt $h ] && h=$ho
echo "New dimensions: " $w $h
convert background.jpg overlay.png -background none -gravity center -extent ${w}x${h} -composite result.png
Here is how it looks running:
Background: 300 50
Overlay: 122 242
New dimensions: 300 242
Presumably, when you have done your overlay, you would add -trim as the final part of the command line to remove any extraneous stuff that has been added.
Try using convert rather than composite. It is more flexible than composite.
convert logo.png in_image.jpg -gravity center -compose over -composite out_image.jpg
But if you insist on using composite, then
composite in_image.jpg logo.png -gravity center -compose src_over out_image.jpg
Note sure exactly what you mean by preserving the alpha channel, since jpg does not support transparency. Perhaps you can post a pair of inputs and your desired output, if what I have posted does not do what you want.

Image magic text writing on image dynamically

I am building a image editor with php, jquery, image magic, I am trying to write text on image dynamic position but not getting the exact command of image magic.
I tried imagemagic commands of writing text but these commands are for fixed position. e.g i need to write text on image "hey love" on position (x: 831, y: 38) x is width and y is height now what i need is exact command able to write text on dynamic positions.
convert temp.jpg -gravity North -pointsize 30 -annotate +0+100 'Love you mom' temp1.jpg
this command uses gravity i have dynamic positions
You can do it like this:
convert -size 400x250 xc:cyan -pointsize 18 -fill magenta -draw "text 250,65 'Love you Mum'" result.png

Merge Images Side by Side (Horizontally)

I have five images of sizes: 600x30, 600x30, 600x30, 600x30, 810x30. Their names are: 0.png, 1.png, 2.png, 3.png, 4.png, respectively.
How do I merge them Horizontally to make an image of size 3210x30 with ImageMagick?
ImageMagick ships with the montage utility. Montage will append each image side-by-side allowing you to adjust spacing between each image (-geometry), and the general layout (-tile).
montage [0-4].png -tile 5x1 -geometry +0+0 out.png
Other examples can be found on Montage Usage page
ImageMagick has command line tool named 'convert' to merge images horizontally, or for other purpose. i have tried this command and working perfectly on your case:
To join images horizontally:
convert +append *.png out.png
To stack images vertically:
convert -append *.png out.png
Use -resize if the images don't have the same width/height
You can fix the height for all of them with the -resize option, e.g. to fix a 500 pixel height on two images joined horizontally:
convert +append image_1.png image_2.png -resize x500 new_image_conbined.png
Or for vertical joins, you would want to set a fixed width instead:
convert -append image_1.png image_2.png -resize 500x new_image_conbined.png
Example:
image_1.png 1067x600
image_2.png 1920x1080
new_image_conbined.png 889x500
Related:
https://askubuntu.com/questions/226054/how-do-i-join-two-images-together
https://superuser.com/questions/290656/combine-multiple-images-using-imagemagick
How to do it interactively with GIMP
If you need to crop/resize images interactively first, which is often the case, then GIMP is the perfect tool for it, here's a detailed step-by-step: https://graphicdesign.stackexchange.com/questions/83446/gimp-how-to-combine-two-images-side-by-side/145543#145543
SVGs
ImageMagick 6.9.11-60 doesn't handle them, so see:
How to concatenate SVG files lengthwise from linux command line?
https://graphicdesign.stackexchange.com/questions/137096/is-there-a-way-to-stack-two-svgs-on-top-of-each-other
https://graphicdesign.stackexchange.com/questions/90844/joining-together-multiple-svg-images
Very simple with ImageMagick (brew install imagemagick )
convert +append image_1.png image_2.png new_image_conbined.png
Anyone using the MiniMagick rails gem can use the built-in tool to merge images:
# Replace this with the path to the images you want to combine
images = [
"image1.jpg",
"image2.jpg"
]
processed_image = MiniMagick::Tool::Montage.new do |image|
image.geometry "x700+0+0"
image.tile "#{images.size}x1"
images.each {|i| image << i}
image << "output.jpg"
end
Check out the documentation for #geometry options to handle resizing and placement. The current example will resize images to a 700px height while maintaining the image's aspect ratio. +0+0 will place the image with no gaps between them.
The convert +append method described in other answers appends images horizontally, aligned to the top. If you prefer to align to the bottom or center, try:
convert input1.png input2.png -gravity South +append output.png
or
convert input1.png input2.png -gravity Center +append output.png
Source: Fred's ImageMagick Tidbits http://www.fmwconcepts.com/imagemagick/tidbits/image.php#append

Resources