QuickMagick を使用して画像からすべてのグレースケールを削除します
概要
Ruby の QuickMagick または RMagic を使用して、画像からすべての灰色を削除したいと考えています。ここでこの解決策を見つけました: https://www.imagemagick.org/discourse-server/viewtopic.php?t=36051 ImageMagick 用ですが、構文を QuickMagick に適切に変換する方法がわかりません
convert in.jpg \( +clone -colorspace HCL -channel G -separate +channel -threshold 15% \) -alpha off -compose CopyOpacity -composite out.png
編集: @8bithero が試してみました
image = QuickMagick::Image.read(remote_image.path).first
# Clone the image and perform operations
clone = image.clone
clone.colorspace = 'HCL'
clone.channel('G').separate
clone.threshold(15)
# Composite the original and the clone
# image.alpha('off')
image.compose = 'CopyOpacity'
image = image.composite(clone, 0, 0, 'CopyOpacity')
# Save the output
image.save(outfile)
しかし、エラーが発生しました
QuickMagick::QuickMagickError ( Error executing command: command
15:45:49 web.1 | Result is:
15:45:49 web.1 | Error is: convert: unable to open image '#<QuickMagick::Image:0x000000010d0fe0c0> 0 0 CopyOpacity': No such file or directory @ error/blob.c/OpenBlob/3572.
15:45:49 web.1 | convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/746.
15:45:49 web.1 | convert: image sequence is required `-composite' @ error/mogrify.c/MogrifyImageList/8049.
15:45:49 web.1 | convert: no images defined `c8cfa655-8437-4ea6-8c34-e60f88fffe8b-1.png' @ error/convert.c/ConvertImageCommand/3362.
15:45:49 web.1 |
15:45:49 web.1 | ):
解決策
これを機能させるには、コマンドライン構文を QuickMagick の Ruby ベースの構文に変換する必要があります。
これをテストしていないので、うまくいくかどうかはわかりませんが、正しい軌道に乗せることを願っています
require 'quick_magick'
# Load the image
image = QuickMagick::Image.read(remote_image.path).first
# Clone the image and perform operations
clone = image.clone
clone.colorspace = 'HCL'
clone.channel('G').separate
clone.threshold(15)
# Composite the original and the clone
image.alpha('off')
image.compose = 'CopyOpacity'
image = image.composite(clone, 0, 0, 'CopyOpacity')
# Save the output
image.save(outfile)
必ず、remote_image.path を入力画像へのパスに変更し、出力パスで出力してください。
アップデート: 発生したエラーは、QuickMagick フレームワーク内で画像オブジェクトが渡され、認識される方法に問題があることを示唆しています。これは、API 呼び出しの違い、または最新バージョンでの画像操作コマンドの構造の違いが原因である可能性があります。 RMagick がインストールされており (gem install rmagick)、ImageMagick もシステムにインストールされて適切に設定されており、最新バージョンを使用していると仮定します。それを念頭に置いて、更新されたバージョンを次に示します。
require 'rmagick'
# Load the image
image = Magick::Image.read(remote_image.path).first
# Clone the image and convert to HCL colorspace
clone = image.clone
clone.colorspace = Magick::HCLColorspace
# Separate the green channel and apply threshold
clone = clone.separate(Magick::GreenChannel).first
threshold_image = clone.threshold(Magick::QuantumRange * 0.15)
# Apply the threshold image as an opacity mask
image.matte = false # Disable alpha channel
image = image.composite(threshold_image, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
# Save the output
image.write(outfile)
アプローチ 2: 上記の提案も失敗する場合は、次のようにして、Ruby から ImageMagick コマンドを直接実行することもできます。
require 'open3'
input_path = remote_image.path
output_path = outfile
command = "convert #{input_path.shellescape} \\( +clone -colorspace HCL -channel G -separate +channel -threshold 15% \\) -alpha off -compose CopyOpacity -composite #{output_path.shellescape}"
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
exit_status = wait_thr.value
unless exit_status.success?
raise "ImageMagick command failed: #{stderr.read}"
end
end