Skip to content

Possible ImageMagick changes causing error #600

@matthewblott

Description

@matthewblott

I have a library RandomLineDrawing used with testing that generates random images and saves them to disk. This is the code:

require 'mini_magick'
require 'fileutils'

class RandomLineDrawing
  def self.generate(filename, width: 500, height: 500, lines: 20, line_width: 2)
    # Ensure the directory exists
    FileUtils.mkdir_p(File.dirname(filename)) unless Dir.exist?(File.dirname(filename))
    
    # Build the command arguments
    args = []
    args << "-size" << "#{width}x#{height}"
    args << "xc:white"
    args << "-stroke" << "black"
    args << "-strokewidth" << line_width.to_s
    
    # Add drawing commands
    lines.times do
      x1, y1 = rand(width), rand(height)
      x2, y2 = rand(width), rand(height)
      args << "-draw" << "line #{x1},#{y1} #{x2},#{y2}"
    end
    
    # Save to file
    args << filename
    
    # Use the recommended approach for ImageMagick 7
    MiniMagick::Tool::Magick.new do |magick|
      args.each { |arg| magick << arg }
    end
    
    # Return the filename
    filename
  end
  
end

This is then called with:

RandomLineDrawing.generate('filename.png')

It has stopped working. The offending lines are:

MiniMagick::Tool::Magick.new do |magick|
  args.each { |arg| magick << arg }
end

This is the error produced:

lib/random_line_drawing.rb:32:in 'RandomLineDrawing.generate': wrong number of arguments (given 0, expected 1) (ArgumentError)
        from (web):1:in '<main>'

All the examples I have found online do not work. It appears that something has changed, possibly with ImageMagick's API. I have got round my issue by using Ruby's system method and using a shell command but this isn't ideal. Any help appreciated :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions