force color PNGs

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
check227
Posts: 5
Joined: 2010-05-11T09:10:58-07:00
Authentication code: 8675308

force color PNGs

Post by check227 »

Howdy,

First let me say, I've already found and reviewed the posts: Trouble forcing TrueColorMatte and -type command not working
However, I'm still having the same issue where images with no color content are being saved as grayscale. I can use the IM command line tools to force PNGs to save as TrueColor however I'm using a python api for MagickWand. Its my own variation of the project here.

My code is preforming the following steps:

Code: Select all

wand=api.NewMagickWand()
api.MagickReadImage(wand, "allGrayImage.png")
api.MagickSetImageFormat(wand, "png24")
api.MagickSetImageDepth(wand, 8)
api.MagickSetImageType(wand, TRUE_COLOR_TYPE)
print api.MagickGetImageType(wand)
api.MagickWriteImage(wand, "pleaseBeColor.png")
The value of TRUE_COLOR_TYPE is 6, however the value that is printed is 2 for GrayscaleType. Likewise the saved image's colorspace is grayscale as reported by KDE file properties, Krita and - most importantly - ffmpeg which doesn't seem to like grayscale PNG images. Is there something I'm missing here? Thanks.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: force color PNGs

Post by anthony »

Assuming I understand your needs....

Many times IM will try to determine the best type when saving the image. However their are controls that are passed to the coder to force it to save to specific types rather than letting it 'guess'.
See http://www.imagemagick.org/Usage/formats/#png_write
for some of these controls.

The full details of the settings/defines are in the coder/png.c source.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
check227
Posts: 5
Joined: 2010-05-11T09:10:58-07:00
Authentication code: 8675308

Re: force color PNGs

Post by check227 »

Okay thanks very much I've got it working. The piece I was ignorant of was the MagickSetOption method. Now my code does the following:

Code: Select all

wand=api.NewMagickWand()
api.MagickReadImage(wand, "allGrayImage.png")
api.MagickSetImageType(wand, TRUE_COLOR_TYPE)
api.MagickSetOption(wand, "png:color-type", 6) # or 2 for RGB no alpha
api.MagickWriteImage(wand, "nowColorImage.png")
Thanks again.
Post Reply