![]() |
IMAGE URL - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: IMAGE URL (/thread-35678.html) |
IMAGE URL - satyanarayana - Nov-30-2021 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. RE: IMAGE URL - snippsat - Nov-30-2021 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') RE: IMAGE URL - satyanarayana - Nov-30-2021 (Nov-30-2021, 12:52 PM)snippsat Wrote: You can drag file to browser to get local url for that file. create http url and post to rest api RE: IMAGE URL - snippsat - Nov-30-2021 (Nov-30-2021, 12:59 PM)satyanarayana Wrote: create http url and post to rest apiHave 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) RE: IMAGE URL - satyanarayana - Dec-01-2021 (Nov-30-2021, 01:47 PM)snippsat Wrote:(Nov-30-2021, 12:59 PM)satyanarayana Wrote: create http url and post to rest apiHave to look at doc for API to see what's required for image upload. when i run this code I am getting error media is non type error RE: IMAGE URL - snippsat - Dec-01-2021 (Dec-01-2021, 05:35 AM)satyanarayana Wrote: when i run this code I am getting error media is non type errorAs 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') RE: IMAGE URL - DeaD_EyE - Dec-03-2021 (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. 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:
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. |