Convert grayscale image to black plus transparency?
Convert grayscale image to black plus transparency?
I'm trying to fiddle with the .jpg images used to decorate chapter headers and such in an epub book. They look fine with a white background, but if you want to change your default background color or use a pattern, they look horrible, turning into visually jarring rectangles on the page. What I'd like to do is turn them into solid black .png files with transparency set by the gray levels, so what was white would be 100% transparent and so on down to pixels that were black being 100% opaque. I'm sure this is possible, but I'm also sure it would take weeks for me to come up to speed on all the jargon I'd need to understand how to find the answer . I was hoping someone might have pity on me and just tell me the magic options I need on a convert command to do this? Thanks.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
Post a link to your input image and explain what you want it to look like afterwards.
Re: Convert grayscale image to black plus transparency?
Here's a fair use safe clip from the title page of my (legally purchased) Game Of Thrones ebook:
This is a completely opaque grayscale jpeg image.
What I want is the same image with transparency instead of whiteness so that if it is displayed on a totally white background it looks absolutely identical to the original, but if displayed on a (for instance) wheat background, it has wheatness instead of whiteness, hopefully that makes sense .
I tried this code sequence:
It sorta works, but the very light background pattern gets all washed out.
This is a completely opaque grayscale jpeg image.
What I want is the same image with transparency instead of whiteness so that if it is displayed on a totally white background it looks absolutely identical to the original, but if displayed on a (for instance) wheat background, it has wheatness instead of whiteness, hopefully that makes sense .
I tried this code sequence:
Code: Select all
convert image00008.jpeg -negate -alpha copy mask.png
convert image00008.jpeg -fill black mask.png -compose Copy_Opacity -composite new.png
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
try one of these. The first makes that appropriate transparent image. The second skips the transparent image and just creates the wheat colored output.
1)
convert image.jpg -alpha copy -channel A -negate +channel image.png
convert image.png -background wheat -flatten image_wheat.jpg
2)
convert image.jpg +level-colors black,wheat image_wheat2.jpg
see
http://www.imagemagick.org/Usage/masking/#alpha
http://www.imagemagick.org/Usage/color_ ... vel-colors
1)
convert image.jpg -alpha copy -channel A -negate +channel image.png
convert image.png -background wheat -flatten image_wheat.jpg
2)
convert image.jpg +level-colors black,wheat image_wheat2.jpg
see
http://www.imagemagick.org/Usage/masking/#alpha
http://www.imagemagick.org/Usage/color_ ... vel-colors
Re: Convert grayscale image to black plus transparency?
Technique 2 works perfectly, but unfortunately I can't use it because the background color is a preference set in the book reader long after the book is created.
Technique 1 produces the same disappearing light background problem my original attempt had. For instance, forgetting wheat for a moment, and just filling in a white background to see if it looks the same as the original, it is all washed out:
Surely there is some way to ask the question of each pixel, "What transparency level would produce this value gray with a white background?" That's the alpha mask I'm looking for .
Technique 1 produces the same disappearing light background problem my original attempt had. For instance, forgetting wheat for a moment, and just filling in a white background to see if it looks the same as the original, it is all washed out:
Surely there is some way to ask the question of each pixel, "What transparency level would produce this value gray with a white background?" That's the alpha mask I'm looking for .
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
I think you may not be using it right.
Last edited by fmw42 on 2012-08-17T14:34:37-07:00, edited 1 time in total.
Re: Convert grayscale image to black plus transparency?
I have discovered that the color to alpha function in gimp does exactly what I want. See the bottom of this web page:
http://home.comcast.net/~tomhorsley/game/calibre.html
http://home.comcast.net/~tomhorsley/game/calibre.html
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
This works for me by making a mask of the image and compose multiply with the image.
convert sample-gray.jpeg \( -clone 0 -fill wheat -colorize 100% \) \( -clone 0 \) \
-compose multiply -composite sample-gray-wheat.jpg
and so does the following, which is probably what you want
convert sample-gray.jpeg -alpha copy -channel A -negate +channel sample-gray.png
convert sample-gray.png \( -clone 0 -fill wheat -colorize 100% \) \
-channel rgba -alpha on -compose multiply -composite sample-gray-wheat.jpg
The trick in the latter is -channel rgba -alpha on
I am surprised that the following does not work.
convert sample-gray.jpeg -alpha copy -colorspace RGB -channel A -negate +channel sample-gray.png
convert sample-gray.png -channel rgba -alpha on -background wheat -compose multiply -flatten sample-gray-wheat.jpg
convert sample-gray.jpeg \( -clone 0 -fill wheat -colorize 100% \) \( -clone 0 \) \
-compose multiply -composite sample-gray-wheat.jpg
and so does the following, which is probably what you want
convert sample-gray.jpeg -alpha copy -channel A -negate +channel sample-gray.png
convert sample-gray.png \( -clone 0 -fill wheat -colorize 100% \) \
-channel rgba -alpha on -compose multiply -composite sample-gray-wheat.jpg
The trick in the latter is -channel rgba -alpha on
I am surprised that the following does not work.
convert sample-gray.jpeg -alpha copy -colorspace RGB -channel A -negate +channel sample-gray.png
convert sample-gray.png -channel rgba -alpha on -background wheat -compose multiply -flatten sample-gray-wheat.jpg
Last edited by fmw42 on 2012-08-18T07:33:35-07:00, edited 1 time in total.
Re: Convert grayscale image to black plus transparency?
I can't decrypt that, but it looks like the "probably what I want" is merging the wheat background in, and that is most definitely not what I want. Wheat is just one example, anyone could select any background they want in their ebook reader. I want to stop the process where I can capture a .png file that has transparency which can later be displayed with pretty much any background color. The gimp "color to alpha" examples on my web page above does that perfectly.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
The first line creates the PNG file with transparency. It simply copies the image into the alpha channel and negates itClaghorn wrote:I can't decrypt that, but it looks like the "probably what I want" is merging the wheat background in, and that is most definitely not what I want. Wheat is just one example, anyone could select any background they want in their ebook reader. I want to stop the process where I can capture a .png file that has transparency which can later be displayed with pretty much any background color. The gimp "color to alpha" examples on my web page above does that perfectly.
convert sample-gray.jpeg -alpha copy -channel A -negate +channel sample-gray.png
The second line shows how to put it over some colored background and merge the color in.
convert sample-gray.png \( -clone 0 -fill wheat -colorize 100% \) \
-channel rgba -alpha on -compose multiply -composite sample-gray-wheat.jpg
The compose multiply is what is needed to avoid the earlier problem.
Re: Convert grayscale image to black plus transparency?
That's still not working like gimp. I don't have the original image to compose with in an ebook, I've just got a single .png file with transparency which will wind up being displayed on a solid background. If you look at my updated web page with all the gimp examples, you'll see exactly what I want. I even managed to figure out how to run gimp in batch mode now, so I can automate it in a script without IM, but I'm still curious if IM can generate the same .png files from the original .jpeg like gimp can do:
http://home.comcast.net/~tomhorsley/gam ... tml#images
All the examples in there are the same .png file being displayed in different table cells with different background colors. All the fine detail is retained exactly the way I want. In fact, I wonder if it will work on the background in this forum? Let's find out:
Looks like it does .
http://home.comcast.net/~tomhorsley/gam ... tml#images
All the examples in there are the same .png file being displayed in different table cells with different background colors. All the fine detail is retained exactly the way I want. In fact, I wonder if it will work on the background in this forum? Let's find out:
Looks like it does .
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
try this
convert sample-gray.jpeg -alpha copy -channel A -negate +channel -channel rgb -evaluate set 0 -channel rgba -alpha on PNG32:sample-gray.png
OK. There are three issues.
First, Gimp is making the underlying image totally black. So I have tried to reproduce that using -evaluate set 0 for the rgb channels.
Second, IM does not support 8-bit palette png with 8-bit alpha channel (which gimp seems to support). IM only allows binary alpha channel in png palette format (i.e. 8-bit pseudocolor with 1 bit alpha)
Third, when using the above command to use 32-bit PNG which does support 8-bit alpha, the number of colors in the alpha is reduced from 256 to 183. I believe this may be a bug in IM. Furthermore, the image is classified as bilevel. It would seem that this overrides the 8-bit alpha and so the result seems not to blend the middle gray shades in the alpha with the background.
convert sample-gray.jpeg -alpha copy -channel A -negate +channel -channel rgb -evaluate set 0 -channel rgba -alpha on PNG32:sample-gray.png
OK. There are three issues.
First, Gimp is making the underlying image totally black. So I have tried to reproduce that using -evaluate set 0 for the rgb channels.
Second, IM does not support 8-bit palette png with 8-bit alpha channel (which gimp seems to support). IM only allows binary alpha channel in png palette format (i.e. 8-bit pseudocolor with 1 bit alpha)
Third, when using the above command to use 32-bit PNG which does support 8-bit alpha, the number of colors in the alpha is reduced from 256 to 183. I believe this may be a bug in IM. Furthermore, the image is classified as bilevel. It would seem that this overrides the 8-bit alpha and so the result seems not to blend the middle gray shades in the alpha with the background.
Re: Convert grayscale image to black plus transparency?
Thanks for the analysis. That does appear to finally get closer to gimp, but I guess I'll stick with gimp for now since the images I get with it look more like the original (maybe the 256 versus 183 issue).
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
I have reported it. We will see what the developers think.Claghorn wrote:Thanks for the analysis. That does appear to finally get closer to gimp, but I guess I'll stick with gimp for now since the images I get with it look more like the original (maybe the 256 versus 183 issue).
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert grayscale image to black plus transparency?
Unfortunately, this about the same and still does not reproduce the gimp result. It also reduces the number of gray values in the alpha channel from 256 to 183.
convert sample-gray.jpeg \
\( -clone 0 -fill black -colorize 100% \) \
\( -clone 0 -negate \) \
-delete 0 -alpha off -compose copy_opacity -composite PNG32:sample_gray.png
convert sample-gray.jpeg \
\( -clone 0 -fill black -colorize 100% \) \
\( -clone 0 -negate \) \
-delete 0 -alpha off -compose copy_opacity -composite PNG32:sample_gray.png