minimagick: `get_pixels` is not working properly

I am trying to get all the pixels to generate CSV file for train-data to tensor-flow machine learning algorithm. I intend to serialize all the pixels of every image in every row.

require 'mini_magick'
require 'csv'
require 'pry'
image_data = MiniMagick::Image.open(File.expand_path('../../Train/digit_0/4558', __FILE__))

# now grayscaling the image_data
image_data.colorspace 'Gray'
pixels = image_data.get_pixels # returns pixels [R, G, B] format
# > [[232, 232, 232], [121, 121, 121], ...]

[1] pry(main)> image_data.get_pixels.map(&:count)
=> [32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 16]
[2] pry(main)> image_data.get_pixels.map(&:count)
=> [32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 16]
[3] pry(main)> image_data.get_pixels.map(&:count).count
=> 30

[5] pry(main)> image_data
=> #<MiniMagick::Image:0x00000001dcf978
 @info=
  #<MiniMagick::Image::Info:0x00000001dcf950
   @info={"raw:%m %w %h %b"=>"PNG 32 32 582B", "format"=>"PNG", "width"=>32, "height"=>32, "dimensions"=>[32, 32], "size"=>582, "human_size"=>"582B"},
   @path="/tmp/mini_magick20170503-32521-17lpgo">,
 @path="/tmp/mini_magick20170503-32521-17lpgo",
 @tempfile=#<File:/tmp/mini_magick20170503-32521-17lpgo (closed)>>

looks like mini_magick is not getting me all the pixel values. According to imagemagick the picture is 32x32 but the array I am getting is only 30x32 and also the last row seems to be incomplete.

Have I done any mistakes? can anybody tell how to fix?

Thanks and help will be appreciated

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks to collaborative debugging and @LAndreas figuring out the bug, this issue should now be fixed in latest version of MiniMagick (4.8.0).

The sample image provided by @shivabhusal has a couple of black rows at the bottom, which was a clue, given that the number of rows captured by get_pixels was 30 instead of 32.

There is indeed a bug, and it looks like there is something wrong with this bit of code:

def get_pixels
    output = MiniMagick::Tool::Convert.new do |convert|
      convert << path
      convert.depth(8)
      convert << "RGB:-"
    end
    [...]

Note that output.length matches the position of the LAST non-black pixel. All trailing [0,0,0] pixels are apparently trimmed.

Also note that the following raw code seems to work instead:

output = `convert filename.png RGB:-`
pixels_array = output.unpack("C*")

Here, output.length gives the correct number of rows.