Page 1 of 1

make every nth pixel transparent

Posted: 2017-05-24T16:26:58-07:00
by tcuser
I have a simple white box and I am essentially trying to create a mask where every nth pixel (in x and y) is made transparent. Basically, I want to lay the mask over images and demonstrate the impact of higher resolutions by varying "n".

As a newbie, I found that I could:
convert image.png -alpha set -region 2x2+1+1 -alpha transparent newimage.png

This gets me one set of transparent pixels, but I cannot find how to replicate this over every n pixels, short of writing a script to keep calculating what pixels to modify and one-by-one convert the image over an over to the achieve the desired goal.

So for n=10, I think what I'm looking for is the proper way to express -region 2x2+1+1 AND 2x2+1+11 AND 2x2+1+22 AND 2x2+1+33, etc.
OR, perhaps there's an even simpler way to make such a mask.

Any help is much appreciated, thanks.

Re: make every nth pixel transparent

Posted: 2017-05-24T16:41:49-07:00
by snibgo
I don't understand what you want in your mask.

I suggest you create some examples in Gimp or similar, where black = transparent, white = opaque.

Re: make every nth pixel transparent

Posted: 2017-05-24T16:53:09-07:00
by tcuser
Here is a sample I made using a script to daisy-chain the above convert line one conversion at a time - yes, technically it works - but a terrible solution and very slow as n gets smaller.

In this case I made a region of 3x3 pixels transparent, every 20th pixel. The mask is sitting over a colorized picture of a hurricane, but at n=20 you would not know it is a hurricane. Next I would set n=10, then n=5, etc until eventually the entire image becomes visible.

Image

The image does not seem to show up. the url is: https://drive.google.com/file/d/0B0sgvn ... sp=sharing

Re: make every nth pixel transparent

Posted: 2017-05-24T17:14:12-07:00
by fmw42
Is this what you want? I have used the ImageMagick internal image logo: which is 640x480. I create a 10x10 tile that is all black with one white pixel in the upper left corner. Then I tile it out to the size of the logo: image. Then I put it into the alpha channel of the logo: image and write it out.

In IM 6:

Code: Select all

convert logo: \
\( -size 10x10 xc:black -size 1x1 xc:white -gravity northwest -composite \
-write mpr:tile +delete -size 640x480 tile:mpr:tile \) \
-alpha off -compose copy_opacity -composite logo_dots.png
or in IM 7, you can get the logo: size and use it inline

Code: Select all

magick logo: -set option:dim "%wx%h" \
\( -size 10x10 xc:black -size 1x1 xc:white -gravity northwest -composite \
-write mpr:tile +delete -size "%[dim]" tile:mpr:tile \) \
-alpha off -compose copy_opacity -composite logo_dots.png
Image

For tiling, see http://www.imagemagick.org/Usage/canvas/#tile_memory

Re: make every nth pixel transparent

Posted: 2017-05-24T18:02:49-07:00
by fmw42
Here is an alternate method for IM 6 that does not need to know the size of the input image.

Code: Select all

convert logo: \
\( -size 10x10 xc:black -size 1x1 xc:white -gravity northwest -composite -write mpr:tile +delete \) \
\( +clone -tile mpr:tile -draw "color 0,0 reset" \) \
-alpha off -compose copy_opacity -composite logo_dots.png

Re: make every nth pixel transparent

Posted: 2017-05-24T18:11:17-07:00
by tcuser
I ran this code using my own image. I'm still digesting what this is doing and how it works, but the end result gives me the desired effect! Thank you.

Re: make every nth pixel transparent

Posted: 2017-05-24T18:31:10-07:00
by fmw42
# first line -- read image

# second line -- create a 10x10 black image, then a 1x1 white image, then put (composite) the white image into the black image at its top left corner via -gravity northwest and -composite, then write that composited image to an in memory mpr: image that I call mpr:tile and delete the disk copy.

# third line -- copy the original image use one of the tile commands to tile it out and replace its image texture with the tile texture, so that I have a black and white image the size of the logo: image

# fourth line -- turn alpha off of both images and put the black and white dot image into the alpha channel of the logo: image such that black will be fully transparent and white will be fully opaque.

# fifth line -- save the output

Code: Select all

convert logo: \
\( -size 10x10 xc:black -size 1x1 xc:white -gravity northwest -composite -write mpr:tile +delete \) \
\( +clone -tile mpr:tile -draw "color 0,0 reset" \) \
-alpha off -compose copy_opacity -composite \
logo_dots.png
see the fifth example down at http://www.imagemagick.org/Usage/canvas/#tile where it uses -draw "color 0,0 reset" and http://www.imagemagick.org/Usage/draw/#color and http://www.imagemagick.org/Usage/draw/#color where it says color x,y method and http://www.imagemagick.org/Usage/files/#mpr

Re: make every nth pixel transparent

Posted: 2017-05-24T18:40:36-07:00
by tcuser
Thank you for the detailed reply, I will follow-up on reading the examples. I played with varying the size of the tile, going from 50x50 to 25x25 to 12x12, etc and got the desired effect of showing more of the image each time the tile size was reduced. I ran into a problem however when I tried to do every other pixel (2x2) - I think I just broke the x server :)

Re: make every nth pixel transparent

Posted: 2017-05-24T18:45:39-07:00
by fmw42
This should be fine for 2x2. It works for me on IM 6.9.8.6 Q16 Mac OSX.

Code: Select all

convert logo: \
\( -size 2x2 xc:black -size 1x1 xc:white -gravity northwest -composite -write mpr:tile +delete \) \
\( +clone -tile mpr:tile -draw "color 0,0 reset" \) \
-alpha off -compose copy_opacity -composite \
logo_dots.png
Please always provide your IM version and platform when asking questions on this forum.

Re: make every nth pixel transparent

Posted: 2017-05-24T18:51:49-07:00
by tcuser
I have to use reflection software on an ancient pc to connect to a redhat 6.9 server running IM 6.7.2-7

it grinds to a halt when trying to render the image at 2x2 (and 3x3 too), and eventually crashes due to "lack of resources". Anyway, at least I can convey the point I was looking for. Thank you again!

Re: make every nth pixel transparent

Posted: 2017-05-24T19:11:27-07:00
by fmw42
IM 6.7.2-7 is ancient. About 260 versions old. Perhaps you should consider upgrading if possible. It is possible there is a bug in that old version that an upgrade would fix.