Convert images from camera module to base64 and post a request to an url

560 views
Skip to first unread message

SoSoDef

unread,
Aug 25, 2018, 5:44:11 PM8/25/18
to Kivy users support

Could a kind soul help me to wrap the following pure python code into KIVY. I am struggling with the documentation a lot.

Basically, what I try to achieve is to capture images with the KIVY camera module and store them within a list. All images in that list should be converted to base64 string and added to a specific JSON dump. That JSON dump will be sent via POST to an url. The response will be stored within a variable. I wrote the pure python code which basically does what I described before. 

I think the following KIVY modules are needed: 

  • kivy.network.urlrequest
  • kivy.storage.jsonstore

Also the cache of KIVY should be cleared after restart of the app. Wouldn't it blow the cache if captured images do not get deleted?

Pure Python Code
import base64
import json
import requests

image_list_from_kivy_captures = ["image_1.png", "image_2.png", "image_3.png"]

json_components = []
further_json_obj = []

def img_to_base64():

    for img in image_list_from_kivy_captures:
        with open(img, 'rb') as image_file:
            content_json_obj = {
                'image_b64': base64.b64encode(image_file.read()).decode('UTF-8')
                 }

        further_json_obj.append({
            'Test_1': "text_1",
            'Test_2': "text_2"
        })


        json_components.append({
            'image': content_json_obj,
            'tests': further_json_obj
        })

    json_data = json.dumps({'requests': json_components})

    return json_data

data = img_to_base64()

response = requests.post(url='https://my_url.com/test-me', data=data)



So far I have the following KIVY code:

class CamScreen(Screen):
    def capture(self):

        camera = self.ids['camera']
        timestr = time.strftime("%Y%m%d_%H%M%S")
        camera.export_to_png("IMG_{}.png".format(timestr))

   # I think the most of the pure python code will appear here


 class TempResultScreen(Screen):
     # the response should be stored here within a variable as a string


And the following KIVY Screens:


<CamScreen>
    RelativeLayout:
        size_hint: 1, 1
    
        Camera:
            id: camera
            pos_hint: {'center_x':0.5, 'center_y':0.5}
            resolution: (1920,1080)
            play: True
            allow_stretch: True
            keep_ratio: False
    
        Button:
            pos_hint: {'center_x':0.5, 'center_y':0.1}
            size_hint_y: None
            size_hint_x: None
            background_normal:'icons/icon_cam_white_64.png'
            border: (0,0,0,0)
            background_down: 'icons/icon_cam_grey_64.png'
            allow_stretch: True
            keep_ratio: False
            on_press: 
                root.capture()
    
        Button:
            pos_hint: {'center_x':0.8, 'center_y':0.1}
            size_hint_y: None
            size_hint_x: None
            background_normal:'icons/icon_done_white_64.png'
            border: (0,0,0,0)
            background_down: 'icons/icon_done_grey_64.png'
            allow_stretch: True
            keep_ratio: False
            on_press: 
                root.manager.transition.direction = "left"
                root.manager.transition.duration = .5
                root.manager.current = "tempresultscreen"


<TempResultScreen>
    Button:
        pos_hint: {'center_x':0.5, 'center_y':0.5}
        text: "This is TempResultScreen"

Umut Oezdemir

unread,
Sep 1, 2018, 8:32:37 PM9/1/18
to Kivy users support
I managed to solve that. The Code is too big to post it here.

If you have a similar issue, feel free to contact me.
Reply all
Reply to author
Forward
0 new messages