Skip to content

Commit 1a434a7

Browse files
committed
Merge pull request #444 from arokem/dft-fromstring
MRG: Use newer call to frombytes, replacing fromstring Use `frombytes` method available in newer PIL == Pillow, falling back to older `fromstring` if necessary. This closes #443
2 parents 83d3249 + d8ff4e6 commit 1a434a7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ addons:
2020
- libatlas-base-dev
2121
env:
2222
global:
23-
- DEPENDS="numpy scipy matplotlib h5py"
23+
- DEPENDS="numpy scipy matplotlib h5py pillow"
2424
- PYDICOM=1
2525
- INSTALL_TYPE="setup"
2626
python:
@@ -44,7 +44,7 @@ matrix:
4444
# Minimum pydicom dependency
4545
- python: 2.7
4646
env:
47-
- DEPENDS="numpy==1.5.1 pydicom==0.9.7"
47+
- DEPENDS="numpy==1.5.1 pydicom==0.9.7 pillow==2.6"
4848
# test against numpy 1.7
4949
- python: 2.7
5050
env:

nibabel/dft.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def __getattribute__(self, name):
124124

125125
def as_png(self, index=None, scale_to_slice=True):
126126
import PIL.Image
127+
# For compatibility with older versions of PIL that did not
128+
# have `frombytes`:
129+
if hasattr(PIL.Image, 'frombytes'):
130+
frombytes = PIL.Image.frombytes
131+
else:
132+
frombytes = PIL.Image.fromstring
133+
127134
if index is None:
128135
index = len(self.storage_instances) // 2
129136
d = self.storage_instances[index].dicom()
@@ -138,8 +145,9 @@ def as_png(self, index=None, scale_to_slice=True):
138145
max = data.max()
139146
data = data * 255 / (max - min)
140147
data = data.astype(numpy.uint8)
141-
im = PIL.Image.fromstring('L', (self.rows, self.columns),
142-
data.tostring())
148+
im = frombytes('L', (self.rows, self.columns),
149+
data.tostring())
150+
143151
s = BytesIO()
144152
im.save(s, 'PNG')
145153
return s.getvalue()

0 commit comments

Comments
 (0)