Sunday, July 17, 2022

[FIXED] How to convert gif to text, for inline access from a tk script?

Issue

I saw the following code in the famous tkdiff utility:

image create photo rediffImage -format gif -data {
R0lGODdhFAAUAPf/AAAAAIAAAACAAICAAAAAgIAAgACAgMDAwMDcwKbK8P/w1P/isf/Ujv/G
......
}

I have a gif image to include in my tk script. How can I convert it to data like this so that I don't have to call the separate gif file ?


Solution

It's base64 data. Tcl 8.6 includes a command for doing the conversion.

# Read the data in *as binary data*
set f [open yourfile.gif b]
set data [read $f]
close $f

# Do the encoding
set encodedData [binary encode base64 $data]

# Print the encoded version out; it's just plain ASCII text now
puts $encodedData

Note that you can add whatever whitespace you want to base64 data; it remains valid. This means that you can split up and indent the lines however you see fit.



Answered By - Donal Fellows
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.