Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I assumed that Image and ImageData are inverses, so that the following expression gives back the original image.

Image[ImageData[image]]

But that doesn't work. Why?

share|improve this question
    
What do you mean by "it doesn't work"? Image[ImageData[ExampleData[{"TestImage", "Mandrill"}]]] works fine... – blochwave Feb 26 at 19:36
1  
Actually Image[ImageData[ExampleData[{"TestImage", "Mandrill"}]]] == ExampleData[{"TestImage", "Mandrill"}] returns false. If you look at FullForm you see the image color representation has changed from interger to float. – george2079 Feb 26 at 19:49
    
If you use for example a black 24-bit Bitmap image, then what you will get back is a blue image. – Anton Alice Feb 26 at 19:52
    
I colud have sworn this is a duplicate or at least thoroughly discussed in another question but can't find it. – Liam Feb 27 at 4:53
    
Strongly related: "ImageType used by ImageData." – Alexey Popkov Feb 27 at 7:33
up vote 8 down vote accepted

I would guess it's something to do with the ImageType.

img1 = ExampleData[{"TestImage", "Mandrill"}];
img2 = Image[ImageData[ExampleData[{"TestImage", "Mandrill"}]]];    
img1 == img2
(* False *)
ImageType[img1]
(* Byte *)    
ImageType[img2]
(* Real *)

However, trying the following doesn't seem to help:

img3 = Image[
   ImageData[ExampleData[{"TestImage", "Mandrill"}], "Byte"], "Byte"];
ImageType[img3]
(* Byte *)    
img3 == img1
(* False *)

But the following (courtesy of @george2079) does work! It must be related to the default option for Image being the following, rather than RGB, so the original colorspace is not preserved.

ColorSpace -> Automatic treats values as arbitrary channel intensities

img4 = Image[
  ImageData[ExampleData[{"TestImage", "Mandrill"}], "Byte"], "Byte", 
  ColorSpace -> "RGB"]
img1 == img4
(* True *)

Hence the following code from @Kuba should work generally:

Image[ImageData[#, ImageType[#]], ImageType[#], Options[#]] &[img1]
share|improve this answer
2  
puzzled myself.. this gets it back: Image[ImageData[ExampleData[{"TestImage", "Mandrill"}], "Byte"], "Byte", ColorSpace -> "RGB"] – george2079 Feb 26 at 20:05
    
thank you both very much. – Anton Alice Feb 26 at 20:07
1  
probably more general: img1 === Image[ImageData[#, ImageType[#]], ImageType[#], Options[#]] &[img1] – Kuba Feb 26 at 20:08
    
Shouldn't the ColorSpace of RGB provide only 3 parameters? A black bmp image mentioned above comes with a ColorSpace of RGB, but the ImageData looks like {{0,0,0,255},{0,0,0,255}...}. What does the 4th parameter do? – Anton Alice Feb 26 at 20:36
1  
@AntonAlice It denotes the transparency, or "alpha" value. – blochwave Feb 26 at 20:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.