Page 1 of 1
How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-12T11:52:29-07:00
by mikebounds
If I want to replace white with orange I can use
Code: Select all
convert orig.png -fill orange -opaque white new.png
But how would I replace white with a checkerboard pattern of red and yellow (dithered colour) ?
I am running ImageMagick 6.8.9-9 on Ubuntu 16.04
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-12T13:58:20-07:00
by fmw42
See the checkerboard pattern at
https://imagemagick.org/script/formats. ... n-patterns. You can recolor that. Or you can create a checkerboard pattern of your own. Make and image the same size as your orig.png with the colored checkerboard. Then make white into transparent with -transparent white. Then composite your image over the colored checkerboard pattern image.
Post your input image to some free hosting service and put the URL here. Then we can use that to show you the code.
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-12T14:29:07-07:00
by mikebounds
Example image:
Desired output with a checkerboard (each square of one pixel) of red and yellow:
blown up extract
I did the above using fill with pattern in mtPaint, but I want to be able to do this to 50 images so want to be able to do translation in imagemagick with any combination of 2 colours - example cyan and white:
blown up extract
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-12T16:26:37-07:00
by fmw42
Your images did not show up. So I removed the img tag.
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-12T16:32:04-07:00
by fmw42
Here is how I would do it in Unix syntax. Note that your input has no transparency. So it is not possible to color the white directly with checkerboard pattern. So I have to make the white into transparency. Then make the checkerboard pattern. The composite the transparent input over the checkerboard pattern.
Code: Select all
Line 1: read the input
Line 2: copy the input and negate (invert) it
Line 3: put the negated image into the alpha channel of the input, so that white becomes transparent.
Line 4: create 1 pixel sized red and yellow images and append them side to side
Line 5: copy that and flip it left to right
Line 6: append them top to bottom and save as in memory mpr: image and delete the disk file version
Line 7: clone the input image and draw the checkerboard pattern over it
Line 8: swap the order of the two remaining images
Line 9: composite them
Line 10: write the output
Code: Select all
convert img.png \
\( +clone -negate \) \
-alpha off -compose copy_opacity -composite \
\( -size 1x1 xc:red xc:yellow +append \
\( +clone -flop \) \
-append -write mpr:checks +delete \) \
\( +clone -fill mpr:checks -draw 'color 0,0 reset' \) \
+swap -compose over -composite \
img_checks.png
or
Code: Select all
convert img.png \
\( +clone -negate \) \
-alpha off -compose copy_opacity -composite \
\( -size 1x1 xc:red xc:yellow +append \
\( -size 1x1 xc:yellow xc:red +append \) \
-append -write mpr:checks +delete \) \
\( +clone -fill mpr:checks -draw 'color 0,0 reset' \) \
+swap -compose over -composite \
img_checks.png
See
https://imagemagick.org/Usage/basics/#parenthesis
https://imagemagick.org/Usage/basics/#clone
https://imagemagick.org/Usage/files/#mpr
https://imagemagick.org/Usage/layers/#append
https://imagemagick.org/Usage/canvas/#tile_memory
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-13T03:06:39-07:00
by mikebounds
Thanks Fred - much appreciated- this does exactly what I wanted. I understand what it is doing, but don't understand all of the commands, especially making white into transparent - I have just used "-transparent white" in the past and this seems to work if I replace in your script so this is easier for me to understand and I also created images in the right order to start with so I didn't need to swap (I think +clone clones last image and "-clone 0" clones original input image)
Code: Select all
convert img.png \
\( -size 1x1 xc:red xc:yellow +append \
\( +clone -flop \) \
-append -write mpr:checks +delete \) \
\( -clone 0 -fill mpr:checks -draw 'color 0,0 reset' \) \
\( -clone 0 -transparent white \) \
-compose over -composite \
img_checks.png
Also, I may not always be replacing white with transparent, so I thought the above would also work for changing other colours to a check fill pattern, by just changing "white" to the colour I want to change, but this did not work and white is always changed to transparent which I don't understand as for instance:
Code: Select all
convert img.png -transparent black img_trans.png
changes black, not white to transparent, so I don't understand why this doesn't work in clone command if I change line to:
Code: Select all
\( -clone 0 -transparent black \) \
then white still gets changed to red/yellow check, not black.
Thanks again for your help on this as it has not just resolved my problem, but made me understand more of what you can do with imagemagick.
Mike
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-13T09:33:17-07:00
by fmw42
The problem with -transparent somecolor is that if the image has antialiasing, it will leave aliased artifacts that may still be there even with -fuzz XX%. So that is why I did it above. It is harder to do that if you are changing some other color.
Something odd is going on with -clone 0 that I do not see right away. But changing to using mpr: for the img.png image works.
Code: Select all
convert img.png +write mpr:img +delete \
\( -size 1x1 xc:red xc:yellow +append \
\( +clone -flop \) \
-append -write mpr:checks +delete \) \
\( mpr:img -fill mpr:checks -draw 'color 0,0 reset' \) \
\( mpr:img -transparent black \) \
-compose over -composite \
img_checks.png
Re: How to replace colour with a 2-colour checkerboard pattern
Posted: 2019-03-13T10:08:51-07:00
by mikebounds
Thanks, this now works for any colour. I couldn't get "-transparent black" to work with -clone 0 or +clone with swap, but writing image to memory work, but would be interested if you do find out why it doesn't work with clone.
Mike