Using GIF images in Python GUI scripts

Using GIF images in Python GUI scripts

ironman_explosion

Knocked My Socks Off!


My coworkers and I attended our first JAMF Nation conference this past October. The last session I attended on the first day knocked my socks off. Bryson Tyrrell of JAMF showed off an iPhone ordering script he wrote in Python and Tk. I’ve written some Tk, but it was a long time ago and I remember it being an uncomfortable experience. It seems a lot has changed. His script included an image that would dynamically change based on user interaction. It was so cool I knew I needed to make use of this technique as soon as I could.

The script I’m currently working on will be used to populate inventory data in our Casper management system, collating data from LDAP, MySQL and the JSS. Being able to include images takes the look-and-feel to another level. Showing the user what the appropriate asset tag looks like should increase the likelihood that the correct data will be input.

Screen Shot 2016-02-25 at 6.45.36 PM

Encoding an Image


Here is a simple script that will properly encode a GIF image to be included in a script:

#!/usr/bin/python
import base64
import sys

def main():
    filepath = sys.argv[1]

    print  '*' * 10 + ' copy after this line ' + '*' * 10
    print "variablename='''\\\n" + base64.encodestring(open(filepath, "rb").read(  )) + "'''"
    print  '*' * 10 + ' copy before this line ' + '*' * 10

if __name__ == '__main__':
    main()

The following image of Swoop is shown encoded below.

swoop

./image_encoder_simple.py ~/Desktop/swoop.gif
********** copy after this line **********
variablename='''\
R0lGODlh4QDhAPZCAAAAACUlJU1NTVNTU2lpaXV1dcAAAMAAB8AAC8AAEMEAE8EAFcEAGMIAHMIA
HcIAIMIAIcMAJMMAJsMAKMMAKcQKLMQPLsUSMMciO8cjPMgrQso1Sss6TsxCVM5LXM5OXtBVZNJf
bdNicNVsedVwetd0gNl8h4iIiJKSkqysrLi4uLq6utuFj96QmeCYoOGcpOSmreevtuext+u+w8rK
ytLS0u3DyO7IzPHQ1PPY3PPZ3ODg4Pbg4/jn6frt7/zz9Pz19v77/P///wAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5
BAAAAAAALAAAAADhAOEAAAf+gEKCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWm
p6ipqqunPDYxMC0tMDI4P6y4ubqcPzYsGwzBwRISwgwdMrvKy8yHPzEfw8XBCNXDFxfFIM3c3as6
I8PBGSUwODw/6Tw3LNnYDCbe8vOfOiAMxQgdMD2MIxLYJMgYOMOGDnT0Eipk9GPEAWLweEDSYOGd
OGr6SMDQsbCjQhvELCAY0S9SCYDYUqq8QGGahBAxbnmcuczFAQsSLOCglKHiyp8pcTI4AMIGzaOs
XCBgqUGmJBgMgEpdSYFBhmRIs5K6ceCCBQuVbCDwObWsVwYadibq0QPHqxf+LUiMEEF3BAkXMGZI
1MpXUE8LDG5QerHUrGFsFBCwEKTjxgsWIDJME0aMWEsJl4VVMFGyL00dSytO6nHvsOl3gCnnPK0S

< ... >

AILnbeIwlGzED2QQ0QQ8YDDXPJoFEBACMTo0kTfIAD3TR4EGNGAEMajXDV4Angx0IyeDbEQNVKAC
GuygBkLYAQ1G6sEYWKAB17RALhGQARDIcAY40EEOeHCDrYVgoNtDakUnQYMTDEAFKACAEAgQAKB+
UAYbqJNkUCoBlSagAWtV6S0byYAQXLUSNUCBEFAgABV7CGAAYs2hDqSUgC9ahRkaeIFIJ1GDEwjh
BCoAQAoE0Ncj3sAEGkBAAzba0aONDgQvaF4xapACIaRgBwT4QQEie8YezIAFHxhqW1Way7Sq1SAZ
GMEoUIvbjfiABzaQAQyK2YIXwCAGNriBZ3OL3OQqd7nMba5znwtdYQQCADs=
'''
********** copy before this line **********

