You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Ruby及ruby-vips gem将图片转换为单色形状图?

Solution using Ruby and ruby-vips

Got it, let's tackle this problem! You want to convert any image (no matter its original colors or shape) into a version that only preserves the outline—filling all non-transparent areas with a single solid color. Using Ruby and the ruby-vips gem is perfect here because it's fast, memory-efficient, and handles all common image formats seamlessly.

Step 1: Set up the gem

First, make sure you have ruby-vips installed. You can install it via RubyGems:

gem install ruby-vips

Or add it to your Gemfile if you're using Bundler:

gem 'ruby-vips'

Step 2: Complete working code

Here's a ready-to-use function that does exactly what you need. It takes an input image path, output path, and optional fill color (defaults to red if not specified):

require 'vips'

def convert_to_shape_only(input_path, output_path, fill_color = [255, 0, 0])
  # Open the input image with sequential access (great for large files)
  image = Vips::Image.new_from_file(input_path, access: :sequential)

  # Ensure the image has an Alpha channel—add one if it doesn't exist
  image = image.add_alpha unless image.has_alpha?

  # Extract the Alpha channel to use as a mask for transparency
  alpha_channel = image.extract_band(image.bands - 1)

  # Create a solid-colored image matching the original's dimensions and format
  solid_image = Vips::Image.new_from_array(fill_color, image.width, image.height)
  solid_image = solid_image.cast(image.format)

  # Combine the solid color with the original Alpha channel to preserve transparency
  result_image = solid_image.bandjoin(alpha_channel)

  # Save the final shape-only image
  result_image.write_to_file(output_path)
end

# Example usage: Convert input.png to a green shape-only image saved as output.png
convert_to_shape_only('input.png', 'output.png', [0, 255, 0])

How this works

Let's break down the key parts of the code:

  • Sequential access: Using access: :sequential when opening the image means ruby-vips processes the image in chunks instead of loading it all into memory—critical for large images.
  • Alpha channel handling: We check if the image has an Alpha channel; if not, we add a fully opaque one. This ensures the code works for both transparent and non-transparent images.
  • Mask with Alpha: By extracting the Alpha channel, we retain the original image's transparent areas exactly as they are.
  • Solid color layer: We create a solid color image that matches the original's size and pixel format to avoid color inconsistencies.
  • Combine layers: bandjoin merges the solid color layer with the Alpha channel, resulting in an image where all non-transparent areas are your chosen solid color, and transparent areas stay clear.

Notes

  • Supported formats: This works with all image formats ruby-vips supports, including PNG, WebP, GIF (with transparency), and JPEG (though JPEG doesn't support transparency, the code will still add an opaque Alpha channel and fill the entire image).
  • Customize fill color: Pass any RGB value as an array (values 0-255). For example, [255,255,255] for white, [0,0,0] for black, or any custom hex-to-RGB converted value.
  • Grayscale images: The code automatically handles grayscale inputs—no extra changes needed.

内容的提问来源于stack exchange,提问作者Molfar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:52:42