How to set a particular colorspace for the output in imagemagick - imagemagick-convert

How to control the colorspace in the output file using convert. I tried
convert __targets/test_pattern_raw.png -set colorspace RGB -depth 16 -colorspace RGB PNG64:__targets/test_pattern.png
First I set colorspace to RGB, because the input file has falsely been tagged as sRGB. Now I want to save the image in linear space as well. However, the command above gives a non-linear image:
identify __targets/test_pattern.png
__targets/test_pattern.png PNG 1600x1000 1600x1000+0+0 16-bit sRGB 78794B 0.000u 0:00.000
When saving in EXR format, it becomes linear, because EXR does not support anything else, but I need it to work for png, and also for rgba (which is a dump without any headers).

It should work by simply using -colorspace RGB in Imagemagick.
Input:
Filename: lena.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 256x256+0+0
Units: Undefined
Colorspace: sRGB
convert lena.png -colorspace RGB x.png
Filename: x.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 256x256+0+0
Units: Undefined
Colorspace: RGB

The problem was a bug in ImageMagick. After the fix
convert -set colorspace RGB __targets/test_pattern_raw.png -depth 16 PNG64:__targets/test_pattern.png
Works as intended.

Related

Pngs are blank when using convert

The following is giving me a blank image of the expected size:
magick convert a.png b.png -compose difference -composite out.png
However, I can produce the correct output as a jpg:
magick convert a.png b.png -compose difference -composite out.jpg
I then tried to convert this to a png and it works fine:
magick convert out.jpg out.png
Why is the first command giving me a blank image and how might I fix the problem?
Libpng version 1.6.37.
Version: ImageMagick 7.0.11-8 Q16 x86_64 2021-04-17 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5)
Delegates (built-in): bzlib freetype gslib heic jng jp2 jpeg lcms lqr ltdl lzma openexr png ps tiff webp xml zlib
UPDATE:
Bmp is also blank:
magick convert a.png b.png -compose difference -composite out.bmp
And this also works for jpg but not png:
composite -compose difference a.png b.png out.png
And this is what I see when running verbose:
a.png PNG 500x700 500x700+0+0 8-bit sRGB 125771B 0.030u 0:00.016
b.png PNG 500x700 500x700+0+0 8-bit sRGB 90207B 0.020u 0:00.012
a.png=>b.png PNG 500x700 500x700+0+0 8-bit sRGB 90207B 0.450u 0:00.084
This is working and is giving my the required output:
composite -verbose -compose minus a.png b.png out.png

Imagemagick fill with opaque color Gray issue

I need create 50% RGB Gray canvas from existing image to my task.
And using this command to do this:
magick.exe 00_B.tif -fill fractal -opaque fractal 000.png
or
magick.exe 00_B.tif -alpha Opaque +level-colors "rgb(50%,50%,50%)" 000.tif
Original image is 16bit RGB
But Imagemagick always save png or tif or jpg as Grayscale image.
When i define non Gray color
magick.exe 00_B.tif -fill fractal -opaque Sienna 000.tif
ImageMagick save RGB image with Sienna color on it.
What i missed and how should i create 50% Grayscale image?
I plan to use it with -write MPR:GrayLayer in final command so not actual write as a RGB file is needed.
In ImageMagick, you can create a 50% Gray image and save as RGB as follows:
For TIF output:
convert 00_B.tif -fill "gray(50%)" -colorize 100 -type truecolor result.tif
For PNG output:
convert 00_B.tif -fill "gray(50%)" -colorize 100 PNG24:result.png
Alternately:
convert 00_B.tif -evaluate set 50% -type truecolor result.tif
convert 00_B.tif -evaluate set 50% PNG24:result.png

Can ImageMagick return the convert result

