User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

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

  #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 7:02 am. Reason: Formatting
May 'the Google' be with you!
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 9:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC