Reversing bits in a byte

2013-03-12 Thread Robert Flintham
Hi,

I have a 'bytes' object which contains a simple bitmap image (i.e. 1 bit per 
pixel).  I can't work out how I would go about displaying this image.  Does 
anyone have any thoughts?

All the best,
Rob


Robert Flintham
Trainee Clinical Scientist - MRI
Tel:

 +44 (0)121 371 7000

Email:

 robert.flint...@uhb.nhs.uk

Web:

 http://www.uhb.nhs.uk


We're bringing the world's most advanced cancer treatments to Birmingham.
Find out more at www.qecancerappeal.org<http://www.qecancerappeal.org> or text 
QEHB01 £5 to 70070 to donate £5 to our appeal.

RRPPS
Medical Physics - University Hospitals Birmingham NHS Foundation Trust
63 Melchett Road, Kings Norton,
Birmingham, B30 3HP

[cid:image001.gif@01CE1E6D.AF57F9D0]
DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.
<>-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Reversing bits in a byte

2013-03-12 Thread Robert Flintham
Sorry, the subject line was for a related question that I decided not to ask, I 
forgot to change it when I changed my email.  I've changed it now!

I'm using Python 3.3 on Windows with the pydicom module 
(http://code.google.com/p/pydicom/).  Using pydicom, I've ended up with a 
"bytes" object of length (512*512/8 = 32768) containing a 512x512 1-bit bitmap 
(i.e. each byte represents 8 pixels of either 1 or 0).  When I print this to 
screen I get:
'b\x00\x00\x00.'

I can unpack this to a tuple of the integer representations of binary data, but 
that doesn't really help as presume I need the binary (8 digit) representation 
to be able to translate that into an image.

I wasn't sure which GUI library to use, so haven't specified one.  As it's 
Python 3, Tkinter is available.  I also have matplotlib and numpy installed, 
and PIL.

Ideally, I'd like to be able to access the pixel data in the form of a numpy 
array so that I can perform image-processing tasks on the data.

So now that I've explained myself slightly more fully, does anyone have any 
thoughts on how to do this?

All the best,
Rob

Robert Flintham
Trainee Clinical Scientist - MRI

Tel:  +44 (0)121 371 7000
Email:   robert.flint...@uhb.nhs.uk
Web:  http://www.uhb.nhs.uk

We're bringing the world's most advanced cancer treatments to Birmingham.
Find out more at www.qecancerappeal.org or text QEHB01 £5 to 70070 to donate £5 
to our appeal.

RRPPS
Medical Physics - University Hospitals Birmingham NHS Foundation Trust
63 Melchett Road, Kings Norton,
Birmingham, B30 3HP


ð Delivering the best in care



-Original Message-
From: Python-list 
[mailto:python-list-bounces+robert.flintham=uhb.nhs...@python.org] On Behalf Of 
Dave Angel
Sent: 12 March 2013 12:47
To: python-list@python.org
Subject: Re: Reversing bits in a byte

On 03/11/2013 11:32 AM, Robert Flintham wrote:
> Hi,
>
> I have a 'bytes' object which contains a simple bitmap image (i.e. 1 bit per 
> pixel).  I can't work out how I would go about displaying this image.  Does 
> anyone have any thoughts?
>
> All the best,
> Rob
>
>

How does your subject line relate to your question?

But more importantly, what version of Python, what OS, and which GUI library 
(wxpython, qt, etc.) are you used to?  Specify those, and somebody familiar 
with that particular library will probably pop up with an answer.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list

DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Reversing bits in a byte

2013-03-12 Thread Robert Flintham
Further to my earlier reply to Dave:

I'd like to either display the image in a GUI, or save it in a format that can 
be opened easily in Windows (like a PNG or a 24-bit BMP).

I know the dimensions as it's coming from the header of a DICOM file.  I'm 
trying to analyse DICOM images where an 'overlay' image is stored as a bitmap 
in the header information.  So the bitmap data is one DICOM tag (6000,3000) and 
the height and width of the overlay are in two other tags (6000,0010) and 
(6000,0011).

All the best,
Rob

Robert Flintham
Trainee Clinical Scientist - MRI

Tel:  +44 (0)121 371 7000
Email:   robert.flint...@uhb.nhs.uk
Web:  http://www.uhb.nhs.uk

We're bringing the world's most advanced cancer treatments to Birmingham.
Find out more at www.qecancerappeal.org or text QEHB01 £5 to 70070 to donate £5 
to our appeal.

RRPPS
Medical Physics - University Hospitals Birmingham NHS Foundation Trust
63 Melchett Road, Kings Norton,
Birmingham, B30 3HP


ð Delivering the best in care



-Original Message-
From: Tim Chase [mailto:python.l...@tim.thechases.com] 
Sent: 12 March 2013 13:21
To: Robert Flintham
Cc: 'python-list@python.org'
Subject: Re: Reversing bits in a byte

On 2013-03-11 15:32, Robert Flintham wrote:
> I have a 'bytes' object which contains a simple bitmap image (i.e.
> 1 bit per pixel).  I can't work out how I would go about displaying 
> this image.  Does anyone have any thoughts?

You'd need to detail
- how you want to display it (console, GUI, web page)
- how you know what the dimensions are
- the bit order

It could be something as simple as

  HEIGHT = 40
  some_bytes = file('data.bin').read()
  WIDTH = len(some_bytes) // HEIGHT
  for i, byte in enumerate(some_bytes):
if i and i % WIDTH == 0:
  print # a new line
for bit in range(8):
  if byte & (1 << bit):
print '*',
  else:
print ' ',


-tkc

> DISCLAIMER:
[trim a paragraph of useless junk]
Please remove these disclaimers if at all possible.  You're posting to a public 
forum, which pretty much waives all credibility to the disclaimer (not that 
they've held much legal standing in any argument I've heard).






DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.

-- 
http://mail.python.org/mailman/listinfo/python-list


"module could not be found" error

2013-03-19 Thread Robert Flintham
Hi,

I'm trying to run the following, with  representing an array of 
floating point numbers:


import numpy as np
import scipy as sp
from scipy import optimize

xdata=
ydata=

def t2fit(x,T2,A):
return A * np.exp(-x/T2)

popt, pcov = optimize.curve_fit(t2fit, xdata, ydata, p0=None, sigma=None)


But I'm getting an ImportError:

Traceback (most recent call last):
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", 
line 1397, in 
debugger.run(setup['file'], None, None)
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", 
line 1090, in run
pydev_imports.execfile(file, globals, locals) #execute the script
 File 
"K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\_pydev_execfile.py",
 line 38, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
  File "K:\PROGRAMMING\MRI_Python\fatfrac.py", line 7, in 
from scipy import optimize
  File "K:\Python33\lib\site-packages\scipy\optimize\__init__.py", line 147, in 

from ._minimize import *
  File "K:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 29, in 

from .lbfgsb import _minimize_lbfgsb
  File "K:\Python33\lib\site-packages\scipy\optimize\lbfgsb.py", line 40, in 

from . import _lbfgsb
ImportError: DLL load failed: The specified module could not be found.


I've checked the path, and the file lbfgsb.py is definitely at that location 
(as are optimize.py and _minimize.py).  Does anyone know why I'm getting the 
error?

All the best,
Rob



Robert Flintham
Trainee Clinical Scientist - MRI
Tel:

 +44 (0)121 371 7000

Email:

 robert.flint...@uhb.nhs.uk

Web:

 http://www.uhb.nhs.uk


We're bringing the world's most advanced cancer treatments to Birmingham.
Find out more at www.qecancerappeal.org<http://www.qecancerappeal.org> or text 
QEHB01 £5 to 70070 to donate £5 to our appeal.

RRPPS
Medical Physics - University Hospitals Birmingham NHS Foundation Trust
63 Melchett Road, Kings Norton,
Birmingham, B30 3HP

[cid:image001.gif@01CE24B3.0686DA50]
DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.
<>-- 
http://mail.python.org/mailman/listinfo/python-list


RE: "module could not be found" error

2013-03-19 Thread Robert Flintham
Joel: The 2.7 is the version number of the PyDev plugin for Eclipse, rather 
than the Python version number.

Dave: I don't have access to the system's PATH environment variable because 
this computer is very tightly controlled.  However, my user PATH environment 
contains "K:\Python33", which I haven't had a problem with before when 
importing modules from site-packages.

The directory "site-packages\scipy\optimize" contains (among other things):
__init.py__
_minimize.py
_lbfgsb.pyd
lbfgsb.py

So I'm not entirely sure what it is that it can't find.

All the best,
Rob

(Apologies for lengthy disclaimers, I can't get rid of them.  Work email.)


-Original Message-
From: Python-list 
[mailto:python-list-bounces+robert.flintham=uhb.nhs...@python.org] On Behalf Of 
Dave Angel
Sent: 19 March 2013 15:24
To: python-list@python.org
Subject: Re: "module could not be found" error

On 03/19/2013 11:10 AM, Robert Flintham wrote:
> Hi,
>
> I'm trying to run the following, with  representing an array of 
> floating point numbers:
>
> 
> import numpy as np
> import scipy as sp
> from scipy import optimize
>
> xdata=
> ydata=
>
> def t2fit(x,T2,A):
>  return A * np.exp(-x/T2)
>
> popt, pcov = optimize.curve_fit(t2fit, xdata, ydata, p0=None, 
> sigma=None)
> 
>
> But I'm getting an ImportError:
>
> Traceback (most recent call last):
>File 
> "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 
> 1397, in 
>  debugger.run(setup['file'], None, None)
>File 
> "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 
> 1090, in run
>  pydev_imports.execfile(file, globals, locals) #execute the script
>   File 
> "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\_pydev_execfile.py",
>  line 38, in execfile
>  exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
>File "K:\PROGRAMMING\MRI_Python\fatfrac.py", line 7, in 
>  from scipy import optimize
>File "K:\Python33\lib\site-packages\scipy\optimize\__init__.py", line 147, 
> in 
>  from ._minimize import *
>File "K:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 29, 
> in 
>  from .lbfgsb import _minimize_lbfgsb
>File "K:\Python33\lib\site-packages\scipy\optimize\lbfgsb.py", line 40, in 
> 
>  from . import _lbfgsb
> ImportError: DLL load failed: The specified module could not be found.
>
>
> I've checked the path, and the file lbfgsb.py is definitely at that location 
> (as are optimize.py and _minimize.py).  Does anyone know why I'm getting the 
> error?
>

You're on Windows, so presumably you're using the PATH environment variable for 
the following search.

You checked the path for what file?  it's looking for  _lhfgsb.dll, or maybe 
_lhfgsb.pyc which is imported by lbfgsh.py.  The latter file is found, as you 
can tell from the stack trace.  That's the file you got the error in.

So perhaps the dll isn't being loaded into a directory that's on the PATH.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list

DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.

-- 
http://mail.python.org/mailman/listinfo/python-list


Drag and drop in Windows

2013-04-29 Thread Robert Flintham
Hello all,

Sorry to post such a generic question, but after searching the interwebs I'm 
not really any wiser about how to start with this.

I'm currently on:
Windows XP
Python 2.7

I'm trying to create a small window in Python 2.7, that when you drop a file 
onto it from Windows explorer returns the file's path so that I can then go on 
to open the file and do whatever with it.  I was planning on using Tkinter 
because that's what I've used before for GUI's, but I can be swayed from this 
if needs be.

I've found this (TkDND):
http://wiki.tcl.tk/2768

But I don't know how to implement this in Python.  The Windows binary for it 
comes as a set of ".tcl" files and a single ".dll" file.

The two options I've stumbled across seem to be

1.  a Python wrapper for the DLL (I think to wrap C code??), which can then be 
imported like you'd import a Python package

2.  direct implementation of the Tcl file [tk.eval('source ...')], but I don't 
reallu understand what's going on with this - can you only execute a "main" bit 
of Tcl files rather than implementing individual functions?

Any input (however minimal) is definitely appreciated!  I've included what I 
think are probably the relevant functions from the Tcl files at the bottom of 
the email, but I don't really understand the nuts and bolts of the code.

All the best,
Rob

[From "tkdnd.tcl"...]

# 
#  Command tkdnd::drag_source
# 
proc tkdnd::drag_source { mode path { types {} } { event 1 } } {
  set tags [bindtags $path]
  set idx  [lsearch $tags "TkDND_Drag*"]
  switch -- $mode {
register {
  if { $idx != -1 } {
bindtags $path [lreplace $tags $idx $idx TkDND_Drag$event]
  } else {
bindtags $path [concat $tags TkDND_Drag$event]
  }
  set types [platform_specific_types $types]
  set old_types [bind $path <>]
  foreach type $types {
if {[lsearch $old_types $type] < 0} {lappend old_types $type}
  }
  bind $path <> $old_types
}
unregister {
  if { $idx != -1 } {
bindtags $path [lreplace $tags $idx $idx]
  }
}
  }
};# tkdnd::drag_source


[From "tkdnd_windows.tcl"...]

# 
#  Command olednd::_GetDragSource
# 
proc olednd::_GetDragSource {  } {
  variable _drag_source
  return $_drag_source
};# olednd::_GetDragSource
DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Drag and drop in Windows

2013-04-30 Thread Robert Flintham
Thanks Christian.

I've tried the following code:

import Tkinter

root = Tkinter.Tk()
root.title("DICOM Opener")
root.tk.eval('lappend auto_path {K:/Python27/Lib/site-packages/tkdnd2.6}')
root.tk.eval('package require tkdnd')


But I get the following traceback:

Traceback (most recent call last):
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", 
line 1397, in 
debugger.run(setup['file'], None, None)
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", 
line 1090, in run
pydev_imports.execfile(file, globals, locals) #execute the script
  File "K:\PROGRAMMING\mripy\dicom_opener.py", line 7, in 
root.tk.eval('package require tkdnd')
_tkinter.TclError: couldn't load library 
"K:/Python27/Lib/tkdnd2.6/tkdnd26.dll": invalid argument


Is "invalid argument" as generic as it sounds, or does it mean something 
specific in this case?  Is Tcl expecting an additional argument in the 'package 
require' line?

I assume the append auto path line is correct as it seems to have found the DLL 
(presumably from pkgIndex.tcl).

Apologies for any disclaimers that pad out this message - it's a work email 
account and they get added after I hit "send".

All the best,
Rob

-Original Message-
From: Python-list 
[mailto:python-list-bounces+robert.flintham=uhb.nhs...@python.org] On Behalf Of 
Christian Gollwitzer
Sent: 29 April 2013 21:38
To: python-list@python.org
Subject: Re: Drag and drop in Windows

Hi Robert,

Am 29.04.13 12:25, schrieb Robert Flintham:
> I've found this (TkDND):
>
> http://wiki.tcl.tk/2768
 > But I don't know how to implement this in Python.  The Windows binary  > for 
 > it comes as a set of ".tcl" files and a single ".dll" file.
 > 2.direct implementation of the Tcl file [tk.eval('source ...')], but I  > 
 > don't reallu understand what's going on with this - can you only execute  > 
 > a "main" bit of Tcl files rather than implementing individual functions?

I can only comment on the Tcl side, since I'm not an expert in the Tkinter 
coupling mechanism. TkDND is indeed the way to go if you want native 
drag'n'drop support. The first step would indeed be to load the package into 
the Tcl interpreter. You need to:

1) Create a folder for the packages, put the files in a subfolder Typically, 
this is something like lib/tkdnd, and at that level there must be the 
"pkgIndex.tcl" file
2) Append the lib/ folder to the auto path tk.eval('lappend auto_path 
{mypath/lib}') (the braces are Tcl's quoting mechanism)
3) load the package
tk.eval('package require tkdnd')

