nikon – Handle Nikon sidecar files (read only)
Handle Nikon sidecar files (read only)
The sidecar.nikon module implements handlers for the Nikon sidecar
file. The article named Inside Nikon Sidecar file details the data structure and tags used by Nikon.
Each set of XMP properties (sdc, ast, nine) have its own
handlers to process that set. The NikonSideCar is the main class
managing a Nikon sidecar file.
Using nikon
For this short tutorial, we have a sidecar file named ‘full_XMP.IPTC.NEF.nksc’.
We create an instance of NikonSideCar).
>>> from sidecar.nikon import NikonSideCar
>>> from pathlib import Path
>>> path = Path('images_samples/NKSC_PARAM/full_XMP.IPTC.NEF.nksc')
>>> nksc = NikonSideCar(path)
We can parse the Nikon sidecar file to extract metadata.
>>> nksc.parse()
We can simply access th some key properties via methods as
NikonSideCar.get_rating() or NikonSideCar.is_geotagged(). See
NikonSideCar for an exhaustive list of methods.
>>> nksc.get_rating()
5
>>> nksc.get_label()
'Rose'
>>> nksc.is_geotagged()
True
We can list all the metadata parsed from the sidecar files. The following output has been truncated to fit in the page.
>>> for k, v in nksc.metadata.items():
... print(f" * {k}: {v}")
* appname: NX Studio
* appversion: 1.10 W
* xmp:Rating: 5
* xmp:Label: Rose
* dc:title: [Description] Titre
* dc:subject: ['Mot-clé #1', 'Mot-clé #2', 'Mot-clé #3']
...
* Iptc4xmpExt:Event: ['[Description] Evénement']
We can list all the image adjustement stored in the sidecar file. The following output has been truncated to fit in the page.
>>> for k, v in nksc.processing.items():
... print(f" * {k}: Active={v.active}")
... for n, p in v.params.items():
... print(f" * {n}: {p} ")
* nikon::ColorShift: Active=False
* colorCorrection: 0
* nikon::PictureControl: Active=True
* Export: b'NCP\x00\x00\x00\x00\x01\x00\x00\x00$0100STANDARD...'
* SelectedPictureControl: 0
* AutoContrast: 255
* AutoSaturation: 255
* SelectedPictureControlVersion2: 0
* SavedPicConProcess: 1
* PictureControl: 0
...
We can show the geolocation stored in the side file. The geolocation
attribute is a NikonGPSProperties. As this class have a
object.__repr__ method, only the GPS coordinates are printed. All GPS
properties are in NikonGPSProperties.props. See
NikonGPSProperties for a more detailled view of geolocation
properties.
>>> nksc.geolocation
lat: 48.13672 (N) long: 1.64114 (W)
>>> nksc.geolocation.props['GPSLatitude']
48.13672002447976
Reference
Exception
Sidecar object
- class darkbridge.sidecar.nikon.NikonSideCar(path: Path)
Bases:
objectNikon Side car file.
This class parses a sidecar file to extract the metadata of the image on the one hand, and the image processing stack on the other. The NKSC files are in XML/XMP, so we walk in the RDF tree.
The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
- Parameters:
path – filesystem path of the Nikon Sidecar files containing the XMP Packet element.
- geolocation
object contains the geolocation of the image.
- Type:
- processing
dictonary of image ajustements. The key
activeindicate if the processing will be applied on the image when opening in NX Studio. The keyparamscontains processing’s parameters as a dictionnary.- Type:
dict | None
- parse()
Parse the XMP Packet.
- Raises:
NikonError – Generic error, the
NikonError.messagedetails the error.
- _set_sdc_attr(xmp_property: NikonXMPProperty)
Set the SDC attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_ast_attr(xmp_property: NikonXMPProperty)
Set the AST attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_nine_attr(xmp_property: NikonXMPProperty)
Set the NINE attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _merge_metadata()
Merge the metadata sets.
Image metadata comes from the IPTC set (
ast:IPTCelement) and the XMP set (ast:XMLPackets). This method merge the two sets and store the metadata inmetadata. The priority is the following order : IPTC, XMP.The image label and rating may be defined in the
nineset and in the XMP set (ast:XMLPackets). In the observed use cases, these properties in thenineset are always set to 0 (probably deprecated, but there is no public information). So the priority is the following order : XMP, Nine.
- _set_processing()
Set the image adjustments.
This method simply copy image adjustments in
processing.
- _set_geoloc()
Set the geolocation of the image.
This method simply copy GPS data in
geolocation.
Properties objects
- class darkbridge.sidecar.nikon.NikonSDCProperties
Bases:
objectNikon SDC properties container.
This class parse a sidecar file and extract
sdctags (namespace ‘http://ns.nikon.com/sdc/1.0/’). The properties read are copied into the attributepropswhich can contain the following entries:appname: Name of the application that created the sidecar file.appversion: Version identifier of the application that created the sidecar file.
The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
- get_xmp_props()
Returns the supported properties
This method provides the list of the supported XMP properties to populate the attribute classes.
- Returns:
List of the supported properties. The property names are those of the XMP properties expressed in a short form (i.e.
prefix:tag).- Return type:
- set_attr(xmp_property: NikonXMPProperty)
Set the SDC attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _check_about(xmp_property: NikonXMPProperty)
Check the identifier of properties set.
The identifier have to be
nikon sidecar/1.0.- Parameters:
xmp_property – Nikon XMP property containing data.
- _check_version(xmp_property: NikonXMPProperty)
Check the version identifier of properties set.
The method simply read the version identifier as this attributes is not used.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_appname(xmp_property: NikonXMPProperty)
Set the name of the application that created the sidecar file.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_appversion(xmp_property: NikonXMPProperty)
Set the version of the application that created the sidecar file.
- Parameters:
xmp_property – Nikon XMP property containing data.
- class darkbridge.sidecar.nikon.NikonAsteroidProperties
Bases:
objectNikon Asteroid (AST) properties container.
This class parse a sidecar file and extract
asttags (namespace ‘http://ns.nikon.com/asteroid/1.0/’). The properties read are copied into the attributepropswhich can contain the following entries:XMLPackets: Image’s metadata expressed as an XMP Packet.GPS: Object containing the GPS propertiesIPTC: Image’s metadata using IPTC Information Interchange Model.
The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- about
Identifier of the sidecar file format. The identifier have to be
core-asteroid-tags.- Type:
str | None
- _IPTC_XMP = {'2:05': 'dc:title', '2:101': 'photoshop:Country', '2:103': 'photoshop:TransmissionReference', '2:105': 'photoshop:Headline', '2:110': 'photoshop:Credit', '2:115': 'photoshop:Source', '2:116': 'dc:rights', '2:120': 'dc:description', '2:122': 'photoshop:CaptionWriter', '2:25': 'dc:subject', '2:40': 'photoshop:Instructions', '2:55': 'photoshop:DateCreated', '2:80': 'dc:creator', '2:85': 'photoshop:AuthorsPosition', '2:90': 'photoshop:City', '2:95': 'photoshop:State'}
Mapping IPTC properties to XMP properties, IPTC Specification defines the mapping.
- _SUBJECTS = {'ACE': '1000000', 'CLJ': '2000000', 'DIS': '3000000', 'EDU': '5000000', 'EVN': '6000000', 'FIN': '4000000', 'HTH': '7000000', 'HUM': '8000000', 'LAB': '9000000', 'LIF': '10000000', 'POL': '11000000', 'REL': '12000000', 'SCI': '13000000', 'SOI': '14000000', 'SPO': '15000000'}
- _XMP_META = {'Iptc4xmpCore:CountryCode', 'Iptc4xmpCore:CreatorContactInfo', 'Iptc4xmpCore:IntellectualGenre', 'Iptc4xmpCore:Location', 'Iptc4xmpCore:Scene', 'Iptc4xmpCore:SubjectCode', 'Iptc4xmpExt:Event', 'MicrosoftPhoto:Rating', 'dc:creator', 'dc:description', 'dc:rights', 'dc:subject', 'dc:title', 'photoshop:AuthorsPosition', 'photoshop:CaptionWriter', 'photoshop:Category', 'photoshop:City', 'photoshop:Country', 'photoshop:Credit', 'photoshop:DateCreated', 'photoshop:Headline', 'photoshop:Instructions', 'photoshop:Source', 'photoshop:State', 'photoshop:SupplementalCategories', 'photoshop:TransmissionReference', 'photoshop:Urgency', 'xmp:Label', 'xmp:Rating', 'xmpRights:UsageTerms'}
Supported XMP metadata expressed as XMP properties. This list is subset of the XMP specification [ref] as observed from use cases
- get_xmp_props()
Returns the supported properties
This method provides the list of the supported XMP properties to populate the attribute classes.
- Returns:
List of the supported properties. The property names are those of the XMP properties expressed in a short form (i.e.
prefix:tag).- Return type:
- set_attr(xmp_property: NikonXMPProperty)
Set the AST attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _check_about(xmp_property: NikonXMPProperty)
Check the identifier of properties set.
The identifier have to be
core-asteroid-tags.- Parameters:
xmp_property – Nikon XMP property containing data.
- _check_version(xmp_property: NikonXMPProperty)
Check the version identifier of properties set.
The method simply read the version identifier as this attributes is not used.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_xmlpacket(xmp_property: NikonXMPProperty)
Set the metadata of the image.
The image data are serialized in an embedded XML/XMP, so we walk in the RDF tree.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_iptc(xmp_property: NikonXMPProperty)
Set the IPTC data expressed as an XMP binary property.
This method use the XMP name for the IPTC properties and not the IPTC name. It allows to have a unique naming referential
Datasets 2:15 “Category” and 2:20 “Supplemental Category” are deprecated, but both are still present in NX studio. These two fields were replaced in IIM version 4 by the Dataset 2:12 “Subject Reference” which must be populated by values from the Subject NewsCodes controlled (see Guideline for mapping Category Codes to Subject NewsCodes
- Parameters:
xmp_property – Nikon XMP property containing IPTC data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_gps(xmp_property: NikonXMPProperty)
Set the GPS data expressed as an XMP property.
This method is called for each GPS attribute, so the GPS container is created during the first call and updated on following calls.
- Parameters:
xmp_property – Nikon XMP property containing data.
- class darkbridge.sidecar.nikon.NikonNineProperties
Bases:
objectNikon NINE properties container.
This class parse a sidecar file and extract
ninetags (namespace ‘http://ns.nikon.com/nine/1.0/’).The properties read are copied into the attributepropswhich can contain the following entries:NineEdits: dictionary of image’s adjustments.Label: the image labelRating: the image rating
The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
Note
- In the observed use cases,
LabelandRatingproperties are always set to 0 (probably deprecated, but there is no public information).
- Raises:
NikonError – generic error, the
NikonError.messagedetails the error.
- get_xmp_props()
Returns the supported properties
This method provides the list of the supported XMP properties to populate the attribute classes.
- Returns:
List of the supported properties. The property names are those of the XMP properties expressed in a short form (i.e.
prefix:tag).- Return type:
- set_attr(xmp_property: NikonXMPProperty)
Set the Nine attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _check_about(xmp_property: NikonXMPProperty)
Check the identifier of properties set.
The identifier have to be
nikon sidecar/1.0.- Parameters:
xmp_property – Nikon XMP property containing data.
- _check_version(xmp_property: NikonXMPProperty)
Check the version identifier of properties set.
The method simply read the version identifier as this attributes is not used.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_nine_edits(xmp_property: NikonXMPProperty)
Set the NineEdits (aka adjustments) attribute.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
NikAdjustmentError – Generic error, the
NikAdjustmentError.messagedetails the error.
- _set_label(xmp_property: NikonXMPProperty)
Set the label of the image.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_rating(xmp_property: NikonXMPProperty)
Set the rating of the image.
The image data are serialized in an embedded XML/XMP, so we walk in the RDF tree.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _ignore(xmp_property: NikonXMPProperty)
Do nothing.
- Parameters:
xmp_property – Nikon XMP property containing data.
- class darkbridge.sidecar.nikon.NikonGPSProperties
Bases:
objectNikon GPS properties container.
This class parse a sidecar file and extract
asttags (namespace ‘http://ns.nikon.com/asteroid/1.0/’) related to GPS Information. The properties read are copied into the attributepropswhich can contain the following entries:GPSVersionID: The version of GPS information.GPSLatitudeRef: Whether the latitude is north (N) or south (S).GPSLatitude: The latitude, expressed in decimal degrees.GPSLongitudeRef: Whether the longitude is east (E) or west (W).GPSLongitude: The longitude, expressed in decimal degrees.GPSAltitudeRef: The altitude used as the reference altitude in meters.GPSAltitude: The altitude based on the reference inGPSAltitudeRefin meters.GPSDateTimeStamp: The date and time, in UTC (Coordinated Universal Time).GPSStatus: The status of the GPS receiver when the image is recorded.Ameasurement is in progressVmeasurement interrupted
GPSProcessingMethod: The name of the method used for location finding (GPS,CELLID,MANUAL).GPSMapDatum: The geodetic survey data used by the GPS receiver.WGS-84is used in most cases.GPSSpeedRef: The unit used to express theGPSSpeedproperty.Kkilometers per hourMmiles per hourNknots
GPSSpeed: The speed of GPS receiver movement.GPSImgDirectionRef: The reference for giving the direction of the image when it is captured.Ttrue directionMmagnetic direction
GPSImgDirection: The direction of the image when it was captured. The range of values is from 0.00 to 359.99.
Note
Others GPS properties are ignored.
This class can be used in a formatted string and returns the latitude and longitude.
The article named Inside Nikon Sidecar file details the data structure and tags used by Nikon. In a nutshell, Nikon seems to have using DICOM specification (see GPS Attributes in NKSC files).
- Raises:
ValueError – argument has the right type but an inappropriate value.
- get_xmp_props()
Returns the supported properties
This method provides the list of the supported XMP properties to populate the attribute classes.
- Returns:
List of the supported properties. The property names are those of the XMP properties expressed in a short form (i.e.
prefix:tag).- Return type:
- set_attr(xmp_property: NikonXMPProperty)
Set the GPS attribute expressed as an XMP property.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_version(xmp_property: NikonXMPProperty)
Set the version of GPS information.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_latitude_ref(xmp_property: NikonXMPProperty)
Set the latitude reference: north or south.
Whether the latitude is north (
0) or south (1). This attribute do not match with the DICOM specifications [digps].- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_latitude(xmp_property: NikonXMPProperty)
Set the latitude.
The latitude expressed in degrees-minutes-seconds as a set of three floating number.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_longitude_ref(xmp_property: NikonXMPProperty)
Set the longitude reference: east or west.
Whether the longitude is east (
2) or west # (3). This attribute do not match with the DICOM specifications [digps].- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_longitude(xmp_property: NikonXMPProperty)
Set the longitude.
The longitude expressed in degrees-minutes-seconds as a set of three floating number.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_altitude_ref(xmp_property: NikonXMPProperty)
Set the altitude reference.
Reference altitude in meters as a binary string.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_altitude(xmp_property: NikonXMPProperty)
Set the altitude.
The altitude based on the reference in
GPSAltitudeRef, in meters.- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_timestamp(xmp_property: NikonXMPProperty)
Set the time stamp.
The time in UTC (Coordinated Universal Time) expressed in hours-minutes-seconds as a set of three floating number. This attribute do not match with the DICOM specifications [digps].
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_datestamp(xmp_property: NikonXMPProperty)
Set the datetime stamp.
The date as a string in the format: YYYY:MM:DD. This attribute do not match with the DICOM specifications [digps].
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_status(xmp_property: NikonXMPProperty)
Set the status of the GPS receiver when the image is recorded.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_map_datum(xmp_property: NikonXMPProperty)
Set The geodetic survey data used by the GPS receiver.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_processing(xmp_property: NikonXMPProperty)
Set the name of the method used for location finding.
The name of the method used for location finding as a set of two fixed strings ended by NULL characters.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_speed_ref(xmp_property: NikonXMPProperty)
Set The unit used to express the
GPSSpeed.- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_speed(xmp_property: NikonXMPProperty)
Set the speed of GPS receiver movement.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _set_img_direction_ref(xmp_property: NikonXMPProperty)
Set the reference for giving the direction of the image.
- Parameters:
xmp_property – Nikon XMP property containing data.
- Raises:
ValueError – Inappropriate value, the
ValueError.messagedetails the error.
- _set_img_direction(xmp_property: NikonXMPProperty)
Set the direction of the image.
- Parameters:
xmp_property – Nikon XMP property containing data.
- _ignore_bearing(xmp_property: NikonXMPProperty)
Do nothing.
Bearing GPS properties are is ignored as it contains the same information as Image Direction.
- Parameters:
xmp_property – Nikon XMP property containing data.
- class darkbridge.sidecar.nikon.NikonXMPProperty(element: Element)
Bases:
objectNikon XMP property.
This class parses an XML element as a
XMPproperty and decode the resource. The resource may be a simple text , a structured data identified with therdf:parseTypeattribute, or an array (see section 7.3 to 7.9 [adxmp1])The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
- Parameters:
element – Element containing the property.
- Raises:
NikonError – Generic error, the
NikonError.messagedetails the error.
- value
Value of the property, the type of the attribute depends on the resource type (binary, double,…).
- _set_array(element: Element)
Set an array.
The XMP specifications defines three type of array (section 6.3.4 [adxmp1]). This method store the array item in a
list.- Parameters:
element – Element containing the property.
- _set_simple(element: Element)
Set a simple value.
- Parameters:
element – Element containing the property.
- _set_binary(value: str)
Set a binary resource.
- Parameters:
value – Value of the resource expressed as binary string encoded in base64 .
- _set_long(value: str)
Set an integer resource.
- Parameters:
value – Value of resource expressed as binary string encoded in base64 .
- _set_double(value: str)
Set float number resource.
The value may define one or more float number (IEEE754 Double precision 64-bits). All numbers are simply concatenated with no separators.
- Parameters:
value – Value of resource expressed as binary string encoded in base64 .
- _set_ascii(value: str)
Set ACSII string resource.
- Parameters:
value – Value of resource expressed as binary string encoded in base64 .
- class darkbridge.sidecar.nikon.NikonXMPDescriptions(element: Element)
Bases:
objectManage the XMP Packet header description
This class parses an XMP Packet and checks its structure. In case of error and NikonError exception is raised.
descriptionsattribute contains the list of description block. For the observed use case, there is only description block per sidecar file.The article named “Inside Nikon Sidecar file” details the data structure and tags used by Nikon.
- Parameters:
element – XML element containing the XMP Packet with its wrapper(
xpacket) as an XML processing instruction.
- descriptions
List of ElementTree.Element containing XMP description block identified by the
rdf:Descriptionelement.- Type:
list[xml.etree.ElementTree.Element] | None
- Raises:
NikonError – Generic error, the
NikonError.messagedetails the error.
- _check_xmp_packet()
Check the XMP metadata marker
This method checks the XMP Packet identified by a
rdf:RDFelement (section 7.4 [adxmp1]). The method set the_elementattribute tordf:RDFelement in the XML tree. Anx:xmpmetaelement may be placed around therdf:RDFelement (aka XMP Packet - see section 7.3.3 [adxmp1]) with name of the toolkit (x:xmptk) as attribute. This value is saved inxmptk.- Raises:
NikonError – Generic error, the
NikonError.messagedetails the error.
- _set_xmp_descriptions()
Set the XMP descriptions attributes
This method checks the XMP description identified by a
rdf:Descriptionelement (section 7.4 [adxmp1]). The method set thedescriptionsattribute.- Raises:
NikonError – Generic error, the
NikonError.messagedetails the error.
Constants
- darkbridge.sidecar.nikon.NIKON_SUPPORTED_FORMAT = ['.nef', '.nrw', '.jpg', '.jpeg', '.tif', '.tiff', '.hif', '.nefx', '.mpo']
File extensions of supported image files.
- darkbridge.sidecar.nikon.NIKON_NKSC_SUBFOLDER = 'NKSC_PARAM'
Subfolder storing the Nikon sidecar files.
- darkbridge.sidecar.nikon.NIKON_NKSC_EXT = '.nksc'
File extension of Nikon sidecar files.
- darkbridge.sidecar.nikon.NIKON_LABEL_MAP = {'0': '(Aucune)', '1': 'Red', '2': 'Orange', '3': 'Yellow', '4': 'Green', '5': 'Cyan', '6': 'Blue', '7': 'Purple', '8': 'Magenta', '9': 'Pink'}
Mapping between Nikon Label and XMP label expressed as a text. This table is based on default value and do not consider any customization. See Nikon, [Labels], Options > [Labels]