Convert image and mask to PNG

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
oceanthrsty
Posts: 9
Joined: 2014-12-17T15:00:32-07:00
Authentication code: 6789

Convert image and mask to PNG

Post by oceanthrsty »

I have an image
Image

And it's mask
Image

Here's the command I'm using right now:

Code: Select all

convert main.jpg \( mask.jpg -colorspace gray -alpha off \) \ -compose  copy-opacity -composite new.png
It produces a great PNG... but it's in grayscale :(

How can I get it in color.... and is there anyway to make it process faster?

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert image and mask to PNG

Post by snibgo »

It works fine for me, IM v6.9.0-9, red letters on a transparent background.

What version IM are you using? Perhaps an old one, and you need to upgrade.

You don't need "-colorspace gray -alpha off".

Using miff instead of jpg for input may be faster. Png is slow to write; one of the compression options may make it faster.
snibgo's IM pages: im.snibgo.com
oceanthrsty
Posts: 9
Joined: 2014-12-17T15:00:32-07:00
Authentication code: 6789

Re: Convert image and mask to PNG

Post by oceanthrsty »

What's the exact syntax you're using?

I upgraded my version I'm running the same syntax as above and I'm getting an error message

Code: Select all

convert main.jpg \( mask.jpg -colorspace gray -alpha off \) \ -compose  copy-opacity -composite new.png

convert: unable to open image ` -compose': No such file or directory @ error/blob.c/OpenBlob/2709.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `copy-opacity': No such file or directory @ error/blob.c/OpenBlob/2709.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
I get a new.png exported. But it looks exactly like the mask.jpg :?

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert image and mask to PNG

Post by snibgo »

oceanthrsty wrote:I upgraded my version
What version are you now using? "convert -version"

My commands (Windows BAT syntax):

Code: Select all

%IM%convert ^
  -verbose ^
  main.jpg ^
  ( mask.jpg -colorspace gray -alpha off ) ^
  -compose copy-opacity -composite ^
  new.png

%IM%convert ^
  -verbose ^
  main.jpg ^
  mask.jpg ^
  -compose copy-opacity -composite ^
  new2.png

%IM%convert main.jpg main.miff
%IM%convert mask.jpg mask.miff

%IM%convert ^
  -verbose ^
  main.miff ^
  mask.miff ^
  -compose copy-opacity -composite ^
  new3.png
All three results have red letters on transparent background.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert image and mask to PNG

Post by fmw42 »

try breaking the line at the \ or remove it. You cannot have a space after a new line \. Also add -alpha off before -compose

Code: Select all

convert main.jpg mask.jpg \
-alpha off -compose  copy-opacity -composite new.png
or

Code: Select all

convert main.jpg  mask.jpg -alpha off -compose  copy-opacity -composite new.png

If on Windows, the new line character is ^ rather than \. Also Windows does not need to escape the parenthesis and escape is ^. I assume above that you are not on Windows.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert image and mask to PNG

Post by snibgo »

Well spotted, Fred. The OP's command seems to be a single line with a spurious backslash, like this (bash syntax):

Code: Select all

$ convert xc: xc: \ -composite x.png
convert: unable to open image ` -composite': No such file or directory @ error/blob.c/OpenBlob/2709.
snibgo's IM pages: im.snibgo.com
oceanthrsty
Posts: 9
Joined: 2014-12-17T15:00:32-07:00
Authentication code: 6789

Re: Convert image and mask to PNG

Post by oceanthrsty »

Fred,

Thank you. That works!
Post Reply