-transparent-color query

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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

-transparent-color query

Post by Bonzo »

I am creating an image with some resizes, composites and rotates and saving as a png with a transparent background and all is going well.

This code is going onto a website for users to upload their own images and I am thinking there is bound to be a problem as some of the users will download the new image and save as a jpg at some point and so I want to set the transparent colour to white instead of the default black.

Simple I thought just add a -transparent-color white but it does not seem to work no matter where or how many times I put it into the code.

My actual code is quite complicated and has a lot of variables in and so is difficault to post here - what is the proper usage of -transparent-color?

It is a bit hard writing some test code when nothing happens as expected.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -transparent-color query

Post by snibgo »

I think (but could be wrong) that "-transparent-color" applies only to paletted images. JPEG files are never paletted.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -transparent-color query

Post by Bonzo »

Thanks for the reply; I was hoping to find a way to specify the colour jpg would use to replace transparency.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -transparent-color query

Post by snibgo »

I think the conversion to jpg simply removes the alpha channel, so the colour will be the underlying colour of those pixels. "-background XX -alpha background" will turn fully transparent pixels into transparent-XX, where XX is any colour.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -transparent-color query

Post by Bonzo »

Thanks snibgo but that did not seem to have any effect - I suppose it depends where it is in the options list?

This has brought up another problem; when looking at my produced image in photoshop the image is not fixed onto the background.

Code: Select all

convert image1.png image2.png -gravity center -geometry -5-106 -composite output.png
The image2 is fixed to the image1 correctly but the combined image 1&2 is not fixed to the transparent background; I would say I have two layers.
Using -flatten turns my transparency black on my combined image 1&2 but it is still not fixing my combined image 1&2 to the transparent background.

Does this matter? I would not think so but I would like to fix it if I could.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -transparent-color query

Post by snibgo »

Code: Select all

convert -size 200x200 gradient:red-blue -alpha transparent t.png

convert t.png t1.jpg

convert t.png -background Green -alpha background t2.jpg
t.png has 4 channels. The colour channels have values from red to blue, but the alpha channel is fully transparent.

t1.jpg strips out the alpha channel, so it shows pixels that vary from red to blue.

The third command changes all of the fully-transparent pixels, making them transparent-green. Saving it as jpg removes the alpha channel, so the result is green.


I don't understand "the combined image 1&2 is not fixed to the transparent background".
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -transparent-color query

Post by Bonzo »

Right I get :
t.png completely transparent
t1.jpg Red to blue gradient
t2.jp completely green
Which is as expected; thanks for the examples. I will have another play with my code later and see what I get.

This image was created with the code I posted above and if you open it in photoshop and probably Gimp you will see that the easel ( image1.png )and the picture ( image2.png ) are combined but they are floating over a transparent background. Not anchored or merged?
In photoshop if I "deselect layers" they are fixed as one image. Sorry to keep using Photoshop but that is all I currently have installed.
http://www.rubble.info/donotdelete/1_1415653499_CVH.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -transparent-color query

Post by snibgo »

The image looks normal to me in Gimp. The image is 200x384 pixels, but "identify -verbose" shows fields:

Code: Select all

    Thumb::Image::Height: 1564
    Thumb::Image::Width: 815
Perhaps Photoshop interprets this as a canvas larger than the image.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -transparent-color query

Post by Bonzo »

Good point - the image is bounded by a "box" and that is smaller than the "canvas". If I drag the image around it disappears off the edge of the canvas and where ever I deselect the layers the image stays. I had assumed it was a general image problem, but it could just be something photoshop does or interoperates from the image data. I will not worry about it; thanks again for the help.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -transparent-color query

Post by Bonzo »

I had a brainwave this morning about the canvas anomaly; my version of Photoshop has an option to delete pixels after cropping or retain. I have my option set to retain and I assume that was causing that error.
Post Reply