1

I need to extract an image from a PDF.

But since its background is transparent it's saved with a soft mask to create transparency.

While I'm able to extract the two opaque images below, I have no idea if there's a way to recreate the original image in Java. Could I use JAI?

Below there's the "main" image:

img.jpg

and here there's the corresponding soft mask:

mask.jpg

I tried with ImageMagick with the command:

convert mask.jpg -background Black -alpha shape copy.png

and I almost got what I need but the foreground color is only black while the original one has also blue as foreground color.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
alessmar
  • 4,689
  • 7
  • 43
  • 52

1 Answers1

3

I think you probably want something like this, but it is hard to tell as you do not show us your expected result:

convert -fuzz 20% main.jpg -transparent black   \
     mask.jpg -compose copy-opacity -composite  \
     result.png

enter image description here

The -fuzz allows nearly black pixels in main.jpg to become transparent. The mask image is then loaded and its opacity copied to the main image.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    Wonderful! It's exactly what I need, but is there a Java equivalent? – alessmar Feb 18 '15 at 09:52
  • 1
    Sorry, I don't speak Java - I assumed you were happy with the command-line interface too, since you showed what you had been trying there. Maybe you could `shell exec` the ImageMagick command I gave, or have a look here http://stackoverflow.com/questions/19296075/how-would-i-use-the-imagemagick-library-with-processing – Mark Setchell Feb 18 '15 at 10:43