Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IMAGE URL
#1
Hi friends,

"D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png"

I have local image file with path as shown above . I have to generate http URL of that file in python any example code.
Reply
#2
You can drag file to browser to get local url for that file.
If tests in webbrowser it will be like this.
import webbrowser

webbrowser.open('file:///D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png')
Reply
#3
(Nov-30-2021, 12:52 PM)snippsat Wrote: You can drag file to browser to get local url for that file.
If tests in webbrowser it will be like this.
import webbrowser

webbrowser.open('file:///D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png')


create http url and post to rest api
Reply
#4
(Nov-30-2021, 12:59 PM)satyanarayana Wrote: create http url and post to rest api
Have to look at doc for API to see what's required for image upload.
Can use Requests for this.
Example.
import requests

url = 'Api url'
files = {'media': open('D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png', 'rb')}
requests.post(url, files=files)
Api may need more information like eg API key.
import requests
 
url = 'Api url'
headers = {
    'content-type': "multipart/form-data",
    'accept': "application/json",
    'apikey': "my_key"
    }
files = {'media': open('D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png', 'rb')}
response = requests.post(url, headers=headers, files=files)
Reply
#5
(Nov-30-2021, 01:47 PM)snippsat Wrote:
(Nov-30-2021, 12:59 PM)satyanarayana Wrote: create http url and post to rest api
Have to look at doc for API to see what's required for image upload.
Can use Requests for this.
Example.
import requests

url = 'Api url'
files = {'media': open('D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png', 'rb')}
requests.post(url, files=files)
Api may need more information like eg API key.
import requests
 
url = 'Api url'
headers = {
    'content-type': "multipart/form-data",
    'accept': "application/json",
    'apikey': "my_key"
    }
files = {'media': open('D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png', 'rb')}
response = requests.post(url, headers=headers, files=files)

when i run this code I am getting error media is non type error
Reply
#6
(Dec-01-2021, 05:35 AM)satyanarayana Wrote: when i run this code I am getting error media is non type error
As mention you most look at API documentation,i can not test this.
Here is working example httpbin.org open for testing Request & Response.
import requests

url = 'https://httpbin.org/post'
headers = {
    'content-type': "text/html",
    'accept':  '*/*',
    }
files = {'file': open('help.jpg', 'rb')}
response = requests.post(url, headers=headers, files=files)
Test,see that binary data of image has been accepted and uploaded.
# Test see that get 200
>>> response
<Response [200]>

# The content of response
>>> response.content
(b'{\n  "args": {}, \n  "data": "data:application/octet-stream;base64,LS05ZDk'
 b'xMzMxNWQ4ZDJkMjM1ZjlmNTMxMGI1MTYyMDYyMQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1'
 b'kYXRhOyBuYW1lPSJpbWFnZSI7IGZpbGVuYW1lPSJoZWxwLmpwZyINCg0K/9j/4AAQSkZJRgABAQA'
 .....ect
 , \n  "files": {}, \n  "form": {}, \n  "h'
 b'eaders": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate",'
 b' \n    "Content-Length": "13765", \n    "Content-Type": "text/html", \n    '
 b'"Host": "httpbin.org", \n    "User-Agent": "python-requests/2.25.1", \n   '
 b' "X-Amzn-Trace-Id": "Root=1-61a7819b-659a9d9e01b8ac6f402f4854"\n  }, \n  "'
 b'json": null, \n  "origin": "46.246.117.13", \n  "url": "https://httpbin.or'
 b'g/post"\n}\n')
Reply
#7
(Nov-30-2021, 11:43 AM)satyanarayana Wrote: I have local image file with path as shown above . I have to generate http URL of that file in python any example code.

To generate an uri, you can use pathlib.
from pathlib import PureWindowsPath

waveform_uri = PureWindowsPath("D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png").as_uri()
# the as_uri converts the Path object to a str which is a file uri
print(waveform_uri)
In my case, I used PureWindowsPath because I'm on a Linux system. You can just use Path from pathlib.
Using Path will give you the right Type depending on the current running OS. If you're running Windows, you get a WindowsPath back. If you use Linux, you'll get a PosixPath.

Output:
file:///D:/Satya_Projects_Onedrive/Epigon_Projects/BILVA_Projects/waveform.png
The benefit of this method is, that also chars are replaced via the % representation.
So, you could also use a Path with white spaces, and they get replaced with %20 automatically.

But this does not solve the problem of getting the file from the web server itself, and it works only local on your machine.

Solutions:
  1. let the web server itself delivers the static content. In this case, the generated uri is wrong because it points to a local file system on your local machine.
  2. embedding the Data in the HTML-Document itself. Then the data is embedded in the data uri. Format:
    data:MIME_TYPE;base64,BASE64_DATA

    One example with a svg from Inkscape:
    <img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB3aWR0aD0iMjEwbW0iCiAgIGhlaWdodD0iMjk3bW0iCiAgIHZpZXdCb3g9IjAgMCAyMTAgMjk3IgogICB2ZXJzaW9uPSIxLjEiCiAgIGlkPSJzdmc1IgogICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzCiAgICAgaWQ9ImRlZnMyIiAvPgogIDxnCiAgICAgaWQ9ImxheWVyMSI+CiAgICA8cmVjdAogICAgICAgc3R5bGU9ImZpbGw6I2ZmMmEyYTtzdHJva2Utd2lkdGg6MC4yNjQ1ODMiCiAgICAgICBpZD0icmVjdDMxIgogICAgICAgd2lkdGg9IjUyLjAyMDkzNSIKICAgICAgIGhlaWdodD0iMzMuNDgxNjAyIgogICAgICAgeD0iNzMuOTQwNzUiCiAgICAgICB5PSI3OS40NDczMjciIC8+CiAgICA8ZWxsaXBzZQogICAgICAgc3R5bGU9ImZpbGw6I2FjOTM5MztzdHJva2Utd2lkdGg6MC4yNjQ1ODMiCiAgICAgICBpZD0icGF0aDEzNSIKICAgICAgIGN4PSI5OS4zMTM1OTkiCiAgICAgICBjeT0iOTcuNjM0NzI3IgogICAgICAgcng9IjEwLjQ4NDk1MiIKICAgICAgIHJ5PSI4LjkwNzE2MDgiIC8+CiAgPC9nPgo8L3N2Zz4K">

Here example code to generate from a file an img tag, where the src is a data-uri and the mime type is detected automatically.
import mimetypes
import base64


def data_uri(file):
    mime = mimetypes.guess_type(file)[0]
    with open(file, "rb") as fd:
        b64s = base64.b64encode(fd.read()).decode()

    return f"data:{mime};base64,{b64s}"


def img(file):
    return f'<img src="{data_uri(file)}">'

If you're working with flask or other Python web frameworks, you don't embed data into your template.
The usual way is to generate a template, fill it with data (happens in your Python code) and static resources points to /static.

But if you want to host your HTML Document only locally without a web server and no requirement of accessing via network, you could use the data-uri.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020