Page 1 of 1

How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-05T02:42:34-07:00
by thejas
I am trying to make an image with rounded corners and then add a shadow effect like the one below. (please right click and open image in new tab in case the images don't populate automatically below; they are being streamed from Google drive):

Image

I used a plain jpg file which worked well since there is no transparency. However, for a png file with transparency the result shows the left over black arcs at the corners like here:

Image

The source file I used for the above is here:

Image

The script that I am using so far is as follows:

Code: Select all

convert garfield.png \
\( +clone -alpha extract \
	\( -size 15x15 xc:black -draw 'fill white circle 15,15 15,0' -write mpr:arc +delete \) \
	\( mpr:arc \) -gravity northwest -composite \
	\( mpr:arc -flip \) -gravity southwest -composite \
	\( mpr:arc -flop \) -gravity northeast -composite \
	\( mpr:arc -rotate 180 \) -gravity southeast -composite \
\) -alpha off -compose CopyOpacity -composite \
-compose over \( +clone -background black -shadow 80x3+5+5 \) \
+swap -background none -layers merge output.png
Can anyone please help me by suggesting how to remove those black arcs in the corners in the case of png files?

[Moderator note:

Your images don't show as images for me, and are not clickable. The URLs are:

https://drive.google.com/file/d/0B69e4- ... sp=sharing
https://drive.google.com/file/d/0B69e4- ... sp=sharing
https://drive.google.com/file/d/0B69e4- ... sp=sharing
]

Re: How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-05T09:28:48-07:00
by fmw42
For images with transparency, you need to approach is differently, if I understand what you want. Try

Code: Select all

convert garfield.png \( +clone -background black -shadow 80x3+5+5 \) \
+swap -background none -layers merge output.png

Re: How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-05T20:47:15-07:00
by thejas
Thank you Mr. Weinhaus, that works well. It's really nice seeing a reply from one of the pioneers of image processing. Most of my image processing skills have been gleaned through your writings and of course Anthony Thyssen's. Just wondering whether it would be possible for you and Mr. Thyssen to create a video tutorial series, like a course on Coursera, to share your knowledge with generations to come.

Re: How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-06T00:46:50-07:00
by thejas
I have a related question to make a round image of a png. I get the following output:

Image

The command line code I use is:

Code: Select all

convert garfield.png -resize 150x150^ -gravity Center -crop 150x150+0+0 +repage \
\( +clone -threshold -1 -negate -fill white -draw "circle 75,75 148,75" \) \
-alpha off -compose copy_opacity -composite +repage output.png 
Is there are way of re-introducing the original transparency in the output to remove the black background?

Re: How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-06T01:17:09-07:00
by snibgo
There are various ways to do this. My preference is to process alpha as a greyscale image, and CopyOpacity at the end, just as you have done.

Here, the opacity comes from two sources: extracted from garfield.png, and a white circle on black background. At each pixel, we take the darker (smallest opacity) of these two.

Instead of "Darken", we could "Multiply". This would give a reduced opacity where both input opacities are partially transparent.

Windows BAT syntax. Adjust for bash.

Code: Select all

convert ^
  garfield.png ^
  -resize 150x150^^ -gravity Center -crop 150x150+0+0 +repage ^
  ( +clone ^
    -alpha extract ^
    ( +clone ^
      -fill Black -colorize 100 ^
      -fill White -draw "circle 75,75 148,75" ) ^
    -compose Darken -composite ^
  ) ^
  -alpha off -compose copy_opacity -composite ^
  out.png

Re: How do I remove black background areas after using -compose CopyOpacity operation for PNG file?

Posted: 2017-04-06T03:01:23-07:00
by thejas
Thank you Mr. Gibson, that is a nice technique. It works perfectly. I reviewed the various -compose options after your explanation.