1

Problem

I’d like to flatten only a part of an image.

Image Example

Take for example this image:

enter image description here

It has transparent background, which I’d like to keep (rather than re-create), also the transparency between the cable casing and the machine (the casing is on the top of the machine). All other transparent parts of the image (i.e. the transparency in the machine itself) should be flatten.

What I Already Know

I’ve seen this question, but it deals only with not-fully-transparent pixels, however, I’d like to flaten all non-fully-opaque pixels in a part of a image.

I use this command to flatten whole image:

convert -flatten input.png output.png
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
tukusejssirs
  • 564
  • 1
  • 7
  • 29

1 Answers1

1

I don't really understand what you are trying to do, maybe you could add another image showing the expected result please?

In the meanwhile, you can extract the alpha/transparency layer into a separate image that you can play with to you heart's content like this:

convert input.png -alpha extract alpha.png

enter image description here

and then merge back into the image with a command like this (but where I create a magenta backdrop instead of using the original image):

convert -size 800x600 xc:magenta alpha.png -compose copyalpha -composite result.png

enter image description here

Fill the outer background with cyan like this to keep it as a temporary colour we can find again later:

convert alpha.png -fill cyan -draw 'color 0,0 floodfill' a.png

enter image description here

Now make everything that is not cyan into white, then everything that is cyan back into black:

convert a.png -fuzz 80% -fill white -opaque black -fuzz 5% -fill black -opaque cyan b.png

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you, @MarkSetchell. All I want do have is an image with transparent background only. The _thing_ (is in example: the machine) should not be transparent at all in any pixel of the _thing_. I’d like to do it in terminal only, as I’d like to automate the job on user-input image. – tukusejssirs Jun 15 '19 at 11:39
  • In this particular image, I’d like to remove the transparency from the two rectangles that apear in both of your images. – tukusejssirs Jun 15 '19 at 11:41
  • So am I correct if I re-state your question as... you'd like to flood-fill all *"holes"* in the alpha layer that would not be flood-filled if you started flood-filling the background from the top-left corner? – Mark Setchell Jun 15 '19 at 11:50
  • Most-probably yes, I am not certain of the ‘flood-fill’ word meaning. I’d like to flatten all pixels in those ‘holes’. – tukusejssirs Jun 15 '19 at 11:56
  • I have to run but have put in the outline so you can get started. – Mark Setchell Jun 15 '19 at 12:10
  • While trying merge the two images, however, I get the following error: `convert: unrecognized compose operator \`copyalpha' @ error/convert.c/ConvertImageCommand/1036.` – tukusejssirs Jun 15 '19 at 12:41
  • I tried to merge alpha channel from z.png to original image using the following command ([src](http://www.imagemagick.org/discourse-server/viewtopic.php?t=30341#p136925)): `convert orig.png z.png -alpha off -compose CopyOpacity -composite output.png`, however it does not look good ([output.png](https://imgur.com/a/bkynolP)). – tukusejssirs Jun 15 '19 at 12:46
  • Back home now. I updated and improved it, so have another look please. If it is jagged, you can blur the alpha mask a bit also (with `-blur 0x3`), or expand it, or contract it (with `-morphology dilate disk:2`). – Mark Setchell Jun 15 '19 at 15:35
  • I think I was not clear enough: I don’t want to have black background and white machine. :) … I want the image of machine as it is on the original image with transparent background, however, without any alpha colour _in_ the image of the machine. … … So, while the final image of yours might be a step towards my goal, there needs to be yet another command to accomplish this. – tukusejssirs Jun 15 '19 at 18:01
  • You need to take your original image, turn off its alpha, and then composite the new alpha in its place... `convert original.png -alpha off newalpha.png -compose copyopacity -composite result.png`. – Mark Setchell Jun 15 '19 at 21:25
  • Indeed, that works, however, there are two things (first, see the [image](https://imgur.com/a/4JDnjFA), original image on the left, result.png on the right): (1) I’d like to keep the transparency there where I marked it on the original image; (2) on the result image, there is some white which was not in the original image. Is there a way to fix these two issues? :) – tukusejssirs Jun 16 '19 at 10:37