question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to download thumbnail (or other non-JSON-encoded) using python REST API?

See original GitHub issue

Pardon my ignorance, my knowledge of server requests is quite limited. I am trying to get the thumbnail and tiles of a slide from HTK to my python interpreter so I can do some analysis. I am getthing this error because it is not JSON-encoded and girder_client assumes JSON (if my understanding is correct). I would appreciate any help with this. Thanks!

Code I am using:

import requests
import json
import girder_client

# Connect to API
apiUrl = 'http://candygram.neurology.emory.edu:8080/api/v1/'
gc= girder_client.GirderClient(apiUrl = apiUrl)
gc.authenticate(interactive=True)

thumbnail = gc.get("item/%s/tiles/thumbnail" % slide_id)

Error message

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-60-acc36e14b24f> in <module>()
----> 1 gc.get("item/%s/tiles/thumbnail" % slide_id)

~/.conda/envs/wsi/lib/python3.5/site-packages/girder_client/__init__.py in get(self, path, parameters, jsonResp)
    501         Convenience method to call :py:func:`sendRestRequest` with the 'GET' HTTP method.
    502         """
--> 503         return self.sendRestRequest('GET', path, parameters, jsonResp=jsonResp)
    504 
    505     def post(self, path, parameters=None, files=None, data=None, json=None, headers=None,

~/.conda/envs/wsi/lib/python3.5/site-packages/girder_client/__init__.py in sendRestRequest(self, method, path, parameters, data, files, json, headers, jsonResp, **kwargs)
    489         if result.status_code in (200, 201):
    490             if jsonResp:
--> 491                 return result.json()
    492             else:
    493                 return result

~/.conda/envs/wsi/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs)
    894                     # used.
    895                     pass
--> 896         return complexjson.loads(self.text, **kwargs)
    897 
    898     @property

~/.conda/envs/wsi/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    317             parse_int is None and parse_float is None and
    318             parse_constant is None and object_pairs_hook is None and not kw):
--> 319         return _default_decoder.decode(s)
    320     if cls is None:
    321         cls = JSONDecoder

~/.conda/envs/wsi/lib/python3.5/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

~/.conda/envs/wsi/lib/python3.5/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dgutmancommented, Aug 26, 2018

Mohamed we have multiple python notebooks showing how to do this… chat with me or JC tomorrow and we will point you in the right direction…

On Sat, Aug 25, 2018 at 8:05 PM Mohamed Amgad Tageldin < notifications@github.com> wrote:

Pardon my ignorance, my knowledge of server requests is quite limited. I am trying to get the thumbnail and tiles of a slide from HTK to my python interpreter so I can do some analysis. I am getthing this error because it is not JSON-encoded and girder_client assumes JSON (if my understanding is correct). I would appreciate any help with this. Thanks!

Code I am using:

import requests import json import girder_client

Connect to API

apiUrl = ‘http://candygram.neurology.emory.edu:8080/api/v1/’ gc= girder_client.GirderClient(apiUrl = apiUrl) gc.authenticate(interactive=True)

thumbnail = gc.get(“item/%s/tiles/thumbnail” % slide_id)

Error message

JSONDecodeError Traceback (most recent call last) <ipython-input-60-acc36e14b24f> in <module>() ----> 1 gc.get(“item/%s/tiles/thumbnail” % slide_id)

~/.conda/envs/wsi/lib/python3.5/site-packages/girder_client/init.py in get(self, path, parameters, jsonResp) 501 Convenience method to call :py:func:sendRestRequest with the ‘GET’ HTTP method. 502 “”" –> 503 return self.sendRestRequest(‘GET’, path, parameters, jsonResp=jsonResp) 504 505 def post(self, path, parameters=None, files=None, data=None, json=None, headers=None,

~/.conda/envs/wsi/lib/python3.5/site-packages/girder_client/init.py in sendRestRequest(self, method, path, parameters, data, files, json, headers, jsonResp, **kwargs) 489 if result.status_code in (200, 201): 490 if jsonResp: –> 491 return result.json() 492 else: 493 return result

~/.conda/envs/wsi/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs) 894 # used. 895 pass –> 896 return complexjson.loads(self.text, **kwargs) 897 898 @property

~/.conda/envs/wsi/lib/python3.5/json/init.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 317 parse_int is None and parse_float is None and 318 parse_constant is None and object_pairs_hook is None and not kw): –> 319 return _default_decoder.decode(s) 320 if cls is None: 321 cls = JSONDecoder

~/.conda/envs/wsi/lib/python3.5/json/decoder.py in decode(self, s, _w) 337 338 “”" –> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 340 end = _w(s, end).end() 341 if end != len(s):

~/.conda/envs/wsi/lib/python3.5/json/decoder.py in raw_decode(self, s, idx) 355 obj, end = self.scan_once(s, idx) 356 except StopIteration as err: –> 357 raise JSONDecodeError(“Expecting value”, s, err.value) from None 358 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DigitalSlideArchive/HistomicsTK/issues/528, or mute the thread https://github.com/notifications/unsubscribe-auth/AArhzpljVqEv5AYgC1yo8LLUO-CjbWiXks5uUeY9gaJpZM4WMlLI .

– David A Gutman MD PhD Assistant Professor of Neurology, Psychiatry & Biomedical Informatics Emory University School of Medicine Staff Physician, Mental Health Service Line Atlanta VA Medical Center

1reaction
jbeezleycommented, Aug 26, 2018

The response from that endpoint is image/jpeg not application/json. To use girder client to read it, try:

resp = gc.get('item/%s/tiles/thumbnail' % '<item id>', jsonResp=False)
resp.content

To get a numpy array, you will need to decode it. Using PIL you can do something like this

import StringIO
from PIL import Image
import numpy as np

image_content = StringIO.StringIO(resp.content)
image_content.seek(0)
Image.open(image_content)
image = Image.open(image_content)
array = np.array(image)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get raw thumbnail using rest api - Pipeline Integrations
I'm trying to display the full res thumbnail inside of another webpage I've created. So i need a direct path to the image....
Read more >
Trying to add item (jpg image) with thumbnail usin...
Hi! I am adding multiple images (jpg format) to ArcGIS Portal using Python REST API. So far the part of the code that...
Read more >
How to generate thumbnail for attachment uploaded via the ...
I am creating a tool for our own confluence server using python ... I am using atlassian-rest-api to upload documents and update the...
Read more >
How to Download a YouTube Video Thumbnail with NO ...
By APIs with Valentine. Learn APIs. Become a better tester. Get tips and tutorials right in your inbox. No more than 1-2 emails...
Read more >
REST API - Tableau and Behold!
One of the conditions that results in a gray generic thumbnails is any workbook that is connected to a Published Data Source that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found