【问题标题】:How to create an image in memory with the MiniMagick::Tool::Convert如何使用 MiniMagick::Tool::Convert 在内存中创建图像
【发布时间】:2019-01-24 18:48:36
【问题描述】:

我有一个函数可以打开图像,调整它的大小,然后设置要包含在其调色板中的最大颜色数。然后将此修改后的图像用于内部处理。我的偏好是避免将图像保存到磁盘,然后立即打开。

有没有办法使用 MiniMagick::Tool::Convert 并将输出捕获到内存中?

def create_image_for_processing(image_path, resize, colors)
    MiniMagick::Tool::Convert.new do |convert|
        convert << image_path
        convert << '-resize' << resize
        convert << '-colors' << colors
        convert << 'temp.png'
    end
    MiniMagick::Image.open('temp.png')
end

【问题讨论】:

    标签: ruby minimagick


    【解决方案1】:

    Charlie Prezzano 的回答看起来应该可以正常工作,但它对我不起作用,所以我将在此处发布替代方案。这对我有用:

    image_data =
        MiniMagick::Tool::Convert.new do |convert|
          convert << image_path
          convert << '-resize' << resize
          convert << '-colors' << colors
          convert << 'png:-'
        end
    MiniMagick::Image.read(image_data)
    

    【讨论】:

      【解决方案2】:

      这行得通:)

      image_data =
          MiniMagick::Tool::Convert.new do |convert|
            convert << image_path
            convert << '-resize' << resize
            convert << '-colors' << colors
            convert.stdout
          end
      MiniMagick::Image.read(image_data)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多