473,977 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unpack problem

2 New Member
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\pgm2 coe.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 1897
bartonc
6,596 Recognized Expert Expert
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 New Member
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\pgm2 coe.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 Recognized Expert Expert
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
1950
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 = struct.pack('iiii', 1, 2, 3, 4) >>> struct.unpack('i(ii)i', data) # Note the parentheses (1, (2, 3), 4)
5
13910
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: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
5
5469
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 coming through fine when using (integer1,) = struct.unpack('l', line) except when line contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that
6
4539
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 value = struct.unpack('I', obj) generated the error "unpack str size does not match format" Unfortunately is len(obj) 7, but integer lengt 4.
3
3863
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
2816
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 something tdc = struct.unpack('=LiLiLiLi',self.lmf.read(32)) # do something
2
4418
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 = struct.unpack('h', testValue)
0
1631
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', self.__read(src, 2))) header = int(struct.unpack('1i', self.__read(src, 4)))
17
2336
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='".$file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='".$file_name."&order_by=location'>Location</a></th> <th><a href='".$file_name."&order_by=sighting_date_asc'>Sighting Date</a></th> <th><a href='".$file_name."&order_by=classification'>Code</a></th>...
19
1896
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='". $file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='". $file_name."&order_by=location'>Location</a></th> <th><a href='". $file_name."&order_by=sighting_date_asc'>Sighting
0
10356
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10916
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8464
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.