473,765 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting a short using struct - won't print.

Thanks to Richard and Alex, I'm doing this:

seg_stru=">BHH"
seg_data=self.f hand.read(struc t.calcsize(seg_ stru))
data=struct.unp ack(seg_stru,se g_data)
x=seg_data[1]
y=seg_data[2]
if debug:
print ("DEBUG: Image [%s] is [%d] by [%d] pixels" % (self.fname,x,y ))

Expecting to extract a filler byte (ignored), then a big-endian short (a
python integer) , then another of the same. When I try to print, I get
this:

File "./pdf_merge", line 426, in getsize
print ("DEBUG: Image [%s] is [%d] by [%d] pixels" % (self.fname,x,y ))
TypeError: int argument required

What am I missing - its probably something simple - earlier I spent half an
hour wondering why I was getting a message that "img_file has no object
getsize()" from x,y=img.getsize (). It turned out to be because I had got
def getsize: indented one indent too far in the class definition. Aaargh!

Python is 2.3b1.

Thanks
--
Graham Nicholls
Rock Computer Consultancy

Jul 18 '05 #1
5 2102
Peter Hansen wrote:
Graham Nicholls wrote:

Thanks to Richard and Alex, I'm doing this:

seg_stru=">BHH"
seg_data=self.f hand.read(struc t.calcsize(seg_ stru))
data=struct.unp ack(seg_stru,se g_data)
x=seg_data[1]
y=seg_data[2]
Why are you unpacking the struct if you aren't going to use the
unpacked data?


Err, I'm trying to extract two shorts - the x and y size in pixels of an
image, which I'll use to scale it to fit another pair of parameters. What
makes you think I'm not using the data? Am I not - I thought I was!

I realise that ">BHH" above ouhjt really to be "xHH" but aside from that, I
don't understand your point - theres more code after the debug in the rel
app - returning x and y.
if debug:
print ("DEBUG: Image [%s] is [%d] by [%d] pixels" %
(self.fname,x,y ))
Best way to troubleshoot this kind of thing, in my experience,
is to insert "import pdb; pdb.set_trace() " into the code just
above the failing line, then use the debugger to inspect the
values directly just before they are used. (You need to execute
the "r" or "return" command immediately after the debugger is
entered... the rest is pretty straightforward to learn.)

I'll give that a go, thanks -Peter


--
Graham Nicholls
Rock Computer Consultancy

Jul 18 '05 #2

"Graham Nicholls" <gr****@rockcon s.co.uk> wrote in message
news:3f******** *************** @auth.uk.news.e asynet.net...
seg_stru=">BHH"
seg_data=self.f hand.read(struc t.calcsize(seg_ stru))
data=struct.unp ack(seg_stru,se g_data)
x=seg_data[1]
y=seg_data[2]


Why are you unpacking the struct if you aren't going to use the
unpacked data?


Err, I'm trying to extract two shorts - the x and y size in pixels of an
image, which I'll use to scale it to fit another pair of parameters. What
makes you think I'm not using the data? Am I not - I thought I was!


You aren't using the returned value from struct.unpack() anywhere.
You need something like x, y = struct.unpack(s eg_stru,seg_dat a)
Jul 18 '05 #3
Graham Nicholls wrote:
Peter Hansen wrote:
Graham Nicholls wrote:

Thanks to Richard and Alex, I'm doing this:

seg_stru=">BHH"
seg_data=self.f hand.read(struc t.calcsize(seg_ stru))
data=struct.unp ack(seg_stru,se g_data)
x=seg_data[1]
y=seg_data[2]
Why are you unpacking the struct if you aren't going to use the
unpacked data?


Err, I'm trying to extract two shorts - the x and y size in pixels of an
image, which I'll use to scale it to fit another pair of parameters. What
makes you think I'm not using the data? Am I not - I thought I was!

I realise that ">BHH" above ouhjt really to be "xHH" but aside from that,
I don't understand your point - theres more code after the debug in the
rel app - returning x and y.
if debug:
print ("DEBUG: Image [%s] is [%d] by [%d] pixels" %
(self.fname,x,y ))