I'm using ImageMagick from the command line to convert pdf to image
convert -density 300x300 -resize 800x600 -quality 85 1.pdf[1,2,3] ZIEL.jpg
However, I don't know the result of this operation.this command has nothing output,can anyone know how to get result of convert?
You can use convert's "-verbose" option to see what is happening:
convert -verbose -density 300x300 -resize 800x600 -quality 85 logo: logo: ZIEL.jpg
LOGO GIF 640x480 640x480+0+0 8-bit sRGB 256c 28.6KB 0.020u 0:00.019
logo:=>LOGO GIF 640x480 640x480+0+0 8-bit sRGB 256c 28.6KB 0.000u 0:00.000
LOGO GIF 640x480 640x480+0+0 8-bit sRGB 256c 28.6KB 0.020u 0:00.029
logo:=>LOGO GIF 640x480 640x480+0+0 8-bit sRGB 256c 28.6KB 0.000u 0:00.000
logo:=>ZIEL-0.jpg[0] GIF 640x480=>800x600 800x600+0+0 8-bit sRGB 42KB 0.340u 0:00.330
logo:=>ZIEL-1.jpg[1] GIF 640x480=>800x600 800x600+0+0 8-bit sRGB 42KB 0.210u 0:00.210
Because JPEG is a single-image format, the output is in separate images ZIEL-0.jpg, ZIEL-1.jpg, etc.

Imagemagick depth convert

Having an imgage in.png. Now do the next:
$ convert in.png -strip out.tiff #convert to tiff
$ convert out.tiff -strip out.png #and back
$ cmp in.png out.png
$#no output - the images has no difference - theyre same
$ identify in.png out.png
in.png PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out.png[1] PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
now trying this via eps. So:
$ convert in.png -strip out2.eps #convert to eps
$ convert out2.eps -strip out2.png #back
and the images are different.
$ identify in.png out.png out2.png
in.png PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out.png[1] PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out2.png[2] PNG 300x300 300x300+0+0 8-bit sRGB 4c 321B 0.000u 0:00.009
As you can see, the conversion eps -> png creates 8-bit png.
Curiosity plus - the EPS is 16 bit!
$ identify out2.eps
out2.eps PS 300x300 300x300+0+0 16-bit sRGB 1.42KB 0.000u 0:00.000
Questions:
why the eps->png conversion changes the bit-depth?
how to convert from eps to png to get 16-bit png? (as from tiff).
EDIT
Tested #Rachel recommendation:
$ convert out2.eps -depth 16 out3.png
$ identify out3.png
out3.png PNG 300x300 300x300+0+0 8-bit sRGB 4c 518B 0.000u 0:00.000
8 bit again.
My ImageMagick version:
$ convert --version
Version: ImageMagick 6.8.9-1 Q16 x86_64 2014-06-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib djvu fftw fontconfig freetype gslib gvc jng jp2 jpeg lcms ltdl lzma pangocairo png ps tiff webp wmf x xml zlib
If anyone want test, here is my test image:
FINAL
So, #Mark Setchell and #Rachel Gallen's recommendation are good. #Marks command works, so the key is PNG48.
$ convert out2.eps -depth 16 PNG48:out7.png
$ identify out7.png
out7.png PNG 300x300 300x300+0+0 16-bit sRGB 1.86KB 0.000u 0:00.000
Finally 16-bit png. What I will ask it in another question, why the next:
$ mogrify -strip out7.png
$ identify out7.png
out7.png PNG 300x300 300x300+0+0 8-bit sRGB 4c 321B 0.000u 0:00.000
8 bit again. And the -strip should remove only metadata and should not change the image itself.
I think you need something along these lines to get 16-bit PNGs
convert a.png -depth 16 PNG48:b.png
i think from looking at the docs you can do
convert -depth 16 image.eps image2.png[16]
the syntax is
convert [ options … ] file [file …] file
This site says 'Use an optional index enclosed in brackets after a file name to specify a desired subimage of a multi-resolution image format like Photo CD (e.g. img0001.pcd[4]) or a range for MPEG images (e.g. video.mpg[50-75]).'
so maybe put the [16] after image2.png

Imagemagick gradient from left and right

I want to use Imagemagick to add a transparent gradient from the left and right to be composed with an image.
I can generate the image with:
convert -size 100x100 gradient: -function Polynomial -4,4,0 -distort SRT 90 gradient.png
And it looks like:
I can also compose that image on top of the image I want with:
composite original.jpg -compose Multiply gradient.png final_image.jpg
All of this works fine. My question is, how do I change the color of that gradient?
Thanks!
Simply add comma-separated colors after gradient: option:
convert -size 100x100 gradient:white,blue -function Polynomial -4,4,0 -distort SRT 90 gradient.png
Note, that -function converts channels too, so colors in gradient wont be original.
Found a way around this by doing
convert input.jpg -alpha set -virtual-pixel transparent -background red -channel A -blur 0x34 -level 75%,100% +channel -flatten final.jpg
which works in my case, giving me:

Resources