Convert pixels within IM?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Convert pixels within IM?

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert pixels within IM?

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert pixels within IM?

Post by fmw42 »

Do you have more than two 1s in your 1D image?
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Convert pixels within IM?

Post 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.
Post Reply