Page 1 of 1

Convert pixels within IM?

Posted: 2018-11-18T18:34:57-07:00
by muccigrosso
I'm working with single rows of pixels in binary images, so a bunch of white and black pixels. What I'd like to do is make sure that all the pixels between the first and the last black ones are also black. For example, if the row were:

Code: Select all

0 0 0 1 0 0 1 0 0 0
It would become

Code: Select all

0 0 0 1 1 1 1 0 0 0
Can I do this within IM without writing out to a file or variable? (I'm writing in bash.)

I was thinking about converting to pbm format, then using regex, but the line breaks inside the pbm file make that harder than anticipated and I think I have to store the file data while I process it.

On a Mac, High Sierra, ImageMagick 7.0.8-14 Q16 x86_64.

Re: Convert pixels within IM?

Posted: 2018-11-18T18:57:00-07:00
by snibgo
Easy. Make a sample image:

Code: Select all

magick xc:black xc:black xc:black xc:White xc:Black xc:Black xc:White xc:black xc:black xc:black +append +repage x.png

Add a black border, then trim without repage. Make the image white, then "untrim", and shave off the added border.

Code: Select all

magick x.png -bordercolor Black -border 1 -trim -fill White -colorize 100 -background Black -layers Flatten -shave 1x1 out.png

Re: Convert pixels within IM?

Posted: 2018-11-18T20:11:07-07:00
by fmw42
Do you have more than two 1s in your 1D image?

Re: Convert pixels within IM?

Posted: 2018-11-22T00:59:37-07:00
by muccigrosso
@snibo, thanks! That's the approach I was looking for. (I often find that I just need a new way of thinking about what I'm trying to do to get IM to work for me.)

Turns out that I don't want to add the border before trimming, or at least I don't want to add a black one. A white one works fine.