Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Add Cards/Notes with Images #158

Closed
autoantwort opened this issue Apr 27, 2020 · 2 comments
Closed

Add Cards/Notes with Images #158

autoantwort opened this issue Apr 27, 2020 · 2 comments

Comments

@autoantwort
Copy link

I can add Cards/Notes with the addNote Endpoint. The Endpoint support Audio Files, but is there a option to creates cards/notes with images?

@snoyes
Copy link
Contributor

snoyes commented May 2, 2020

Use storeMediaFile to pass in a URL to download or base64 encoded data, and a filename to save it as. Then in your note you may use <img src="that filename">. And you may combine those into a single call with multi. For example:

{
  "action": "multi",
  "params": {
    "actions": [
      {
        "action": "storeMediaFile",
        "params": {
          "filename": "example.png",
          "url": "https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png"
        }
      },
      {
        "action": "addNote",
        "params": {
          "note": {
            "deckName": "Default", 
            "modelName": "Basic", 
            "fields": { 
              "Front": "Where have you seen this image:<br><img src='example.png'>", 
              "Back": "Wikipedia" 
            }, 
            "tags": []
          }
        }
      }
    ]
  }
}

Note that files of the same name will be silently overwritten.

@armanaegit
Copy link

armanaegit commented Jan 14, 2023

For local files, as @snoyes mentioned, you have to use base64:

import base64
with open('path/to/Example.png', 'rb') as img_file:
    encoded_img = base64.b64encode(img_file.read())

Then your json would be:

{
  "action": "multi",
  "params": {
    "actions": [
      {
        "action": "storeMediaFile",
        "params": {
          "filename": "example.png",
          "data": encoded_img.decode()
        }
      },
      {
        "action": "addNote",
        "params": {
          "note": {
            "deckName": "Default", 
            "modelName": "Basic", 
            "fields": { 
              "Front": "Where have you seen this image:<br><img src='example.png'>", 
              "Back": "Wikipedia" 
            }, 
            "tags": []
          }
        }
      }
    ]
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants