ICO file with PNG data - imagemagick

Looking at some ICO files that have PNG data instead of ICO and I am trying to create the same. Example:
$ identify sample.ico
sample.ico[0] PNG 32x32 32x32+0+0 8-bit sRGB 884B 0.000u 0:00.000
sample.ico[1] PNG 16x16 16x16+0+0 8-bit sRGB 884B 0.000u 0:00.000
$ file sample.ico
sample.ico: MS Windows icon resource - 2 icons,
32x32, 16 colors withPNG image data, 32 x 32, 8-bit/color RGBA, non-interlaced, 4 bits/pixel,
16x16, 16 colors withPNG image data, 16 x 16, 8-bit/color RGBA, non-interlaced, 4 bits/pixel
Playing around I have found I can do something like this to get PNG ICO:
$ convert icon-32x32.png png8:test1.ico
$ identify test1.ico
test1.ico PNG 32x32 32x32+0+0 8-bit sRGB 934B 0.000u 0:00.000
But if I use multiple input files I get multiple output files:
$ convert icon-16x16.png icon-32x32.png png8:test2.ico
$ ls -1 test2*
test2-0.ico
test2-1.ico
Not prefixing with png8: converts the data to ICO.
$ convert icon-16x16.png icon-32x32.png test3.ico
$ identify test3.ico
test3.ico[0] ICO 16x16 16x16+0+0 32-bit sRGB 5.43KB 0.000u 0:00.000
test3.ico[1] ICO 32x32 32x32+0+0 32-bit sRGB 5.43KB 0.000u 0:00.000
If the file is too big for the ICO format, the PNG is "preserved":
$ convert ../icon-32x32.png ../icon-512x512.png test4.ico
$ identify test4.ico
test3.ico[0] ICO 32x32 32x32+0+0 32-bit sRGB 53.4KB 0.000u 0:00.000
test3.ico[1] PNG 512x512 512x512+0+0 8-bit sRGB 53.4KB 0.000u 0:00.000
How can I create a ICO file with PNG data for all sizes in ImageMagick?

Have not found any solution with imagemagick but one can use GIMP.
Just create a layer for each size and export as file.ico.
In options dialogue that appear check off Compress (PNG).
One can also set bpp etc. For lower resolutions one can use for example:
16x16 4bpp, 1-bit alpha, 16-slots palette
24x24 4bpp, 1-bit alpha, 16-slots palette
32x32 8bpp, 1-bit alpha, 256-slots palette
48x48 ...
…

Related

Create pattern and save it to 24-bit RGB PNG file

How can I create PNG file with ImageMagick pattern (e. g. input file pattern:gray50) and save it to 24-bit RGB PNG file using ImageMagick commandline?
This produces 8-bit PNG file:
convert -size 64x64 pattern:gray50 output.png
In ImageMagick, you can force the output PNG to 24-bits by prefacing the output with PNG24. Try
convert -size 64x64 pattern:gray50 PNG24:output.png

How to convert PNG images (specifically grayscale ones) to Indexed color