Best way to troubleshoot this kind of thing, in my experience,
is to insert "import pdb; pdb.set_trace() " into the code just
above the failing line, then use the debugger to inspect the
values directly just before they are used. (You need to execute
the "r" or "return" command immediately after the debugger is
entered... the rest is pretty straightforward to learn.)

I'll give that a go, thanks
-Peter

OK, the code now looks like this:

seg_stru=">xHH"
seg_data=self.f hand.read(struc t.calcsize(seg_ stru))
data=struct.unp ack(seg_stru,se g_data)

x=seg_data[1]
y=seg_data[2]
import pdb ; pdb.set_trace()
if debug:
print ("DEBUG: Image [%s] is [%d] by [%d] pixels" % (self.fname,x,y ))
return(x,y)

When I run it, I get this:

EBUG: Scaling image file hb.jpg to fit 100 100
DEBUG: Searching hb.jpg for dimensions
DEBUG: found header type e0
DEBUG: found header type db
DEBUG: found header type db
DEBUG: found header type c0
--Return-- /usr/local/lib/python2.3/pdb.py(972)set_ trace()->None -> Pdb().set_trace ()
(Pdb)
(Pdb) r /home/graham/work/hsb/pdflive/pdf_merge(427)g etsize()

-> if debug:
(Pdb) print x

(Pdb) print y
m
(Pdb)

Which _seems_ to indicate that unpack is not unpacking the shorts as shorts,
but as chars. Is this right?

Thanks
Graham
--
Graham Nicholls
Rock Computer Consultancy

Jul 18 '05 #4
Graham Nicholls wrote:
Richard Brodie wrote:
....snipped

I told you it'd be something stupid!
I mean
x=data[1]
y=data[2]
don't I!
Thanks!!!!
Graham


Which, of course works - one of the annoying things about learning a
language is that you tend to assume its something more complex than it
really is when you get an error. I'd see that in 2 seconds flat in a c
program , of course, but then the compiler would help..

BTW How does one test python programs - because its interpreted, it often
won't run bits of code till unexpected circumstances, so a syntax error can
be hidden until runtime?

Graham
--
Graham Nicholls
Rock Computer Consultancy

Jul 18 '05 #5
Peter Hansen wrote:
Graham Nicholls wrote:


don't I!
Yes, you do! :-) Sorry for not being more obvious about the answer,
and I thought you might figure it out yourself by using the "import pdb"
trick I showed. "Teach a man to fish" and all that. <grin>


Too tired to fish, but thanks, I suspect will be a useful technique.
Graham
-Peter


--
Graham Nicholls
Rock Computer Consultancy

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8003
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
11
6600
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
2
2816
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to C#. The only significant problem that I have encountered in the conversion is this one - extracting an icon from the 'KTEntryPoint' program into the software suite and placing that icon on the PC Desktop.
4
7919
by: crafuse | last post by:
Hello, I've overridden the WndProc function in my form to hand some special behavior. Specifically, I look for the WM_NCMOUSEMOVE event to tell me when the user is trying to move the window by draggin the title bar. However, I am having trouble extracting the POINT structure that is supposed to come with the message. For example: Structure POINTS Public x As Short
16
10977
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some pointers where to get started would be much appreciated. An example text file: ----------- Some text that can span some lines.
4
3217
by: bejiz | last post by:
Hello, I have written a short program for practising linked lists. But there is surely something wrong for when I compile there is a unhandled exception and it does not print if I try to add more than one element to the list. Is this a problem with the function of the internal class Node ? Thanks for any help. Here is the code: #include<iostream>
4
2652
by: Kosmos | last post by:
Hey guys...as a relatively new programmer, I try to give back where I can. Below is the code for a useful program I believe many of you could use...just don't use the same exact code because...well I don't work for myself if you get my drift....anyways even if this is not asked of you...it might get you on friendly terms with the lawyers in the office The code is written to extract information from a table with contract information and divide...
2
3797
by: Steve Holden | last post by:
Fredrik Lundh wrote: Thanks so much, Fredrik. The reason I asked is because I found the specification completely opaque ... regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/
7
3143
by: latalui | last post by:
i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS. check out what is wrong the code i tried /program to find shortest path #include<stdio.h> #include<cstdlib> #include<cstring> #include<fstream> #include<iostream.h> #define member 1 // use to indicate current node
0
9398
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
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
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
6649
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.