In Ruby draw line on image, draw circle on image/ various shapes can be drawn using RMagick gem. To install RMagick gem you need to add RMagick in Gemfile
Add RMagick in Gemfile
Install the Gem
Bundle the Rails project to install rmagick if not already installed
After successful installation of RMagick gem you are ready to use RMagick features in your Rails Project
1. Create Image Object
To Draw something on a image, you need to create image object in Rails as follows,
Then you will have image object with variable
2. Create Draw object
You need to create draw object as follows,
This will create new draw object through which you can draw various shapes on canvas.
3. Draw on Canvas
Define colour need to be used to draw on canvas by following command,
The above command will set color with which drawing will happen
4. Draw Line on Canvas
The above command will draw line from point (x1, y1) to point (x2, y2)
Similarly you can draw line, circle — various shapes using object of Magick::Draw.new here gc.
Finally you need to impose canvas on image as,
The above command imposes whatever on canvas onto image.
5. Write Image file
You can write the image file on your file system using following command,
This will write image in current location of your project.
You can refer for more available methods with draw RMagick documentation which can well be used with Ruby. Feel free to comment if you face any problem while using RMagick with Ruby. Documentation will also help you with how to use RMagick gem for Image Manipulation with Ruby.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox