clementfarabet / graphicsmagick

A simple Lua wrapper to graphicsmagick.
126 stars 41 forks source link

documentation on pixel access #31

Open ghost opened 8 years ago

ghost commented 8 years ago

what is the syntax for getting the value of a pixel?

I have loaded an image and have tried:

width, length = myimage:size() --this works

but there does not appear to be a method in Image.lua that gives pixel access, does one need to be written? something like, Image:get_pixel(x,y) that returns a value and/or Image:get_pixels(x,y,w,h) that returns a table of values indexed by column, row.

or can MagickGetImagePixels be used?

soumith commented 8 years ago

We really wrote this in the context of torch, and in that case getting a pixel is pretty direct (access the tensor location). If you want direct pixel access in Lua, you might have to interface a few functions

ghost commented 8 years ago

sorry I am a very basic developer, are you saying that I need to add a declaration to http://www.graphicsmagick.org/api/pixel_cache.html#acquireonepixel or http://www.graphicsmagick.org/api/pixel_cache.html#getonepixel ? is that done in the Image.lua file at the top between the [[ and ]]

i suppose I could use a series of methods, clone():crop():format():tostring() I could get one pixel as a string containing raw bytes...

I tried adding declarations for PixelPacket and the getOnePixel() but I don't really know what I am doing with that.

I did manage to add declarations for

double PixelGetRed( const PixelWand wand ); double PixelGetGreen( const PixelWand wand ); double PixelGetBlue( const PixelWand *wand );

and then wrote this function:

--get pixel(s)
function Image:getPixel(x, y)
  -- Create PixelWand:
   local pixelwand = ffi.gc(clib.NewPixelWand(), function(pixelwand)
      -- Collect:
      clib.DestroyPixelWand(pixelwand)
   end)
   local r,g,b
   r = clib.PixelGetRed(pixelwand)
   g = clib.PixelGetGreen(pixelwand)
   b = clib.PixelGetBlue(pixelwand)
    print(r)
   return r,g,b
end

but of course this doesn't return anything other than 0 because no x,y coordinate is set anywhere. it is probably better to use the pixelPacket approach anyway. not sure where to go from here...

can anyone help? torch is not an option for my project but this library is mostly working for me, except this one thing. pixel access appears to be a basic feature in all the image libraries I've seen. could I request this feature to be added? it would probably be trivial for the maintainers or anyone familiar with declarations to add a method that interfaces with getonepixel, and one that interfaces with getpixels ( http://www.graphicsmagick.org/api/pixel_cache.html#getpixels ), this library already uses getImagePixels... in the toTensor() method... with this feature your library would be useful to many other users who are looking to switch from imagemagick in a lua environment...