Bug 106 - Unable to export preview as jpeg from Lua script
Summary: Unable to export preview as jpeg from Lua script
Status: RESOLVED FIXED
Alias: None
Product: Orthanc
Classification: Unclassified
Component: Orthanc Core (show other bugs)
Version: unspecified
Hardware: All All
: --- minor
Assignee: Sébastien Jodogne
URL:
Depends on:
Blocks:
 
Reported: 2020-06-29 15:14 CEST by Sébastien Jodogne
Modified: 2020-06-29 15:25 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sébastien Jodogne 2020-06-29 15:14:29 CEST
[BitBucket date: 2018-10-19.13:30:57]

In order to export a preview as jpeg, you need to pass the `Accept: image/jpeg` header.  `RestApiGet` does not support passing specific headers, and using `HttpGet` is not allowed to make calls to the local instance.

Here's an example:


```
#!Lua

function OnStoredInstance(instanceId, tags, metadata, origin)


  -- Generate a image preview of the DICOM file
  local png = RestApiGet('/instances/' .. instanceId .. '/preview') # This works!
  local headers = {
    ['Accept']='image/jpeg'
  }
  local jpeg = HttpGet('http://orthanc:foobar@localhost:8042/instances/' .. instanceId .. '/preview', headers) # This does not!


end
```
Comment 1 Sébastien Jodogne 2020-06-29 15:25:54 CEST
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2019-06-24.14:06:54]

Fix issue #106 (Unable to export preview as jpeg from Lua script)

→ https://hg.orthanc-server.com/orthanc/changeset/dd1e68f2d0c0
Comment 2 Sébastien Jodogne 2020-06-29 15:25:55 CEST
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2019-06-24.14:08:53]

`RestApiGet()` now can be provided with HTTP headers. Full example:

```lua
function Initialize()
   local instanceId= '19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5'

   local png = RestApiGet('/instances/' .. instanceId .. '/preview', true)
   print('PNG size: ' .. string.len(png))

   local headers = {}
   headers['Accept']='image/jpeg'
   local jpeg = RestApiGet('/instances/' .. instanceId .. '/preview', true, headers)
   print('JPEG size (default quality = 90): ' .. string.len(jpeg))

   local jpeg50 = RestApiGet('/instances/' .. instanceId .. '/preview?quality=50', true, headers)
   print('JPEG size (quality 50): ' .. string.len(jpeg50))

   headers['Accept']='image/x-portable-arbitrarymap'
   local pam = RestApiGet('/instances/' .. instanceId .. '/preview', true, headers)
   print('PAM size (uncompressed): ' .. string.len(pam))
end
```