Python and the JPEG Image File, Part 2, The Image

Reply

Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Python and the JPEG Image File, Part 2, The Image

 
0
  #1
Oct 12th, 2005
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.

[php]# 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()
[/php]

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.
Last edited by happygeek; Oct 29th, 2006 at 8:02 am. Reason: Formatting
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC