•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,613 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 2329 | Replies: 0
![]() |
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation:
Rep Power: 9
Solved Threads: 172
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.
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 7:02 am. Reason: Formatting
May 'the Google' be with you!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- please help!! (Windows NT / 2000 / XP / 2003)
- Python and the JPEG Image File, Part 1, The Header (Python)
- Inserting a jpeg image into a MSWord document. Max OS X. (Mac Software)
Other Threads in the Python Forum
- Previous Thread: calling python with .net language
- Next Thread: Ways to create Dictionary



Linear Mode