Then, you need to "register the target", i.e. declare a widget that it accepts 
files. Here, you need the Tk path name of the widget, which is retrieved by 
__str__:

tk.eval('tkdnd::drop_target register ' + yourwidget +' *')

Then, if you drop something, the widget recieves a virtual event 
<> . Now this is tricky, I don't know how to bind to that 
event. Following the tutorial for Tcl on http://wiki.tcl.tk/36708, I suppose 
something like

yourwidget.bind("<>", filesdropped)

should in principle work, but how to get the data out of it? It is stuffed into 
the %D bind substitution. Usual events store the MouseWheel distance in this 
field; so maybe you can get it from the field event.delta. I can't test it now, 
but I am a bit skeptical whether this works with the guts of TkInter. If not, 
you'd need to do some more forwarding from the Tcl side.

Christian
--
http://mail.python.org/mailman/listinfo/python-list

DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Drag and drop in Windows

2013-05-01 Thread Robert Flintham
Thanks Kevin, that looks great.  It's having trouble finding TkDND though - is 
there a certain place in the "Python27" directory that it's most likely to 
look?  It's currently under Lib/site-packages, but I'm suspicious that 
Tk/Tkinter has its own library somewhere.

Christian - you were right.  The TkDND DLL looks to be for x64.  Is there a way 
around this?

All the best,
Rob



-Original Message-
From: Python-list 
[mailto:python-list-bounces+robert.flintham=uhb.nhs...@python.org] On Behalf Of 
Kevin Walzer
Sent: 01 May 2013 00:10
To: python-list@python.org
Subject: Re: Drag and drop in Windows

Official link:

http://tkinterdnd.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list

DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.

-- 
http://mail.python.org/mailman/listinfo/python-list