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 - #!/usr/bin/python
-
#
-
# File: pgm2coe.py
-
# Date: 14-Nov-05
-
# Author: I. Chuang <ichuang@mit.edu>
-
#
-
# Q&D conversion of PGM format image file to COE file for Xiling FPGA memory
-
# initialization.
-
-
import string,re,sys,os,struct
-
-
fn = "flowers2-bw-320-240"
-
-
fp = open('%s.pgm' % fn)
-
-
s = fp.readline() # P5
-
-
s = fp.readline()
-
m = re.compile('([0-9]+) ([0-9]+)').search(s)
-
nx = string.atoi(m.group(1))
-
ny = string.atoi(m.group(2))
-
-
print "nx = %d, ny=%d" % (nx,ny)
-
-
s = fp.readline() # maxval
-
-
ndat = nx*ny
-
-
imgdat = fp.read(ndat)
-
-
imgbytes = struct.unpack('%dB' % ndat,imgdat)
-
-
print "size = %d" % len(imgbytes)
-
-
fp.close()
-
-
fp = open('%s.coe' % fn,'w')
-
-
fp.write("memory_initialization_radix=16;\nmemory_initialization_vector=\n")
-
-
for j in range(ny):
-
for k in range(nx):
-
fp.write("%02x," % imgbytes[k+j*nx])
-
fp.write("\n");
-
-
fp.close()
3 1866
Try: -
imgdat = fp.read()
-
# ndat = nx*ny
-
ndat = len(imgdat)#
-
-
# 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.
Try: -
imgdat = fp.read()
-
# ndat = nx*ny
-
ndat = len(imgdat)#
-
-
# 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.
I've detected "mixed mode" (spaces and tabs) indentation here: -
for j in range(ny):
-
for k in range(nx):
-
fp.write("%02x," % imgbytes[k+j*nx])
-
fp.write("\n");
it should look like: -
for j in range(ny):
-
for k in range(nx):
-
fp.write("%02x," % imgbytes[k+j*nx])
-
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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 =...
|
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:...
|
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...
|
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...
|
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 {
|
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...
|
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...
|
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',...
|
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...
|
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='"....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |