Page 1 of 1
Changing Colorspace from Grayscale to RGB
Posted: 2006-06-29T06:19:00-07:00
by mact
Moin,
I just tried this with IM 6.2.8.1 - setting the colorspace (SetImageColorSpace(image,RGBColorspace); ) does not work as expected. Trying to do a scaling from the result gives an error, I think the two missing color channels are not generated.
Is there an easy way of creating a RGB image from a Grayscale image? I could simply copy all pixels to R, G and B - but that would be very slow with larger images, so I'd prefer a way of just telling IM to save the image with three channels instead of one.
BTW "convert" also does not create RGB from Grayscale when used this way:
> convert -colorspace RGB "gray.jpg" "rgb-gray.jpg"
rgb-gray.jpg will NOT be in RGB colorspace but still in grayscale (8 Bit per pixel).
Any hints/suggestions would be highly welcome!
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-15T08:30:09-07:00
by nsh
Hi, i'm trying to do the same, this code does not change the colorspace for given image....
image_info->sampling_factor = "2x2,1x1,1x1";
status = SetImageType(resize_image, TrueColorType);
if (status == MagickFalse){
MagickError(exception.severity, exception.reason, exception.description);
}
status = SetImageColorspace(resize_image, RGBColorspace);
if (status == MagickFalse){
MagickError(exception.severity, exception.reason, exception.description);
{
i get OK as return code, but no the type or colorspace are not changed . . . ?
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-15T10:25:59-07:00
by magick
- image_info->sampling_factor = "2x2,1x1,1x1";
Should be
- image_info->sampling_factor = AcquireString("2x2,1x1,1x1");
Or bad things can occur if you destroy the image_info structure.
When you call "SetImageType(resize_image, TrueColorType);" it automatically converts your image to the RGB colorspace.
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T00:55:29-07:00
by nsh
Thanks for the reply, yes i have now changed to
image_info->sampling_factor = AcquireString("2x2,1x1,1x1");
But, the problem remains, i can't change Grayscale to RGB
my code:
--------------------------------------------------------------------------
image_info->sampling_factor = AcquireString("2x2,1x1,1x1");
status = SetImageType(resize_image, TrueColorType);
if (status == MagickFalse){
fprintf(stderr,"IMAR:ERROR:%s TrueColorType (SetImageType==MagickFalse)",File);
}else{
fprintf(stdout,"IMAR:%s TrueColorType (SetImageType==OK)",File);
}
status = SetImageColorspace(resize_image, RGBColorspace);
if (status == MagickFalse){
fprintf(stderr,"IMAR:ERROR:%s RGBColorspace (SetImageColorspace==MagickFalse)",File);
}else{
fprintf(stdout,"IMAR:%s RGBColorspace (SetImageColorspace==OK)",File);
}
if (image_type = GetImageType(resize_image, &exception)){
fprintf(stdout,"IMAR:%s Type %s (%d)",File,img_types[image_type],image_type);
}else{
description = (char *) exception.description ? (char *)
GetLocaleExceptionMessage(exception.severity, exception.description) : "";
fprintf(stdout,"IMAR:ERROR:%s can't get image type %s",File,description);
}
gives this output:
---------------------------------------------------------------------------------
IMAR:ar-8570-1000002456.org.jpg TrueColorType (SetImageType==OK)
IMAR:ar-8570-1000002456.org.jpg RGBColorspace (SetImageColorspace==OK)
IMAR:ar-8570-1000002456.org.jpg Type GrayscaleType (2)
Do i need to set something else to change the colorspace ?
using : ImageMagick 6.2.3 on SunOS Generic_118558-14 sun4u sparc SUNW,Sun-Blade-100
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T01:02:59-07:00
by mact
see above what I wrote about one and a half year ago, quoting:
> I think the two missing color channels are not generated.
You might want to do that (create the additional channels yourself).
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T01:44:42-07:00
by nsh
OK, understand what you are writing...
But, in magick Core API, how do i do that, add "missing" channels ?
Is the order of these things of great matter ?
have tried to do like this
- Strip profiles
- Add profile (AdobeRGB1998.icc)
- Change Imagetype
- Change Colorspace
and like this
- Strip profiles
- Change Imagetype
- Change Colorspace
- Add profile (AdobeRGB1998.icc)
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T06:53:26-07:00
by nsh
It seams that this can't be done with convert or mogrify,
so maybe it can't be done with imagemagick API ?
nisse@ass001 14:42 ~/local_image_magick >bin/mogrify +profile grey.jpg
nisse@ass001 14:42 ~/local_image_magick >bin/mogrify -profile AdobeRGB1998.icc grey.jpg
nisse@ass001 14:43 ~/local_image_magick >bin/mogrify -type TrueColor grey.jpg
nisse@ass001 14:43 ~/local_image_magick >bin/identify grey.jpg |grep Type
nisse@ass001 14:43 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >bin/mogrify -sampling-factor 1 -type TrueColor grey.jpg
nisse@ass001 14:45 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icc -type TrueColor grey.jpg
nisse@ass001 14:45 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >mv AdobeRGB1998.icc AdobeRGB1998.icm
nisse@ass001 14:46 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icm -type TrueColor grey.jpg
nisse@ass001 14:46 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:47 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icm -colorspace RGB -type TrueColor grey.jpg
nisse@ass001 14:47 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:47 ~/local_image_magick >bin/mogrify -version
Version: ImageMagick 6.3.6 11/14/07 Q16
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2007 ImageMagick Studio LLC
nisse@ass001 14:47 ~/local_image_magick >
?
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T08:25:14-07:00
by nsh
I don't think it is possible to get another return type than GrayscaleType
returned from function GetImageType because it uses
IsGrayImage to determine what type of image it is, and that function check red,green and blue channel...
so am i right when i say that SetImageType(image,TrueColorType)
don't changes the type or colorspace of an greyscale image ?
is that right ? or is that wrong ?
let's begin allover from there...
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-16T08:38:30-07:00
by nsh
Crap !
When asking identify what kind of image grey.jpg is, it says Greyscale and Gray
nisse@ass001 16:35 ~/local_image_magick >bin/identify -verbose grey.jpg
Image: grey.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 150x99+0+0
Type: Grayscale
Endianess: Undefined
Colorspace: Gray
but opening that image in photoshop, tada !
Photoshop understands that image now is RGB...
so, it seams that convert/mogrify really can change the type, but unfortunally dont understand that because of the function isGrayImage that is to clever for me (us?)...
Re: Changing Colorspace from Grayscale to RGB
Posted: 2007-11-22T18:07:31-07:00
by anthony
The colerspace setting Grey and RGB are actually the same thing in memory.
The only difference is that one specificed that the three channels may be different.
An Image Type is however the way the image is stored in the image file.
When writing an image, that could have a 'grey' type, IM will test the in-memory image for 'greyness' and set the type as appropriate, UNLESS you force it to be
a different type using -type or the approprate API setting.
When identify read the image is saw the type as grey, so marked the in-memory RGB, as being grey. The statistics is thus also returning a single 'gray' channel even though in-memory it is still saved as RGB.
see IM examples Basics...
http://imagemagick.org/Usage/basics/#colorspace
and
http://imagemagick.org/Usage/basics/#type