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 - #!/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 1897 bartonc 6,596
Recognized Expert Expert
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\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.
bartonc 6,596
Recognized Expert Expert
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 = struct.pack('iiii', 1, 2, 3, 4)
>>> struct.unpack('i(ii)i', data) # Note the parentheses
(1, (2, 3), 4)
|
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
#
|
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
|
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.
|
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 something
tdc = struct.unpack('=LiLiLiLi',self.lmf.read(32))
# do something
|
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)
|
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)))
|
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>...
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |