Storing Image Files in a Script (Embedded Images)
Author: Carl Sassenrath This example requires REBOL/View Return to REBOL Cookbook
Sometimes you need a few images in your script. One way is to
put the images in separate files and load them into your script,
but if you want to send the script or have it downloaded, you
will need to figure a way to get the image files too. (Such as
including everything in a zip file or using REBOL to download
the files from a website.)
Another approach is to put the image data directly into your
script, so it can be run without requiring any other files.
This is easy to do.
For example, let's say you have a script that requires an image.
The example below shows how the image might be loaded from a file.
REBOL [Title: "Show Image"]
img: load %clipboard.gif
view layout [
image img
button "Quit" [quit]
]
|
To embed that image within your script, you can convert it to
a REBOL binary datatype, then include it in your script as
data. Two steps are required.
First, you have to convert the image file to a REBOL binary
string. This is easy with a script like this that does the
conversion and saves the binary data as text.
REBOL [Title: "Convert Image"]
system/options/binary-base: 64
file: request-file/only
if not file [quit]
image: read/binary file
save %image-data.txt image
|
The BINARY-BASE line tells REBOL to use BASE-64 binary format
which is more efficient in as text. The result is saved in
the file %image-data.txt. If you open it with a text editor,
you will see:
64#{
R0lGODlhMAAwANQfAAoFBY6NjWEnJ2BYWDYzMzYVFcbGxmNAQJBhYSgnJ0xMTCIN
DbaXl3VAQHJxcfHw8LW1tY54eKGdneHQ0E0eHk04OHVhYaiHh20wMNGzsy4nJ/7+
/qN7e+jf3z49Pf///yH5BAEAAB8ALAAAAAAwADAAAAX/4CeOZGmeaKqubOu+cCzP
dG3feGppQq46A0UJSKh0Cr5TYOlwfDgXBIGQ2WwYyCTJAdlMI5cJQWO1PjhZ7Ufi
8CQUTM/gUd48EGntgOAIOBRvb3V2eFoODAFsCgRviwaDdg15NhEKAxBtCR4KcpBl
DxiTMxFuCx8eBAqcXZ6fAqIwfxoVFh6bHhKtkA+vNLUeVQEaHg50ursUsCoWAx4I
dRKsx54PyS8XChbT24PVyiYIA9zjnwXfJA0HDRPk4w/mLAUC8+zt2+/nIgsUFBj1
9sceHMj3YYE8DB0ATntgId8AABQEIFQYMIJDgxITUmz14MLFiA00btyFZcWBfRlH
wbbCYirFyYgTVQ7qUAGASZQCjI00lgGATxYvJerc+GABAHM2gRYAORTgAQALBFAw
6iIUU4UMfDZoYJCqi2TznrWb4JNCAwELjAJIAANsv3ETjC7YmlbtjH0F9m3DoFVS
Wp9CaOyjAFUXh7INMPyFikMeYQAcBjGQuzWvXR9R0QLIaUWATwwNpqr1oGdzKKgN
fM49qbalmgFRMUwte2D2TzUkYM8zujSvT9K4S5xEu691cBSs1SY9jqImYObQo0v3
EQIAIf4fT3B0aW1pemVkIGJ5IFVsZWFkIFNtYXJ0U2F2ZXIhAAA7
}
|
Now, copy that data to your program where it can be LOADed
instead of the file:
REBOL [Title: "Show Image"]
img: load 64#{
R0lGODlhMAAwANQfAAoFBY6NjWEnJ2BYWDYzMzYVFcbGxmNAQJBhYSgnJ0xMTCIN
DbaXl3VAQHJxcfHw8LW1tY54eKGdneHQ0E0eHk04OHVhYaiHh20wMNGzsy4nJ/7+
/qN7e+jf3z49Pf///yH5BAEAAB8ALAAAAAAwADAAAAX/4CeOZGmeaKqubOu+cCzP
dG3feGppQq46A0UJSKh0Cr5TYOlwfDgXBIGQ2WwYyCTJAdlMI5cJQWO1PjhZ7Ufi
8CQUTM/gUd48EGntgOAIOBRvb3V2eFoODAFsCgRviwaDdg15NhEKAxBtCR4KcpBl
DxiTMxFuCx8eBAqcXZ6fAqIwfxoVFh6bHhKtkA+vNLUeVQEaHg50ursUsCoWAx4I
dRKsx54PyS8XChbT24PVyiYIA9zjnwXfJA0HDRPk4w/mLAUC8+zt2+/nIgsUFBj1
9sceHMj3YYE8DB0ATntgId8AABQEIFQYMIJDgxITUmz14MLFiA00btyFZcWBfRlH
wbbCYirFyYgTVQ7qUAGASZQCjI00lgGATxYvJerc+GABAHM2gRYAORTgAQALBFAw
6iIUU4UMfDZoYJCqi2TznrWb4JNCAwELjAJIAANsv3ETjC7YmlbtjH0F9m3DoFVS
Wp9CaOyjAFUXh7INMPyFikMeYQAcBjGQuzWvXR9R0QLIaUWATwwNpqr1oGdzKKgN
fM49qbalmgFRMUwte2D2TzUkYM8zujSvT9K4S5xEu691cBSs1SY9jqImYObQo0v3
EQIAIf4fT3B0aW1pemVkIGJ5IFVsZWFkIFNtYXJ0U2F2ZXIhAAA7
}
view layout [
image img
]
|
Of course, you can include as many images as you want in this same way.
They are like any other string data, so you can assign them to variables
and load them later. For example:
clipboard-gif: 64#{
R0lGODlhMAAwANQfAAoFBY6NjWEnJ2BYWDYzMzYVFcbGxmNAQJBhYSgnJ0xMTCIN
DbaXl3VAQHJxcfHw8LW1tY54eKGdneHQ0E0eHk04OHVhYaiHh20wMNGzsy4nJ/7+
...
fM49qbalmgFRMUwte2D2TzUkYM8zujSvT9K4S5xEu691cBSs1SY9jqImYObQo0v3
EQIAIf4fT3B0aW1pemVkIGJ5IFVsZWFkIFNtYXJ0U2F2ZXIhAAA7
}
img: load clipboard-gif
|
This same approach works for sounds and any other type of files
(even executable files).
|