vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Intro

In part 1 we learned mostly about the header of the JPEG file. In this part we get to display the image and also extract and show any embedded comments.

wxPython

To display the image we use wxPython. The raw JPEG image information is first pushed into a data stream, which is then read and converted into a bitmap by wxPython. The bitmap is displayed on a wx.Panel, but it could have been other widgets too. The extracted comment text is assigned to the value of a wx.TextCtrl, configured here as a multiline edit box. Comment texts can be up to 64k in size and the edit box allows for scrolling of large text.

# show a jpeg image with optional comment text using wxPython
# tested with Python24 and wxPython25   vegaseat    12oct2005

import wx
import  cStringIO

class Panel1(wx.Panel):
    """class Panel1 creates a panel with an image and an edit box, inherits wx.Panel"""
    def __init__(self, parent, id, comment, jpgImage):
        # create a panel with white background uses an rgb-tuple
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour((255,255,255))
        # create the edit box to show any comment, make it multiline for long comments
        self.edit1 = wx.TextCtrl(self, id, value=comment, pos=(0,310), size=(495,50), style=wx.TE_MULTILINE)
        self.edit1.SetBackgroundColour((255,248,220))
        # convert jpg image to a data stream
        stream = cStringIO.StringIO(jpgImage)
        # convert data stream to a bitmap
        bmp = wx.BitmapFromImage(wx.ImageFromStream(stream))
        # show the bitmap, (5, 5) are upper left corner coordinates
        wx.StaticBitmap(self, -1, bmp, (5, 5))


try:
    # the sample jpeg file is an "all blue 80x80 200dpi image, saved at a quality of 90"
    # with this quoted comment added
    imageFile = 'Blue80x80x200C.JPG'
    fin = open(imageFile, "rb")
    jpgImage = fin.read()
    fin.close()
except IOError:
    print "Image file %s not found" % imageFile
    raise SystemExit

# create a list of hexbytes to extract information from
hexList = []
for ch in jpgImage:
    byt = "%02X" % ord(ch)
    hexList.append(byt)

# find any comment inserted into the jpeg image
comment = ""
for k in range(len(hexList)-1):
    if hexList[k] == 'FF' and hexList[k+1] == 'FE':
        length = int(hexList[k+2],16)*256 + int(hexList[k+3],16)
        for m in range(length-3):
            comment = comment + chr(int(hexList[k + m + 4],16))
            
if len(comment) == 0:
    comment = "No comment"

# now work on the wxPython application
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID  (change size as needed)
frame1 = wx.Frame(None, -1, "JPG Image and Comment", size = (500, 400))
# call the derived class
Panel1(frame1, -1, comment, jpgImage)
frame1.Show(1)
# start the event loop, get the show on the road
app.MainLoop()

Conclusion

We are using the same experimental JPEG file that we used in part 1. You can change that to a JPEG (.jpg) file you have, just put it into the working directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.