-
-
Save NathanW2/1933f13bebe548db1104 to your computer and use it in GitHub Desktop.
import requests | |
import base64 | |
@qgsfunction(args='auto', group='Custom') | |
def show_camera(feed, feature, parent): | |
svg = """ | |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
<svg> | |
<g> | |
<image xlink:href="data:image/jpeg;base64,{0}" height="256" width="320" /> | |
</g> | |
</svg> | |
""" | |
data = requests.get(feed, stream=True).content | |
name = feed[-16:] | |
b64response = base64.b64encode(data) | |
newsvg = svg.format(b64response).replace('\n','') | |
path = r"C:\temp\camera\{0}.svg".format(name) | |
with open(path, 'w') as f: | |
f.write(newsvg) | |
return path.replace("\\", "/") |
Hi. JR from (https://gis.stackexchange.com/questions/271096) directed me to your solution. The idea is perfect for what I need to do, but my images are local jpg. As I know nothing of python I have been blindly trying to modify your function to no avail. Can you help? What should be the function to use locally stored jpg images?
This is were I did arrive
import requests
import base64
@qgsfunction(args='auto', group='Custom')
def show_image(pSourceFile, pFileName, feature, parent):
svg =
.
.
.
fullorigpath = "D:\MDKD\MOZA\60 INFRA\09 software\2018-02 GPSfotolapse_1_5_6\v01\vdf200"
fullorigpath = fullorigpath.replace("\", "/")
pSourceFile = fullorigpath + pSourceFile[1:]
vFileName = pFileName[:-4]
vSourceFileSVG = r"D:\temp\{0}.svg".format(vFileName)
origdata = pSourceFile
b64data = base64.b64encode(origdata)
newsvg = svg.format(b64data).replace('\n','')
with open(vSourceFileSVG, 'w') as f:
f.write(newsvg)
return vSourceFileSVG
and this the error I get
'ascii' codec can't decode byte 0x81 in position 30: ordinal not in range(128)
Cool, but it don't work for me.. When i try to test the function qgis tell me:
Traceback (most recent call last):
File "", line 1, in
File "C:/PROGRA
1/QGISLY1/apps/qgis/./python\qgis\utils.py", line 572, in _importmod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named requests
where am I wrong?