Convert 4-band image to single band with colorpalette

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
selphiron
Posts: 2
Joined: 2015-03-24T08:09:00-07:00
Authentication code: 6789

Convert 4-band image to single band with colorpalette

Post by selphiron »

Hi,
I use Windows 7 (64 bit) and ImageMagick 6.9.0-Q16
I have a white PNG and gdalinfo says this about it:

Code: Select all

D:\>gdalinfo J1.png
Driver: PNG/Portable Network Graphics
Files: J1.png
Size is 4950, 4090
Coordinate System is `'
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 4090.0)
Upper Right ( 4950.0,    0.0)
Lower Right ( 4950.0, 4090.0)
Center      ( 2475.0, 2045.0)
Band 1 Block=4950x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=4950x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=4950x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=4950x1 Type=Byte, ColorInterp=Alpha
I try this to convert it to single band:

Code: Select all

D:\>convert J1.png -type palette -colors 256 J11.png
and when I try gdalinfo on the output (J11.png) I have a single battle but the -type palette -colors 256 is being ignored (because it says ColorInterp=Gray)

Code: Select all

D:\>gdalinfo J11.png
Driver: PNG/Portable Network Graphics
Files: J11.png
Size is 4950, 4090
Coordinate System is `'
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 4090.0)
Upper Right ( 4950.0,    0.0)
Lower Right ( 4950.0, 4090.0)
Center      ( 2475.0, 2045.0)
Band 1 Block=4950x1 Type=Byte, ColorInterp=Gray]
  Image Structure Metadata:
    NBITS=1
I want the output to be like this (notice ColorInterp=Palette and Color Table under NBITS=1):

Code: Select all

D:\>gdalinfo Row_A.png
Driver: PNG/Portable Network Graphics
Files: Row_A.png
Size is 49500, 4090
Coordinate System is `'
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 4090.0)
Upper Right (49500.0,    0.0)
Lower Right (49500.0, 4090.0)
Center      (24750.0, 2045.0)
Band 1 Block=49500x1 Type=Byte, ColorInterp=Palette
  Image Structure Metadata:
    NBITS=1
  Color Table (RGB with 2 entries)
    0: 255,255,255,255
    1: 255,255,165,255
When I append several of "J1.pngs" together I have the output that I want but I dont want to append anything ^^

Edit: Btw here is the image to play with:http://i.imgur.com/PupYAYn.png
Last edited by selphiron on 2015-03-24T10:25:37-07:00, edited 1 time in total.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert 4-band image to single band with colorpalette

Post by glennrp »

Use -colors 255 instead of -colors 256, to leave room in the palette for the background color. Also, use png8:J11.png to force output.png to be indexed/paletted.
selphiron
Posts: 2
Joined: 2015-03-24T08:09:00-07:00
Authentication code: 6789

Re: Convert 4-band image to single band with colorpalette

Post by selphiron »

Thx for the answer I should add that my picture is all white. But I still need that as a color palette.. Is that possible?
Post Reply