Preserving instance metadata in Lua script

In my setup any instance that is received is compressed to JPEG2000 using the Lua script below. The problem is this changes the metadata on the instance to something like as follows:

“IndexInSeries”: “585”,
“Origin”: “Lua”,
“ReceptionDate”: “20190918T204126”,
“RemoteAET”: “”,
“SopClassUid”: “1.2.840.10008.”,
“TransferSyntax”: “1.2.840.10008.”

Is it possible to retain the original metadata as I really need to get the RemoteAET info. The script I’m using is below:

function OnStoredInstance(instanceId, tags, metadata, origin)
– Do not compress twice the same file
if origin[‘RequestOrigin’] ~= ‘Lua’ then

– Retrieve the incoming DICOM instance from Orthanc
local dicom = RestApiGet(‘/instances/’ … instanceId … ‘/file’)

– Write the DICOM content to some temporary file
local uncompressed = instanceId … ‘-uncompressed.dcm’
local target = assert(, ‘wb’))

– Compress to JPEG2000 using gdcm
local compressed = instanceId … ‘-compressed.dcm’
os.execute('C:\Orthanc\Tools\GDCM\bin\gdcmconv.exe -U --j2k ’ … uncompressed … ’ ’ … compressed)

–os.execute('C:\Orthanc\Tools\dcmtk\bin\dcmodify.exe --no-backup -gin ’ … compressed)
os.execute(‘C:\Orthanc\Tools\dcmtk\bin\dcmodify.exe --no-backup -m "SOPInstanceUID=’ … tags[‘SOPInstanceUID’] … '" ’ … compressed)

– Read the JPEG2000 file
local source = assert(, ‘rb’))
local jpeg2k = source:read(“*all”)

– Upload the JPEG2000 file and remove the uncompressed file
RestApiDelete(‘/instances/’ … instanceId)
RestApiPost(‘/instances’, jpeg2k)

– Remove the temporary DICOM files


Hi Rana,

This is currently not possible.
What you could do in your lua script is:

  • read the Origin & RemoteAET from the uncompressed instance and store them in local variables
  • compress and upload the new instance
  • set new metadata in the compressed instance (however, you won’t be able to overwrite the Origin and RemoteAET since they are ‘internal’ metadatas So, this means that you’ll have to define “MyOrigin” and “MyRemoteAET” to store this info.

Best regards,


Thank you.