473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

unpack problem

2
i have never contacted to python language before this, i want to use the following py file to convert a pgm image to *.coe file, which is initial file of Xilinx memory.And this py file was written by I. Chuang, MIT, downloaded from MIT web site.
i just use python 2.4(for windows idle) to run this file,but it shows error:

Traceback (most recent call last):
File "D:\python\pgm2coe.py", line 31, in -toplevel-
imgbytes = struct.unpack('%dB' % ndat,imgdat)
error: unpack str size does not match format
Thanks for your help!

the py file:pgm2coe.py
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. #
  3. # File:   pgm2coe.py
  4. # Date:   14-Nov-05
  5. # Author: I. Chuang <ichuang@mit.edu>
  6. #
  7. # Q&D conversion of PGM format image file to COE file for Xiling FPGA memory
  8. # initialization.
  9.  
  10. import string,re,sys,os,struct
  11.  
  12. fn = "flowers2-bw-320-240"
  13.  
  14. fp = open('%s.pgm' % fn)
  15.  
  16. s = fp.readline()    # P5
  17.  
  18. s = fp.readline()
  19. m = re.compile('([0-9]+) ([0-9]+)').search(s)
  20. nx = string.atoi(m.group(1))
  21. ny = string.atoi(m.group(2))
  22.  
  23. print "nx = %d, ny=%d" % (nx,ny)
  24.  
  25. s = fp.readline()    # maxval
  26.  
  27. ndat = nx*ny
  28.  
  29. imgdat = fp.read(ndat)
  30.  
  31. imgbytes = struct.unpack('%dB' % ndat,imgdat)
  32.  
  33. print "size = %d" % len(imgbytes)
  34.  
  35. fp.close()
  36.  
  37. fp = open('%s.coe' % fn,'w')
  38.  
  39. fp.write("memory_initialization_radix=16;\nmemory_initialization_vector=\n")
  40.  
  41. for j in range(ny):
  42.     for k in range(nx):
  43.     fp.write("%02x," % imgbytes[k+j*nx])
  44.     fp.write("\n");
  45.  
  46. fp.close()
Aug 1 '07 #1
3 1866
bartonc
6,596 Expert 4TB
Try:
Expand|Select|Wrap|Line Numbers
  1. imgdat = fp.read()
  2. # ndat = nx*ny
  3. ndat = len(imgdat)#
  4.  
  5. # imgdat = fp.read(ndat)
on lines 26 - 29.

The error that you are getting is due to the calculated size and the actual size differing. One reason could be that the parsing of the size on lines 18 - 21 is not functioning correctly. Or it could be that the entire file is not being read due to buffering. This test should tell which is happening by viewing the result.
Aug 1 '07 #2
echo
2
Try:
Expand|Select|Wrap|Line Numbers
  1. imgdat = fp.read()
  2. # ndat = nx*ny
  3. ndat = len(imgdat)#
  4.  
  5. # imgdat = fp.read(ndat)
on lines 26 - 29.

The error that you are getting is due to the calculated size and the actual size differing. One reason could be that the parsing of the size on lines 18 - 21 is not functioning correctly. Or it could be that the entire file is not being read due to buffering. This test should tell which is happening by viewing the result.
thanks for your replying!
i followed your method and solved the problem above, but another error comes out:
>>>
nx = 320, ny=240
size = 13492

Traceback (most recent call last):
File "D:\python\pgm2coe.py", line 45, in <module>
fp.write("%02x," % imgbytes[k+j*nx])
IndexError: tuple index out of range
i am really newbie to python, so hope you can help me again.thanks!
PS: my *.pgm image file is 320x240.
Aug 1 '07 #3
bartonc
6,596 Expert 4TB
I've detected "mixed mode" (spaces and tabs) indentation here:
Expand|Select|Wrap|Line Numbers
  1. for j in range(ny):
  2.     for k in range(nx):
  3.     fp.write("%02x," % imgbytes[k+j*nx])
  4.     fp.write("\n");
it should look like:
Expand|Select|Wrap|Line Numbers
  1. for j in range(ny):
  2.     for k in range(nx):
  3.         fp.write("%02x," % imgbytes[k+j*nx])
  4.     fp.write("\n");
That could be the problem if the indexing math is good (my brain is to fried at the moment to process that part).
Aug 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data =...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
6
by: g.franzkowiak | last post by:
Hello Everybody, I've read a pipe and store it in a object. My next step was the separation from 4 bytes with obj = string.join(list(dataObject) ==> '\x16 \x00 \x00 \x00' and the converting by...
3
by: Andrew Robert | last post by:
Hey everyone, Maybe you can see something I don't. I need to convert a working piece of perl code to python. The perl code is: sub ParseTrig {
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
17
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a...
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.