Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; 'bits': 0.07; 'pixel': 0.07; 'tkinter': 0.07; 'python': 0.09; 'matplotlib': 0.09; 'pil.': 0.09; 'pixels': 0.09; 'tuple': 0.09; 'unpack': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'gui': 0.11; 'url:)': 0.13; 'library': 0.15; 'represents': 0.15; 'slightly': 0.15; '0).': 0.16; 'fully,': 0.16; 'ideally,': 0.16; 'numpy': 0.16; 'presume': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'bytes': 0.17; 'integer': 0.17; 'windows': 0.19; 'module': 0.19; 'translate': 0.20; 'import': 0.21; 'sorry,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'specified': 0.23; "haven't": 0.23; "i've": 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'question': 0.27; 'forgot': 0.27; 'thoughts': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'this?': 0.28; 'img': 0.29; 'installed,': 0.29; 'array': 0.29; 'url:code': 0.29; "i'm": 0.29; 'related': 0.30; "skip:' 20": 0.32; 'print': 0.32; 'anyone': 0.33; 'changed': 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'screen': 0.34; 'data,': 0.35; 'robert': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; '(i.e.': 0.36; 'data.': 0.36; "wasn't": 0.36; 'available.': 0.37; 'does': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'object': 0.38; 'sure': 0.38; 'help': 0.40; 'containing': 0.61; 'url:p': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'decided': 0.65; 'subject': 0.66; '2013': 0.84; 'ask,': 0.84; 'bitmap': 0.84; 'oscar': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=352fWF8szKACswNK68POznXdtuagJ4pWk0Ab12uaNo4=; b=ZCeiYJ05I5FD3H5CZIXUekyGbmXK8VmoQYOHGg1zny1jBV3kbDEDej2RrhI69XPPtv DJzIf8asqFGrBHFd9jlg+E2n9jpCL98eIINO0qNQ9ua9TjF9YdByzuBTuqecgtnTnetv gBUAhzKnHlDxWt2TwHINoB5qbP2VlYKfMXMIeVc4rbwnxf3nRH+CTaGE4R0zPZq9WW3D c3lpjqafwE63NN/mQYcojisJ8/WHrPIQ26znwHFg5hf7HNtERtnWtdqttf2z5BV2Y6my duV8ygD1CQj111G5+5qNpV/mXQAAUhKS4ndJPQ2Y/YnjhjGYYPpiG/W1bLACoWY7mh6a D9+w== X-Received: by 10.220.140.18 with SMTP id g18mr5034500vcu.54.1363100369814; Tue, 12 Mar 2013 07:59:29 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20130312132840.B5A8D44863A@nhs-pd1e-esg102.ad1.nhs.net> References: <20130311153254.078484489A7@nhs-pd1e-esg101.ad1.nhs.net> <20130312124931.C08D44482E5@nhs-pd1e-esg001.ad1.nhs.net> <20130312132840.B5A8D44863A@nhs-pd1e-esg102.ad1.nhs.net> From: Oscar Benjamin Date: Tue, 12 Mar 2013 14:59:09 +0000 Subject: Re: Reversing bits in a byte To: Robert Flintham Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363100379 news.xs4all.nl 6913 [2001:888:2000:d::a6]:49982 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41125 On 12 March 2013 13:28, Robert Flintham wrote: > 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.goog= le.com/p/pydicom/). Using pydicom, I've ended up with a "bytes" object of = length (512*512/8 =3D 32768) containing a 512x512 1-bit bitmap (i.e. each b= yte represents 8 pixels of either 1 or 0). When I print this to screen I g= et: > 'b\x00\x00\x00.....' > > I can unpack this to a tuple of the integer representations of binary dat= a, but that doesn't really help as presume I need the binary (8 digit) repr= esentation 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 install= ed, and PIL. > > Ideally, I'd like to be able to access the pixel data in the form of a nu= mpy 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 a= ny thoughts on how to do this? Numpy and matplotlib will do what you want: import numpy as np import matplotlib.pyplot as plt def bits_to_ndarray(bits, shape): abytes =3D np.frombuffer(bits, dtype=3Dnp.uint8) abits =3D np.zeros(8 * len(abytes), np.uint8) for n in range(8): abits[n::8] =3D (abytes % (2 ** (n+1))) !=3D 0 return abits.reshape(shape) # 8x8 image =3D 64 bits bytes object bits =3D b'\x00\xff' * 4 img =3D bits_to_ndarray(bits, shape=3D(8, 8)) plt.imshow(img) plt.show() Oscar