What should I do if I want to convert an existing image file to another format in an environment where R Magick is already installed?
Below is sample code to convert a locally existing png file to webp format.
require 'rmagick'
image = Magick::ImageList.new('xxx.png')
image.write('xxx.webp')
From here, we will show you how to install R Magick.
RMagick requires ImageMagick as prerequisite software. For the installation method, refer to RMagick's README.md for each environment. My environment is macOS and I have installed the package manager Homebrew, so it was very easy.
brew update
brew upgrade
brew install imagemagick
After installing Imagemagick, install rmagick via RubyGem. You can also refer to RMagick's README.md for this as well. By the way, I used bundler
.
bundle init
vi Gemfile #=> gem 'rmagick'Added
bundle install
By the way, the introduction of R Magick seems to have a lot of troubles, so if you have any problems, RMagick's README.md "Things that can go wrong" I think you should refer to .md).
Even in my environment, the following error occurs during bundle install
.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
ERROR: Can't install RMagick 4.1.2. Can't find pkg-config in
We were able to resolve this problem based on the "Things that can go wrong" response method.
brew install pkg-config
$ brew -v
Homebrew 2.5.1
Homebrew/homebrew-core (git revision a2bbe; last commit 2020-09-16)
Homebrew/homebrew-cask (git revision dfa88; last commit 2020-09-16)
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin18]
$ bundle -v
Bundler version 2.1.2
Recommended Posts