Ruby MojoMagick to round corners
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi there,

I need to round corners using MojoMagick.

I have this solution for resizing but I now need to round the corners.

https://bountify.co/ruby-resize-images-ignore-proportions-stretch-or-shrink

@davesargent .Did you saw my last solution?
Zhopon 1 month ago
@davesargent, could you please review the two solutions or assign this bounty to charity? Thanks!
bevan 5 days ago
awarded to DanKnox-BitFS
Tags
ruby

Crowdsource coding tasks.

2 Solutions


Simple solution without using MojoMagick, using ImageMagick command 'convert' directly:

https://gist.github.com/Nervovred/7ea8473689b4651952dc8bf10e1c573e

Winning solution

You can execute any ImageMagick command in MojoMagick using the MojoMagick::execute method like so:

in_pic = "in.jpg" 
out_pic = "out.jpg"
radius = 25 
size = "500x500"
# JPG doesn't support alpha channel, write "None" if you want transparent edges for PNG files
bgcolor = "white" 

cmd = "convert #{in_pic} " +
      "-resize #{size}! " + # comment out this line if you don't want to resize
      "\\( +clone -alpha extract " +
      "\\( -size #{radius}x#{radius} xc:black " +
      "-draw 'fill white circle #{radius},#{radius} #{radius},0' " +
      "-write mpr:arc +delete \\) " +
      "\\( mpr:arc \\) -gravity northwest -composite " +
      "\\( mpr:arc -flip \\) -gravity southwest -composite " +
      "\\( mpr:arc -flop \\) -gravity northeast -composite " +
      "\\( mpr:arc -rotate 180 \\) -gravity southeast -composite \\) " +
      "-alpha off -compose CopyOpacity -composite -compose over " +
      "\\( +clone -background black \\) +swap " + 
      "-background #{bgcolor} -layers merge #{out_pic}"

# Here is where we execute the command using MojoMagick instead 
# of shelling out to the command line.

result = MojoMagick.execute(cmd)

Full disclosure... I am not certain wether or not executing the command in this way will retain the supposed memory utilization benefits that seem to be MojoMagick's selling point. This is the only way to do this without switching to something like RMagick though.

Thanks to @kirill.shchepelin for posting the raw command used for the rounded corner technique. I merely added a way to execute it using the MojoMagick interface.

View Timeline