By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

unpack problem

P: 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
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 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
Expert 5K+
P: 6,596
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

Post your reply

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