make every nth pixel transparent

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
tcuser
Posts: 5
Joined: 2017-05-24T15:53:09-07:00
Authentication code: 1151

make every nth pixel transparent

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

Re: make every nth pixel transparent

Post 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.
snibgo's IM pages: im.snibgo.com
tcuser
Posts: 5
Joined: 2017-05-24T15:53:09-07:00
Authentication code: 1151

Re: make every nth pixel transparent

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: make every nth pixel transparent

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: make every nth pixel transparent

Post 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
tcuser
Posts: 5
Joined: 2017-05-24T15:53:09-07:00
Authentication code: 1151

Re: make every nth pixel transparent

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

Re: make every nth pixel transparent

Post 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
tcuser
Posts: 5
Joined: 2017-05-24T15:53:09-07:00
Authentication code: 1151

Re: make every nth pixel transparent

Post 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 :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: make every nth pixel transparent

Post 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.
tcuser
Posts: 5
Joined: 2017-05-24T15:53:09-07:00
Authentication code: 1151

Re: make every nth pixel transparent

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: make every nth pixel transparent

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