Page 1 of 1

Turn a set of pixels into grayscale ?

Posted: 2013-03-23T10:22:22-07:00
by gadapchetvoi
Hi all. This is my first post, thus please forgive my mistakes & my english. I need your helps with this situation:

I have a file with many points' coordinate. The file's content looks like this :

Code: Select all

color 1224,257 point color 1224,259 point color 1050,256 point color 1057,256 point color 1058,256 point...
And I want to use Windows' commandline to convert all the above points(pixels) of "X.png" to grayscale but keep those point's alpha transparent.
I hope I described the problem clearly, please ask me anything that can make it more understandable.
Really thanks for any help !
-----------------------------

Update: my file is "Pixel.txt" which contains fixed pixels' coordinates, and "X.png" can be ANY image. That I hope I can use it like this :

Code: Select all

convert  X.png  -colorspace gray -draw @Pixel.txt  X.png
But of course, that's not the correct way. I want to select all the pixel that have coordinates listed in "Pixel.txt" & convert them into graysacle, but leave the rest pixel as they are.

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-23T11:15:14-07:00
by snibgo
ImageMagick will not understand that text. To use ImageMagick, you need to change it to another format, such as "txt:". See http://www.imagemagick.org/Usage/files/#txt

For example, create a text file called "grays2.txt":

Code: Select all

# ImageMagick pixel enumeration: 2000,1000,255,srgba
1224,257: (128,128,128,0)
1224,259: (128,128,128,0)
1050,256: (128,128,128,0)
1057,256: (128,128,128,0)
1058,256: (128,128,128,0)
Then run this command:

Code: Select all

convert grays2.txt grays2.png
Now grays2.png will contain 5 pixels that are fully-transparent gray. All the other pixels are opaque white.

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-23T19:46:11-07:00
by gadapchetvoi
Thanks snibgo, but it seems that you misunderstood my question.
Please read my updated question. Thanks again.

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-23T20:53:44-07:00
by fmw42
see http://www.imagemagick.org/Usage/draw/#primitives

I do not know if -draw will read a file, but I think you can make a string variable

string="point x,y point x,y point x,y ... "

Then

convert image -fill gray -draw "$string" resultimage

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-23T21:40:07-07:00
by gadapchetvoi
Could you help me please?
I'm not going to fill the point x,y with gray, but going to CONVERT POINT X,Y TO GRAYSCALE.
Maybe not this :
fmw42 wrote:convert image -fill gray -draw "$string" resultimage
But something like this :

Code: Select all

convert image -toGrayScale "$string" resultimage
Thanks a lot !

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-23T21:54:16-07:00
by snibgo
"-draw" will read a file, using "-draw @something.txt", but the OP wants something else. I think the aim is to take an existing image and convert some of the pixels to greyscale. Something like ...

Code: Select all

convert ^
  X.png ^
  ( +clone -modulate 100,0,100 mask.txt -compose CopyOpacity -composite -channel Alpha -negate ) ^
  -compose Over -composite ^
  X2.png
... where mask.txt is ...

Code: Select all

# ImageMagick pixel enumeration: 2000,1000,255,srgb
1224,257: (0,0,0)
1224,259: (0,0,0)
1050,256: (0,0,0)
1057,256: (0,0,0)
1058,256: (0,0,0)
You can use X.png as the output as well as the input. But testing is harder when you destroy the input.

I show mask.txt with dimensions (2000,1000), but these must match the dimensions of X.png. Not a problem, as it would be built with a script.

Or you could use fmw's method, and create a text file or string (Windows "environment variable") of draw commands, and make the mask with that.

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-24T00:10:38-07:00
by snibgo
gadapchetvoi wrote:And I want to use Windows' commandline to convert all the above points(pixels) of "X.png" to grayscale but keep those point's alpha transparent.
If we use my technique above, we can add a facility to save and restore the alpha channel, but it is messy:

Code: Select all

convert ^
  X.png ^
  -set colorspace RGB ^
  ( +clone -channel Alpha -separate -write mpr:saveAlpha +delete ) ^
  ( +clone -modulate 100,0,100 mask.txt -compose CopyOpacity -composite -channel Alpha -negate ) ^
  -compose Over -composite ^
  -channel RGBA ^
  -separate ^
  mpr:saveAlpha ^
  -delete 3 ^
  -combine ^
  -set colorspace sRGB ^
  X2.png

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-24T01:05:30-07:00
by gadapchetvoi
snibgo wrote:

Code: Select all

convert ^
  X.png ^
  -set colorspace RGB ^
  ( +clone -channel Alpha -separate -write mpr:saveAlpha +delete ) ^
  ( +clone -modulate 100,0,100 mask.txt -compose CopyOpacity -composite -channel Alpha -negate ) ^
  -compose Over -composite ^
  -channel RGBA ^
  -separate ^
  mpr:saveAlpha ^
  -delete 3 ^
  -combine ^
  -set colorspace sRGB ^
  X2.png
Thanks a lot snibgo ! Your code worked like a charm!
But to say the truth, I've never ever imagined it must be so complicated !
Although it's pretty hard to understand, I owe you a beers for your awesome experience!

Re: Turn a set of pixels into grayscale ?

Posted: 2013-03-24T22:54:31-07:00
by anthony
NOTE; When reading a Enumerated Pixel text file (.txt), anything not 'enumerated' will be set to the background color, whcih is white by default.

I suggest you set the background color appropriately.

Also. An easy way is to use that mask file as a Write Mask....
See Write masks..
http://www.imagemagick.org/Usage/masking/#write_mask

For example..

Code: Select all

convert X.png -background white -mask mask.txt -modulate 100,0,100 X2.png
that is a lot simplier!

To turn of the mask (for other processing use +mask.

WARNING: image masking in IMv7 will likely be different but greatly improved.