Hardcoding Images

Hi, I got a weird one for you,

Is it possible to hardcode an image into a dynamo graph?

I found the node that read every pixel, returning a list of list or RGBA and making it a single long string, but I havent found a conscise way to reconvert that string into RGBA objects.

My scripts are run by people with changing accesses to our server and would also like to have our logo to appear even if the scripts are running outside of our servers.

Tried Image.FromPixels yet?

1 Like

why not use the c# image save method to get a string, dump it into a code block -and then use

image from stream to convert it to an image again, something like that.

Like Andreas said though - you can do the same with pixels and fromPixels if you write some code to parse the string back to colors.

1 Like

Thanks y’all.

C# is uncharted territory for me, I wouldn’t dare try just yet.

Otherwise, I took the image, broke it down with Image.Pixel, broke those pixels down to their individual color values using Color,Alpha/Red/Green/Blue and flushed that to a file. I intend to use Image,FromPixels to recreate it.

However, Dynamo doesnt like to be pasted a 270 000 characters long string and frequently crashes. Trying to find a workaround.

@slepagetrudeau if you could upload the logo image to the web, this Python method to save the image in the user’s temp folder might work for you:

Python script:

import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
from urllib import urlretrieve

url = 'http://dynamobim.org/wp-content/themes/dynamo/img/icon.png'
file = 'C:\TEMP\logo.png'
urlretrieve(url, file)

OUT= file

The first 3 nodes could be bundled into a custom node to make it cleaner for adding your logo to graphs :slight_smile:

6 Likes

Try:

  1. A smaller version of your image.
  2. Lowering the initial sample resolution (the x and Y sample rate when you converted to text)
  3. Breaking apart the one string into 4 different strings, one for A, one for R, one for G, and one for B.

What @Michael_Kirschner2 suggested is also possible in Python if you’re more comfortable with that:
https://www.programcreek.com/2013/09/convert-image-to-string-in-python/

1 Like

Technically, you already have all of the tools that you need available. The only problem with it is that it ends up being a very inefficient method:

SerializeImage.dyn (132.9 KB)

2 Likes

Wow I didn’t expect so many solutions.

I’m almost there, I ended up using bits and peices of all your ideas.

I scraped the idea of going pixel by pixel, it just ended up being too large and ineficient, as @Dimitar_Venkov demonstrated. I wanted more of a single node I could pop whenever I wanted. I also didnt feel like losing image resolution.

I decided to encode the image with base64 as @Andreas_Dieckmann sugested, but it didn’t recognise Base64 as a module so I used binascii instead.

With the ascii encoded image string I can now recreate the image by decoding it and saving it in a temp file as @awilliams proposed, I chose the current working directory, for simplicity.

I’m down to the last two hurdles:

  • I want to include the File.FromPath and Image.ReadFromFile nodes in the python script and I am unsure of the proper syntax

  • I want to include the Ascii string in the python script without it throwing me a: EOL while scanning single-quoted string. I assume it’s about backslashes or that the ascii might have quotes. Could it be compressed even more with other forms of encoding?

HardCodedLogo.dyn (67.0 KB)

EDIT:

Point two has been fixed using tripple quoted string. I would have known earlier if I knew how to read. Thanks @Dimitar_Venkov

4 Likes

How to make the current python script work?
it doesnt work in my python node for the current dyanamo release.

Do i need to donwload ironPython?

It might be easier to hard-code the image into the graph using a remember node, but I’m not sure if it’s supported.