how to avoid loss of transparency due to overwriting color in png file

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?".
Locked
manit
Posts: 157
Joined: 2009-01-30T22:31:26-07:00

how to avoid loss of transparency due to overwriting color in png file

Post by manit »

hi,
I am using
Version: ImageMagick 7.0.7-25 Q16 x86 2018-03-04 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr openexr pangocairo png ps raw rsvg tiff webp xml zlib
on windows 10 home OS .

I have a png image containing a number written in black on white background.
My aim is to make a semi-transparent green circle around that number so that number can be read and is also covered in green color.
I am using following command

magick my-file.png -fill "#00FF0090" -draw "circle 140,30 140,55" my-file.png

I have observed that running the above command again causes the circle to get filled with a little more solid green so that black is less visible.
Actually, my script on each run will fill the same area with green color and I want that green to remain as it looked the first time.
See https://ibb.co/bdWQy0B
Here the first green filled number is ok for me.
But, the next number got almost completely overwritten by green which I do not want.
How can I overcome this ?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to avoid loss of transparency due to overwriting color in png file

Post by snibgo »

That is like stamping a bingo card with semi-transparent ink. Each time you stamp the same place, you add more ink so less card will show through it.

The obvious solution is to not repeatedly stamp the same place.
snibgo's IM pages: im.snibgo.com

manit
Posts: 157
Joined: 2009-01-30T22:31:26-07:00

Re: how to avoid loss of transparency due to overwriting color in png file

Post by manit »

ok.
is it possible to detect whether that place already has green color ?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to avoid loss of transparency due to overwriting color in png file

Post by snibgo »

An IM command could find the colour of a pixel, or the average of a number of pixels. You could use that in a script to determine whether that area had already been stamped.
snibgo's IM pages: im.snibgo.com

manit
Posts: 157
Joined: 2009-01-30T22:31:26-07:00

Re: how to avoid loss of transparency due to overwriting color in png file

Post by manit »

In my case , I want to count/detect green pixel in small circular area of whole image.
So , i have work on that patch .
I can mask the rest of image or extract that part and process it separately.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to avoid loss of transparency due to overwriting color in png file

Post by fmw42 »

If you threshold and make the green white and the rest black, then the average color times the area of the image will give you the count of white pixels (which are the green ones)

threshold your green image into black and white (not grayscale)

Code: Select all

convert black_white_image -format "%[fx:mean*w*h]" info:
is the count of white pixels.

Locked