Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IMAGE URL
#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


Messages In This Thread
IMAGE URL - by satyanarayana - Nov-30-2021, 11:43 AM
RE: IMAGE URL - by snippsat - Nov-30-2021, 12:52 PM
RE: IMAGE URL - by satyanarayana - Nov-30-2021, 12:59 PM
RE: IMAGE URL - by snippsat - Nov-30-2021, 01:47 PM
RE: IMAGE URL - by satyanarayana - Dec-01-2021, 05:35 AM
RE: IMAGE URL - by snippsat - Dec-01-2021, 02:15 PM
RE: IMAGE URL - by DeaD_EyE - Dec-03-2021, 09:10 AM

Forum Jump:

User Panel Messages

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