omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Help with photos module and http post?

    Pythonista
    5
    29
    16110
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • inzel
      inzel @JonB last edited by

      @JonB

      Thanks for the info. I have tried both of those but will again and post my results.

      Thanks!

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @inzel last edited by cvp

        @inzel Perhaps reading here could help.
        See pyimgur/init.py upload_image
        This module allows to post an image to imgur, using base64...

        inzel 1 Reply Last reply Reply Quote 0
        • inzel
          inzel last edited by

          @JonB

          I am trying to use bytesio and base64 but cant get past the img.save line:

          import photos
          import requests
          import io
          import base64
          
          #img = photos.capture_image()
          
          with io.BytesIO() as output:
          	img = photos.capture_image()
          	img.save(output)
          	contents = output.getvalue()
          	image = base64.b64encode(contents)
          
          url = "https://api.kairos.com/enroll```
          
          Error is:
          
          Traceback (most recent call last):
            File "/private/var/mobile/Containers/Shared/AppGroup/2D8E43AB-E263-4AE8-9F57-E62CCC11779D/Pythonista3/Documents/Kairos/kairos_upload.py", line 10, in <module>
              img.save(output)
            File "/var/containers/Bundle/Application/3C9A09E3-8A1E-49E7-956B-A8E46A817D5A/Pythonista3.app/Frameworks/Py2Kit.framework/pylib/site-packages/PIL/Image.py", line 1697, in save
              format = EXTENSION[ext]
          KeyError
          
          Appears that the save function requires me to have an extension. I then tried another angle and am getting a new error:
          
          

          img = photos.capture_image()
          contents = io.BytesIO(img)
          binary_data = contents.getvalue()
          image = base64.b64encode(binary_data)```

          Error is:

          Traceback (most recent call last):
          File "/private/var/mobile/Containers/Shared/AppGroup/2D8E43AB-E263-4AE8-9F57-E62CCC11779D/Pythonista3/Documents/Kairos/kairos_upload.py", line 8, in <module>
          contents = io.BytesIO(img)
          TypeError: 'Image' does not have the buffer interface

          1 Reply Last reply Reply Quote 0
          • inzel
            inzel @cvp last edited by

            @cvp Thanks! I will look in to that as well

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @inzel last edited by cvp

              @inzel if tu save a PIL image to a variable, you have to specify the format

              img.save(output,'JPG')
              
              inzel 1 Reply Last reply Reply Quote 0
              • inzel
                inzel @cvp last edited by

                @cvp

                I tried that as well as:

                img.save(output, format = ‘JPG’)

                And I get errors each time:

                img.save(output, ‘JPG’) :

                Traceback (most recent call last):
                File "/private/var/mobile/Containers/Shared/AppGroup/2D8E43AB-E263-4AE8-9F57-E62CCC11779D/Pythonista3/Documents/Kairos/kairos_upload.py", line 9, in <module>
                img.save(output,'JPG')
                File "/var/containers/Bundle/Application/3C9A09E3-8A1E-49E7-956B-A8E46A817D5A/Pythonista3.app/Frameworks/Py2Kit.framework/pylib/site-packages/PIL/Image.py", line 1704, in save
                save_handler = SAVE[format.upper()]
                KeyError: 'JPG'

                img.save(output, format = ‘JPG’) :

                Traceback (most recent call last):
                File "/private/var/mobile/Containers/Shared/AppGroup/2D8E43AB-E263-4AE8-9F57-E62CCC11779D/Pythonista3/Documents/Kairos/kairos_upload.py", line 9, in <module>
                img.save(output, format = 'JPG')
                File "/var/containers/Bundle/Application/3C9A09E3-8A1E-49E7-956B-A8E46A817D5A/Pythonista3.app/Frameworks/Py2Kit.framework/pylib/site-packages/PIL/Image.py", line 1704, in save
                save_handler = SAVE[format.upper()]
                KeyError: 'JPG'

                Maybe my syntax is wrong?

                cvp 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @inzel last edited by

                  @inzel sorry img.save(output,'JPEG') is ok

                  inzel 1 Reply Last reply Reply Quote 0
                  • inzel
                    inzel @cvp last edited by

                    @cvp

                    Ah perfect. That part works now.

                    with io.BytesIO() as output:
                    	img = photos.capture_image()
                    	img.save(output,'JPEG')
                    	contents = output.getvalue()
                    	image = base64.b64encode(contents)
                    

                    I feel we are very close. I believe the final step now is determining the proper syntax to POST the payload. This would be much easier if I was able to use wireshark or another packet capture tool to see how the POST looks as its being sent but I cant do that on my iPad. This is what I am trying for my payload line but the syntax is incorrect:

                    payload = '{'"image": + image + "',"' + '\n' + '"subject_id": "test" + ","' + '\n' + '"gallery_name": "test"'}'
                    

                    I need it to look like this when its sent:

                    {
                    “image”: image,
                    “subject_id”: “test”,
                    “gallery_name”:”test”
                    }

                    My apologies for all the new guy questions. I really appreciate all the help you guys have provided me so far. Im learning...

                    1 Reply Last reply Reply Quote 0
                    • inzel
                      inzel last edited by

                      I got it to work!

                      import photos
                      import requests
                      import io
                      import base64
                      import json
                      
                      with io.BytesIO() as output:
                      	img = photos.capture_image()
                      	img.save(output,'JPEG')
                      	contents = output.getvalue()
                      	image = base64.b64encode(contents)
                      	
                      url = "https://api.kairos.com/enroll"
                      
                      values = {
                      	'image': image,
                      	'subject_id': 'test',
                      	'gallery_name': 'test'
                      }
                      
                      headers = {
                      	'Content-Type': 'application/json',
                      	'app_id': '*********',
                      	'app_key': '*************************'
                      }
                      
                      request = requests.post(url, data=json.dumps(values), headers=headers)
                      response = request.content
                      
                      print(response)```
                      
                      Thanks everyone!
                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @inzel last edited by

                        @inzel 👍🍾

                        1 Reply Last reply Reply Quote 0
                        • inzel
                          inzel last edited by inzel

                          I decided to clean it up a bit and use some functions. I havent added my comments yet but here is a fully working solution:

                          import photos
                          import requests
                          import io
                          import base64
                          import json
                          
                          img = photos.capture_image()
                          
                          def getPhoto():
                          	with io.BytesIO() as output:
                          		img.save(output, 'JPEG')
                          		contents = output.getvalue()
                          		image = base64.b64encode(contents)	
                          	return image
                          
                          def enrollPhoto():
                          	subject_id = raw_input("Hello, What is your name: ? ")
                          	print("Thank you " + subject_id + "." + " Analyzing...")
                          	image = getPhoto()
                          	url = "https://api.kairos.com/enroll"
                          	values = {
                          		'image': image,
                          		'subject_id': subject_id,
                          		'gallery_name': subject_id
                          	}
                          	headers = {
                          		'Content-Type': 'application/json',
                          		'app_id': '***********',
                          		'app_key': '****************************'
                          	}	
                          	r = requests.post(url, data=json.dumps(values), headers = headers)
                          	parsed_json = json.loads(r.content)
                          	attr = parsed_json['images'][0]['attributes']
                          	img.show()
                          	print(json.dumps(attr, indent=2))
                          
                          enrollPhoto()
                          

                          Just need to put in your actual app_id and app_key. Should work right away. My next step will be getting a simple interface built and then comparing the pic of the user to existing pics of the same user to determine whether or not they gain access. Something like that anyways. Turned out to be a fun endeavor!

                          Vincekok 1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB last edited by

                            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.

                            1 Reply Last reply Reply Quote 0
                            • Vincekok
                              Vincekok @inzel last edited by

                              @inzel this is great. I am learning to use Kairos as well and I will try this out!

                              Thanks!

                              inzel 1 Reply Last reply Reply Quote 0
                              • inzel
                                inzel last edited by

                                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?

                                1 Reply Last reply Reply Quote 0
                                • inzel
                                  inzel @Vincekok last edited by

                                  @Vincekok Thank you!

                                  1 Reply Last reply Reply Quote 0
                                  • JonB
                                    JonB last edited by

                                    Have you switched to py3 interpreter from py2, or vice versa?

                                    1 Reply Last reply Reply Quote 0
                                    • inzel
                                      inzel last edited by

                                      Hey JonB!

                                      Yes I have done that. Its really odd as this was working. Could it be because I went to the iPadOS beta?

                                      1 Reply Last reply Reply Quote 0
                                      • inzel
                                        inzel last edited by

                                        I tested itg on my phone and am getting the same results. No idea what changed here

                                        1 Reply Last reply Reply Quote 0
                                        • JonB
                                          JonB last edited by

                                          print(values)
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • JonB
                                            JonB last edited by

                                            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.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors