1

As the title suggests I have a PNG image that has some transparency. I'd like to fill that transparency with a second image (which is currently a JPEG, but it's not a problem to convert it to a PNG).

Every post I have found searching on the Internet was about the "inverse" problem (from an image with a background to an image with transparency), so obviously it did not work out for my situation; for example, I tried

convert -flatten myimg.png myimg.png

(taken from here) and

convert myimg1.png  -transparent white myimg.png

(taken from here).

fmw42
  • 46,825
  • 10
  • 62
  • 80
VeryMina
  • 13
  • 3
  • Hi, welcome to stack overflow. Please refer the [ask] link for more details on how to ask a question and update your question accordingly. Post a minimal code example for example. – Jeroen Heier May 28 '19 at 17:06

1 Answers1

0

In ImageMagick 6, if the two images are the same size, then you can just flatten the transparent image over the background image.

Background (lena.jpg):

enter image description here

Transparent (logo_crop_trans.png):

enter image description here

convert lena.jpg logo_crop_trans.png -flatten lena_logo.jpg


enter image description here

If using ImageMagick 7, then change convert to magick.

If you want to anti-alias the transparent image so that it is not so jagged, then use some blur to smooth the outline (Unix syntax):

convert lena.jpg \( logo_crop_trans.png -channel a -blur 0x1 -level 50x100% +channel \) -compose over -composite lena_logo2.jpg


enter image description here

If on Windows remove the \ before the parentheses.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • This is the basic idea, but the "transparent" part of the png is not the background, and moreover the two images hasn't the same size – VeryMina May 28 '19 at 17:47
  • Sorry, I do not understand your command about the transparent part of the png not being the background. My background came from the lena image. The transparent image is the foreground which is place over the opaque lena image so that lena shows through. Please post links to your input images so that we can understand what you want to do and use them to solve your issue. You do not have access to post directly. So post to some free image hosting service – fmw42 May 28 '19 at 19:32