.jpg to .rgb, -colorspace vs. -set colorspace

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
vmartin
Posts: 5
Joined: 2017-04-01T09:21:56-07:00
Authentication code: 1151

.jpg to .rgb, -colorspace vs. -set colorspace

Post by vmartin »

I have an sRGB-encoded JPEG image.
I would like to convert it to "RGB" format, i.e., pure image data in R-G-B triplets with no headers, etc.
In addition, I would like to convert it in two ways, one so that the R-G-B output is sRGB-encoded, and
one so that the R-G-B output is linear.

To convert to sRGB-encoded R-G-B, I did

Code: Select all

convert input.jpg output.rgb
And to convert to linear R-G-B, I did

Code: Select all

convert input.jpg -colorspace RGB output.rgb
but that produced output identical to the first case.

When I do

Code: Select all

convert input.jpg -set colorspace RGB -colorspace RGB output.rgb
then the output is different, but I'm unclear why or whether it did what I intended.

Looking at viewtopic.php?t=17207, it says
-colorspace : change the way actual pixel are represented
-set colorspace : tell image the data is represented this way, but does not touch the data
which matches my reading of the docs, but since .rgb format has no header I don't understand what
-set colorspace is "setting" in my case, or why it makes a difference.

Can someone please set me on the right path.
MV
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: .jpg to .rgb, -colorspace vs. -set colorspace

Post by snibgo »

First things first: What version IM are you using?
snibgo's IM pages: im.snibgo.com
vmartin
Posts: 5
Joined: 2017-04-01T09:21:56-07:00
Authentication code: 1151

Re: .jpg to .rgb, -colorspace vs. -set colorspace

Post by vmartin »

Sorry, meant to include in the question

Code: Select all

Version: ImageMagick 7.0.5-4 Q16 x86_64 2017-04-01 http://www.imagemagick.org
MV
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: .jpg to .rgb, -colorspace vs. -set colorspace

Post by fmw42 »

There is no header with .rgb (it is just raw data). So if you want to change the data from sRGB (gamma 0.4545) to linear RGB (gamma 0.1), I think you can try doing that by using -gamma to do the conversion. I am not 100% sure about that. I would have thought -colorspace RGB might do the same. So try assuming your input jpg is sRGB.

convert input.jpg -gamma 1 output.rgb

If that fails then perhaps raw rgb is always linear?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: .jpg to .rgb, -colorspace vs. -set colorspace

Post by snibgo »

Raw RGB is always assumed to be sRGB, I think.

Using "-gamma" or "-evaluate pow" will approximately change between RGB and sRGB. "-colorspace" is more accurate.


"-colorspace XXX" if image isn't already in colorspace XXX (according to the metadata), this changes pixel values and metadata.

"-set colorspace XXX" if image isn't already in colorspace XXX (according to the metadata), this changes metadata only.

A third fact you need to know: not all filetypes support all colorspaces. When the image is written to a file that doesn't support the colorspace, it is automatically converted a colorspace that is supported, as if "-colorspace XXX" was used.

I think the .RGB format supports only the sRGB colorspace.

If your image is in RGB colorspace and you want to write those values, unchanged, to a .RGB file, first use "-set colorspace sRGB". Then IM won't automatically convert it.
snibgo's IM pages: im.snibgo.com
Post Reply