I'm trying to convert some small PNG images from 32-bit color mode to indexed color mode.
For color images, I ran the command convert IMGS/FLAME.png INDEXED_IMGS/FLAME.png and it converted fine. For an image that had only grayscale colors, I ran that same command (with the filename changed obviously) but I got a warning:
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG 'INDEXED_IMGS/SHADOW.png' # warning/png.c/MagickPNGWarningHandler/1748.
I ran file IMGS/*.pngand got
IMGS/FLAME.png: PNG image data, 16 x 16, 8-bit/color RGBA, non-interlaced
IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit/color RGBA, non-interlaced
which is expected; both images are in 8-bit RGBA mode (since that's the mode I created them in Photoshop). However, when I run file INDEXED_IMGS/*.png I get
INDEXED_IMGS/FLAME.png: PNG image data, 16 x 16, 4-bit colormap, non-interlaced
INDEXED_IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit grayscale, non-interlaced
The 4-bit colormap part checks out, but the grayscale part does not.
So my question is: how can I convert a grayscale image to indexed mode? What really gets me is that it starts out in RGBA mode like the color image, but for some reason it converts automatically to grayscale mode. Is there a way to prevent it from doing that?
I should add that I have a bash script that looks like this:
#!/bin/bash
for img in IMGS/*.png; do
file=$(basename $img)
convert $img INDEXED_IMGS/$file
done
so I don't wanna manually distinguish between grayscale and colored images. If there's a way to do so automatically with some command that's fine though.
Here is info about my ImageMagick tool:
Version: ImageMagick 7.0.8-42 Q16 x86_64 2019-04-24 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
With ImageMagick, for 24-bit color, append the output with PNG8:output
convert input.png PNG8:output.png
PNG grayscale images do not support color profiles, so you get that warning. But the resulting image should be 8-bit palette.
If you have 32-bit color, then this needs more to be done. The color under the alpha channel must be one constant color and one not used elsewhere in the image. Find such a color after converting to 256 colors and set the color under the transparency to that color. For example if you have no opaque black in your image after converting to 256 colors, then set the alpha base color to black.
convert image.png +dither -colors 256 -background black -alpha background PNG8:output.png
You can get a list of unique colors from the image as follows:
convert image.png +dither -colors 256 -unique-colors txt:
Here is an example:
Make it 32-bit transparent:
convert rose.png -fuzz 20% -transparent red rose_trans32.png
identify -verbose rose_trans32.png
...
Colorspace: sRGB
Type: TrueColorAlpha
...
Convert to palette alpha:
convert rose_trans32.png -alpha off +dither -colors 256 -unique-colors txt:
List shows no black
convert rose_trans32.png +dither -colors 256 -background black -alpha background PNG8:rose_trans8.png
Or if you already know that the 32-bit version has not black, then just:
convert rose_trans32.png -background black -alpha background PNG8:rose_trans8.png
identify -verbose rose_trans8.png
...
Colorspace: sRGB
Type: PaletteAlpha
...
If you do this adding -colorspace gray, ImageMagick will still report as type grayscalealpha, since it recognizes it as a single channel image with transparency. But using EXIFTOOL, it will report 9 ColorType: 3, which is 3 = RGB Palette
NOTE: For ImageMagick 7, change convert to magick.

Convert with imagemagick a multi layers psd into a merged layer tiff without visible modification

I need to automatically convert PSD files with or without multiple layers into a TIFF LZW with only one layer, without noticeable differences.
Here is type of command I tried with Imagemagick 6.9.2-7 :
convert "fontaine_p7p.psd" -compress LZW -define tiff:endian=msb -background none -flatten fontaine_p7p.tif
Result is not as expected compare to the one obtained thanks to PhotoShop.
Layers are correctly merged, sizes of conversions are equivalent but I can see visible modifications on the Imagemagick tiff that are not visible on tiff generated thanks to PhotoShop.
Here are examples of a source PSD and the Tiff obtained via the imagemagick conversion above and where visible modifications are clearly iden :
PSD + TIFF + IdentifyVerbose
Here are details on PSD and TIFF files :
PSD :
identify fontaine_p7p.psd
fontaine_p7p.psd[0] PSD 782x819 782x819+0+0 8-bit CMYK 2.965MB 0.000u 0:00.000
fontaine_p7p.psd[1] PSD 544x819 544x819+226+0 8-bit CMYK 2.965MB 0.000u 0:00.000
fontaine_p7p.psd[2] PSD 335x819 335x819+447+0 8-bit CMYK 2.965MB 0.000u 0:00.000
fontaine_p7p.psd[3] PSD 782x13 782x13+0+806 8-bit CMYK 2.965MB 0.000u 0:00.000
fontaine_p7p.psd[4] PSD 782x819 782x819+0+0 8-bit CMYK 2.965MB 0.000u 0:00.000
fontaine_p7p.psd[5] PSD 782x819 782x819+0+0 8-bit CMYK 2.965MB 0.000u 0:00.000
TIFF :
identify fontaine_p7p.tif
fontaine_p7p.tif TIFF 782x819 782x819+0+0 8-bit CMYK 1.552MB 0.000u 0:00.000
It seems that imagemagick is not handling transparency is the same way as Photoshop during conversion to Tiff.
Is somebody have proposition to have a better result with such kind of procedure ? I already tried to replace the ("-flatten" option by "-layers merge" but in that case the transparency background is completely lost.
Thanks in advance

Imagemagick convert tif to rgba16 png

So here is my source tif image:
$ identify -verbose source.tif
Image:
Format: TIFF (Tagged Image File Format)
Class: DirectClass
Geometry: 512x512+0+0
Resolution: 72x72
Print size: 7.11111x7.11111
Units: PixelsPerInch
Type: Palette
Base type: TrueColor
Endianess: MSB
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 1-bit
And I've tried:
convert source.tif output.png
And here are the results:
$ identify -verbose output.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 512x512+0+0
Resolution: 28.34x28.34
Print size: 18.0663x18.0663
Units: PixelsPerCentimeter
Type: Palette
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 1-bit
But I'm not seeing how to make the PNG be RGBA16. Which as I understand means it needs a 16 bit depth, and an alpha channel.
In general, ImageMagick will use the most economical output format that is compatible with your specifications. So, if your input image doesn't have an alpha channel (i.e. transparency), your output image won't have transparency. If a 256-colour palette is adequate for the colours in your image, it will create a palettised output image. If an 8-bit output depth is adequate for your image, it will not bother creating a 16-bit output. And so on...
If you want to force ImageMagick to do something different, you have a number of options.
If you want to force true-colour, or a palettised (indexed) output file, you can do this:
convert input.png -type palette output.png # Force palettised (indexed) output
convert input.png -type truecolor output.png # Force true colour output
If you want to force 8-bit or 16-bit, you can do this:
convert input.png -depth 8 output.png # Force 8-bit output
convert input.png -depth 16 output.png # Force 16-bit (per channel) output
If you want to force an alpha/transparency channel, you can do:
convert input.tif -type TrueColorAlpha output.png # Force a true color output with transparency
And you can combine these too. If you want to see the type options, use this command:
identify -list type
Bilevel
ColorSeparation
ColorSeparationAlpha
ColorSeparationMatte
Grayscale
GrayscaleAlpha
GrayscaleMatte
Optimize
Palette
PaletteBilevelAlpha
PaletteBilevelMatte
PaletteAlpha
PaletteMatte
TrueColorAlpha
TrueColorMatte
TrueColor
Furthermore, specifically in the case of PNG files, you can also force the output by specifying the PNG type in capitals, followed by a colon in front of the output filename, thus:
convert input.tif PNG64:output.png # Force 64-bit RGBA (3 channels # 16-bits each, plus alpha)
convert input.tif PNG32:output.png # Force 32-bit RGBA (3 channels # 8-bits each, plus alpha)
convert input.tif PNG48:output.png # Force 48-bit output (3 channels # 16-bits each, no alpha)
convert input.tif PNG24:output.png # Force 24-bit output (3 channels # 8-bits each, no alpha)
So the short answer is
convert input.tif PNG64:output.png
or
convert input.tif -depth 16 -type TrueColorAlpha output.png
Beware though, ImageMagick will override the second version if no alpha channel is present in your input image, whereas it will not do that if you use PNG64:.

ImageMagick: convert keeps changing the colorspace to Gray. How to preserve sRGB colorspace?

I have a batch script that converts my PNG-24 (with Transparency) images to 50% and 25% size (for mobile development). Usually these images have colors in them but now I am trying to convert an image that has no colors and ImageMagick keeps changing the colorspace profile to "Gray", which messes up my image in the 3d engine I'm using (Unity).
I have tried forcing it to use type TrueColor, colorspace sRGB, and the sRGB.icc profile (the one included with OSX) but it doesn't seem to care. It still changes it to Gray.
> convert old.png -profile srgb.icc -colorspace sRGB -type TrueColor new.png
> identify *.png
old.png PNG 140x140 140x140+0+0 8-bit sRGB 3.68KB 0.000u 0:00.000
new.png PNG 140x140 140x140+0+0 8-bit sRGB 256c 2.33KB 0.000u 0:00.000
ImageMagick still identifies it as an 8-bit sRGB image but it puts "256c" after it which I'm assuming means it has reduced it down to 256 colors, which I don't want either. When I look at the image in OSX Preview.app, it says it is using the Gray color profile. The image also visually looks a lot different.
Here is the image I'm using: https://dl.dropbox.com/u/59304/old.png
There is a duplicate question here, ImageMagick Reduces Colorspace to Gray, but the answer does not work for me and I don't have enough reputation to comment on his answer, unfortunately. I imagine my case is different because I'm using PNG and not JPG.
Version: ImageMagick 6.8.0-7 2013-01-02 Q16 http://www.imagemagick.org
Features: OpenCL
edit- After reading the ImageMagick forums as specified in one of the answers, it looks like just prepending PNG32: or PNG24: to the output file solves the problem.
The proper way to keep a grayscale PNG as RGB is to use PNG24:result.png
Input:
convert lena.png -colorspace gray PNG24:lenag_rgb.png
identify -verbose lenag_rgb.png
Image: lenag_rgb.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 256x256+0+0
Units: Undefined
Colorspace: sRGB
Type: Grayscale
So as you see above, Colorspace is RGB while the type is Grayscale.
For other image formats such as JPG and TIFF, use -define colorspace:auto-grayscale=false along with -type truecolor.
You may pass -set colorspace:auto-grayscale off to convert to disable automatic conversion of RGB channels to a single grayscale channel.
This solution was not yet available at the time of your question, but was introduced in 2015 with version 6.9.2:
2015-07-25 6.9.2-0 Dirk Lemstra <dirk#lem.....org>
Added -set colorspace:auto-grayscale=false that will prevent automatic conversion to grayscale inside coders that support grayscale.

Resources