Using your Image


Here is a simple Tk script that will display our encoded image:

#!/usr/bin/python
from Tkinter import *
import ttk


def main():

    # encoded image
    swoop = '''\
    R0lGODlh4QDhAPZCAAAAACUlJU1NTVNTU2lpaXV1dcAAA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MQFclgiADD8ymExRkyAZg
    EIRK8KtfZzOgB4XwA/RRgG6U8EjaqkeJIOQgBi5gwQhGYIIWwOAGF3PEnuLxwRw64naxaID9JiGl
    EzrpVy7xnQ4LaIKCaQIBJZKEBrRnk7FlJ2+5SNoRj3iS9DFgdZPgQaxssoEabmR5jeHbFT3ogwcw
    rBqDegQODPAuT3QwKj/oQNgaeMYrkit9w6nhDMqVtg3ArxjpCRsFmJbHK94AjumjWSViED5NlMeM
    0bhBByxFgQwALpEeLN5VDDDHRywsJBJoAQ4yOAwd1CRaiNjLJjnpwduhb2SVwEEn7vSABsyKBTCw
    QQ500IP+YPJAcTBoAQgAyLBmvBKWHrxBA6DIGQsI7QcfSAD6Zja6hQyEIQbBlvsGcQAUMlOHPciA
    EjcxsmXC4JmFoYVCgjbOcYrAmiH54bdKYLx2bsITGdBWPJkZA126bx+lrFcPkshOfXoCBFb75zh9
    AILnbeIwlGzED2QQ0QQ8YDDXPJoFEBACMTo0kTfIAD3TR4EGNGAEMajXDV4Angx0IyeDbEQNVKAC
    GuygBkLYAQ1G6sEYWKAB17RALhGQARDIcAY40EEOeHCDrYVgoNtDakUnQYMTDEAFKACAEAgQAKB+
    UAYbqJNkUCoBlSagAWtV6S0byYAQXLUSNUCBEFAgABV7CGAAYs2hDqSUgC9ahRkaeIFIJ1GDEwjh
    BCoAQAoE0Ncj3sAEGkBAAzba0aONDgQvaF4xapACIaRgBwT4QQEie8YezIAFHxhqW1Way7Sq1SAZ
    GMEoUIvbjfiABzaQAQyK2YIXwCAGNriBZ3OL3OQqd7nMba5znwtdYQQCADs=
    '''

    # setting up the Tk environment
    root = Tk()
    root.title("Swoop!")
    mainframe = ttk.Frame(root, padding=(12, 12, 12, 12))
    mainframe.grid(column=0, row=0)
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    mainframe.columnconfigure(1, weight=1)

    # define the image
    asset_label=ttk.Label(mainframe)
    asset_photoimage=PhotoImage(data=swoop)
    asset_label['image'] = asset_photoimage
    asset_label.grid(column=1, row=1)

    root.mainloop()

if __name__ == '__main__':
    main()

Here’s what the running script looks like:

Screen Shot 2016-02-25 at 8.08.22 PM

Other Options


I’m currently using TKinter/ttk as they are included in the stock Python install, but support is limited to GIF and PGM/PPM formats. However, there are a couple other options when it comes to displaying images in python. The Python Imaging Library (PIL) offered many more image formats, but development appears to have ceased. Pillow forked PIL and is under active development.

 

Tags:
, , , , ,
2 Comments
  • Bryson3Gps
    Posted at 00:15h, 27 February Reply

    Great sample write-up! I’m glad you rolled the idea of the session to borrow the code and concepts to apply to your own solutions. I’ve been using Pillow for an internal Gravatar-like service I’ve been writing for us and it’s powerful library.

    • Todd McDaniel
      Posted at 18:26h, 29 February Reply

      Thank you for your kind words, and your very inspiring presentation!

Leave a Reply