# Base64 string (image) to node red

**URL:** <https://discourse.nodered.org/t/base64-string-image-to-node-red/45815>\
**Category:** Dashboard\
**Created:** [18 May 2021 07:19 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815 "2021-05-18T07:19:15Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 07:19 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/1 "2021-05-18T07:19:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 07:43 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/2 "2021-05-18T07:43:53Z")

</div>

> [@Chorum](#):
>
> is there an easy way to get that VERY big text into a node red variable

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

You dont state where this image is coming from.

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 07:47 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/3 "2021-05-18T07:47:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 07:48 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/4 "2021-05-18T07:48:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 07:49 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/5 "2021-05-18T07:49:59Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [18 May 2021 08:30 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/6 "2021-05-18T08:30:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 09:37 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/7 "2021-05-18T09:37:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 May 2021 09:42 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/8 "2021-05-18T09:42:05Z")

</div>

You could maybe look at the mosquitto publish command line and just publish the file  
[https://mosquitto.org/man/mosquitto\_pub-1.html](https://mosquitto.org/man/mosquitto_pub-1.html)

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 09:54 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/9 "2021-05-18T09:54:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 May 2021 10:05 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/10 "2021-05-18T10:05:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 10:11 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/11 "2021-05-18T10:11:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [18 May 2021 11:27 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/12 "2021-05-18T11:27:42Z")

</div>

Well, yes

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

> <https://stackoverflow.com/questions/62576361/how-can-i-download-paho-and-pip-on-my-windows-10>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/987f9bde8976452b2133cf4215806a55f3f515e3.png)

```auto
#!/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()

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [18 May 2021 11:35 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/13 "2021-05-18T11:35:23Z")

</div>

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

```auto
#!/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()

```

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 11:43 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/14 "2021-05-18T11:43:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [18 May 2021 11:48 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/15 "2021-05-18T11:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [18 May 2021 11:50 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/16 "2021-05-18T11:50:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [18 May 2021 11:50 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/17 "2021-05-18T11:50:54Z")

</div>

Good luck!

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [2 June 2021 18:50 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/18 "2021-06-02T18:50:59Z")

</div>

So ... i wasnt able to copy the big string to a function node, but it worked on a inject node 🙂  
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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 July 2021 18:51 UTC](https://discourse.nodered.org/t/base64-string-image-to-node-red/45815/19 "2021-07-02T18:51:29Z")

</div>

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