Help with photos module and http post?
-
Hello Everyone,
I am currently messing around with the Kairos API and am trying to use Pythonista to take a new photo on my iPad and then upload that photo to the Kairos enroll API. I am able to get this to work fine with a URL image but for the life of me I am unable to get this to work by taking a photo with the photos module. From my understanding the photos module returns a PIL Image and I think I need to base64 encode that before uploading to the Kairos API??
Here is my code without using the photos module:
#import photos import requests #img = photos.capture_image() url = "https://api.kairos.com/enroll" values = """ { "image": "URL for image", "subject_id": "test", "gallery_name": "test" } """ headers = { 'Content-Type': 'application/json', 'app_id': '********', 'app_key': '************************' } request = requests.post(url, data=values, headers=headers) response = request.content print(response)
Im hoping that someone can help me out by showing me what I need to do to be able to accomplish this task. Any help is greatly appreciated.
Thank you in advance,
Colin
-
fwiw,
requests
lets you use req.json() rather than json.loads(req.contents). also, you can use json=values instead of json.dumps(values) in the request.
-
@inzel this is great. I am learning to use Kairos as well and I will try this out!
Thanks!
-
I just came back to this project and I get an error!
r = requests.post(url, data=json.dumps(values), headers = headers)
Object of type 'bytes' is not JSON serializable
I havent made any changes so I cant imagine why this is suddenly happening.
Any thoughts?
-
@Vincekok Thank you!
-
Have you switched to py3 interpreter from py2, or vice versa?
-
Hey JonB!
Yes I have done that. Its really odd as this was working. Could it be because I went to the iPadOS beta?
-
I tested itg on my phone and am getting the same results. No idea what changed here
-
print(values)
-
The original code would have probably worked in py2. In py3, you’d need to do
image.decode('ascii')
As the argument to json.dumps.
-
I dont quite understand. Can you please explain the change to me?
Edit: It does work making that change but I dont understand why its now needed. Thats what Im interested in learning about