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?
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]
Image[ImageData[ExampleData[{"TestImage", "Mandrill"}], "Byte"], "Byte", ColorSpace -> "RGB"]
$\endgroup$
Commented
Feb 26, 2016 at 20:05
img1 === Image[ImageData[#, ImageType[#]], ImageType[#], Options[#]] &[img1]
$\endgroup$
ColorSpace
of RGB, but the ImageData
looks like {{0,0,0,255},{0,0,0,255}...}
. What does the 4th parameter do?
$\endgroup$
Commented
Feb 26, 2016 at 20:36
Image[ImageData[ExampleData[{"TestImage", "Mandrill"}]]]
works fine... $\endgroup$Image[ImageData[ExampleData[{"TestImage", "Mandrill"}]]] == ExampleData[{"TestImage", "Mandrill"}]
returns false. If you look atFullForm
you see the image color representation has changed from interger to float. $\endgroup$ImageType
used byImageData
." $\endgroup$