Page 1 of 1

Changing a single pixel to be slightly transparent

Posted: 2018-07-25T20:15:06-07:00
by AkemiHomura
Use case: Social networks Twitter and Tumblr convert all PNGs to JPGs, sometimes substantially lowering their quality, unless the PNG has transparency. A single pixel being 'only' 99% opaque counts as transparency.

Is there any way to use ImageMagick to make a single pixel (e.g. 0,0) very slightly transparent without changing its RGB values? Just setting the A value to 0xFE for example. I can safely assume images have no transparency yet as I can detect images which do and upload as-is.

Re: Changing a single pixel to be slightly transparent

Posted: 2018-07-25T22:24:49-07:00
by fmw42
You have not said what your ImageMagick version is nor what platform you use. Syntax may differ. You also have not said if you need 24-bit PNG or 8-bit PNG


try Unix syntax to produce a 24-bit PNG file with the pixel 0,0 partially transparent

Code: Select all

convert logo: \
\( +clone -crop 1x1+0+0 +repage -alpha on -channel a -evaluate set 99% +channel \) \
-alpha on -channel rgba -geometry +0+0 -compose copy -composite logo.png
replace logo: with your image.png and logo.png with your image.png

Code: Select all

identify -verbose logo.png


Here are the last few entries of the histogram:

9: (255,242,193,255) #FFF2C1FF srgba(255,242,193,1)
31: (255,246,209,255) #FFF6D1FF srgba(255,246,209,1)
14: (255,249,224,255) #FFF9E0FF srgba(255,249,224,1)
256243: (255,255,255,255) #FFFFFFFF white
1: (255,255,255,252) #FFFFFFFC srgba(255,255,255,0.988235)


+++++++++++

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

Re: Changing a single pixel to be slightly transparent

Posted: 2018-07-26T17:23:40-07:00
by AkemiHomura
Sorry, Windows 10, IM7. That command does produce the behavior I'm looking for though, thanks!

Rewritten to Windows format:

Code: Select all

magick in.png ^
( +clone -crop 1x1+0+0 +repage -alpha on -channel a -evaluate set 99% +channel ) ^
-alpha on -channel rgba -geometry +0+0 -compose copy -composite out.png

Re: Changing a single pixel to be slightly transparent

Posted: 2018-07-26T22:05:27-07:00
by GeeMack
AkemiHomura wrote: 2018-07-25T20:15:06-07:00A single pixel being 'only' 99% opaque counts as transparency. [...] I can safely assume images have no transparency yet as I can detect images which do and upload as-is.
If you know for sure the pixel at "0,0" is not already transparent, it can be as simple as this...

Code: Select all

magick in.png xc:gray95 -background none -compose copyopacity -composite out.png
That reads the input image, creates a 1x1 canvas 95% white, and composites it at 0,0 onto the input image. Using the compose method "copyopacity" should result in that top left pixel being 95% opaque, or 5% transparent, while maintaining its original color.

Note: If you composite that "xc:gray95" onto an area already transparent, it will result in an almost black pixel. You probably don't want that.

Re: Changing a single pixel to be slightly transparent

Posted: 2018-07-27T07:17:56-07:00
by snibgo
We can extend GeeMack's solution to cover cases where the input may already have transparency:

Code: Select all

magick in.png ( +clone -crop 1x1+0+0 -alpha extract -evaluate Multiply 0.99 ) -compose CopyOpacity -composite out.png
A simpler command that does the same thing is:

Code: Select all

magick in.png -alpha Set -region 1x1+0+0 -channel A -evaluate Multiply 0.99 +channel +region out.png