Base64 string (image) to node red

Hello,
i need to use an image on a node red with no access to folders on it.
so i would like to use a base64 string(externally created) for displaying the image on an html entpoint.

my question:
is there an easy way to get that VERY big text into a node red variable?

Greetings
Chorum

HTTP Request, MQTT, TCP, UDP, read from database?

You dont state where this image is coming from.

atm i have it in a *.txt file. i am looking for a way to get it into a node red variable.

use a file in node to read the file into flow or global context, then you can return it to your endpoint whenever it is called.

i would like to do so , but like i wrote - i dont have any folder access for the node red user

but i will check something..

Write a Python script where the file is located, read it in and send it to NR via MQTT over a network
Would that not work?

Ok sounds good. i will try that. whats the simpliest method to script python under windows 10 ?

You could maybe look at the mosquitto publish command line and just publish the file
https://mosquitto.org/man/mosquitto_pub-1.html

i have no direct access to that node red. so i would use mosquitto publish with python, is that right?

i will try that , as soon as i can.
thanks to all for the ideas!

dearly
Chorum

if you use the command line publish program you may not need python ... maybe just a batch file to help automate it.

i did not setup our node red/mqtt-broker. Do i need a mosquitto install to use the command line program ?

Well, yes

If you are willing to try the Python way, start here:

Check your Python version but I would go for Python3 right away
Check your Python version: python3
Then install paho-mqtt as per instruction: pip3 install paho-mqtt

Below is a simple Python script based on one of mine. It is very simple, it takes the argument (path and file name of your image), connects to your MQTT broker (so yes, you need one as well or a MQTT cloud service, creates a bytearray of your image and sends it to a topic (images). In Node-RED you sbscribe to the same topic and use one of several image viewers. Like the example below

In the script you have to change the ip of the MQTT broker to make it work. To test from cmd line:
python3 img_send.py -p path_and_name_of_image

image

#!/usr/bin/python
# USAGE
# python img_send.py -p path_and_name_of_image
# or
# python3 img_send.py -p path_and_name_of_image

import argparse
import paho.mqtt.client as mqtt

def on_publish(mosq, userdata, mid):
    mosq.disconnect()
    del mosq
    exit(0)

ap = argparse.ArgumentParser()
ap.add_argument(
    "-p", 
    "--pic", 
    required=True, 
    help="filename"
)
args = vars(ap.parse_args())

client = mqtt.Mosquitto()
client.connect("127.0.0.1", 1883, 60)
client.on_publish = on_publish

f = open(args['pic'], "rb")
fileContent = f.read()
f.close()

byteArr = bytearray(fileContent)

client.publish("images", byteArr, 1, False)
client.loop_forever()

Oh yes, I see now you wanted images as Base64 strings. Then use this script instead

#!/usr/bin/python
# USAGE
# python img_send.py -p path_and_name_of_image
# or
# python3 img_send.py -p path_and_name_of_image

import argparse
import paho.mqtt.client as mqtt
import base64

def on_publish(mosq, userdata, mid):
    mosq.disconnect()
    del mosq
    exit(0)

ap = argparse.ArgumentParser()
ap.add_argument(
    "-p", 
    "--pic", 
    required=True, 
    help="filename"
)
args = vars(ap.parse_args())

client = mqtt.Mosquitto()
client.connect("127.0.0.1", 1883, 60)
client.on_publish = on_publish

f = open(args['pic'], "rb")
fileContent = f.read()
f.close()

jpg_as_text = base64.b64encode(fileContent)

client.publish("images", jpg_as_text, 1, False)
client.loop_forever()

So if i got it right , it follows that path:

Python script (read file, build MQTT Message, send MQTT Message) - > MQTT Broker(take payload, and display the image)

paho-mqtt is for building end sending mqtt messages ?

am i right?

Yes, the path you describe is partly correct

Python script (read file, build MQTT Message, send MQTT Message) - > MQTT Broker -> NR subscribe (take payload, and display the image)

paho-mqtt is the necessary python client that you use to do MQTT connections/subscriptions/publishing. This is necessary to have. I found paho-mqtt working very well but there are other alternatives for Python as well like earlier mentioned mosquitto client. You also need to install a MQTT broker if you do not have it already. There are many hits if you google that

ok, thank you very much !
i will go that path as soon as i can... maybe next week...

Good luck!

So ... i wasnt able to copy the big string to a function node, but it worked on a inject node :slight_smile:
So i have the string in node red and can work with it ...narv
thx for youre ideas, maybe i will use that method to get other data to node red :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.