I have imported an image, and using FullForm
I can see that the image has a collection of properties, e.g. Rule[Interleaving, None]
. What I can't work out is how to access any of those properties. Clearly, I am missing something very simple.
-
$\begingroup$ Just to be clear, I am using this problem as a general example of how to access the properties of any object. I know that I could use e.g. ImageDimensions etc., but I want to understand how to access any property, whether it has a getter function defined, or not. $\endgroup$– Andy BurnettCommented Aug 27, 2017 at 11:43
3 Answers
The rules you mention can be extracted using Options
. Note that this does not find all properties, e.g. the image dimensions are not explicitly stored in the Image
.
Example:
In[1]:= Options@ExampleData[{"TestImage", "Lena"}]
Out[1]= {ColorSpace -> "RGB", Interleaving -> True,
MetaInformation -> <|"Comments" -> <|"Software" ->
"Wolfram Mathematica 8.0",
"Creation Time" ->
DateObject[{2010, 2, 18, 12, 43, 31.}, "Instant", "Gregorian",
2.]|>|>}
-
$\begingroup$ That's great. Thank you so much. I find Mathematica's language quite confusing - as a newbie. I was searching for properties, and attributes, and even entity values. I would not have thought of 'options'. $\endgroup$ Commented Aug 27, 2017 at 11:55
-
$\begingroup$ "…this does not find all properties, e.g. the image dimensions are not explicitly stored in the
Image
." - I would imagine that the properties not stored are as such because they can be easily derived from the result ofImageData[]
. $\endgroup$ Commented Aug 27, 2017 at 11:56
Some information is stored in options:
Import["path/to/file.png", "Options"]
{"BitDepth" -> 8, "ColorSpace" -> RGBColor, "ImageSize" -> {419, 348}}
More information is available by using "Rules":
Import["path/to/file.png", "Rules"]
This is only for use with images stored on your computer. It is not for images that have already been loaded into Mathematica. (To get data from already imported images, use Mathe 172's answer.)
You can see what is inside any of the Mathematica data structures using FullForm. For example, for the image:
img = ExampleData[{"TestImage", "Lena"}];
FullForm[img]
is a long list. It starts with
Image[RawArray["UnsignedInteger8", ....
followed by a long list of Lists (which are the pixel values in the image array). It ends with
"Byte", Rule[ColorSpace, "RGB"],
Rule[Interleaving, True], Rule[MetaInformation,
Association[Rule["Comments",
Association[Rule["Software", "Wolfram Mathematica 8.0"],
Rule["Creation Time",
DateObject[List[2010, 2, 18, 12, 43, 31.], "Instant", "Gregorian", -5.]]]]]]
which gives more information about what is inside an Image. Similarly, you can look inside sound objects and other data structures.