Changing a single pixel to be slightly 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
AkemiHomura
Posts: 2
Joined: 2018-07-25T20:08:58-07:00
Authentication code: 1152

Changing a single pixel to be slightly transparent

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

Re: Changing a single pixel to be slightly transparent

Post 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
AkemiHomura
Posts: 2
Joined: 2018-07-25T20:08:58-07:00
Authentication code: 1152

Re: Changing a single pixel to be slightly transparent

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Changing a single pixel to be slightly transparent

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

Re: Changing a single pixel to be slightly transparent

Post 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
snibgo's IM pages: im.snibgo.com
Post Reply