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

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
thejas
Posts: 4
Joined: 2017-04-05T02:05:40-07:00
Authentication code: 1151

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

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

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

Post 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
thejas
Posts: 4
Joined: 2017-04-05T02:05:40-07:00
Authentication code: 1151

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

Post 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.
thejas
Posts: 4
Joined: 2017-04-05T02:05:40-07:00
Authentication code: 1151

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

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

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

Post 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
snibgo's IM pages: im.snibgo.com
thejas
Posts: 4
Joined: 2017-04-05T02:05:40-07:00
Authentication code: 1151

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

Post by thejas »

Thank you Mr. Gibson, that is a nice technique. It works perfectly. I reviewed the various -compose options after your explanation.
Post Reply