Commit 6cbb22bb authored by Bodo Kaiser's avatar Bodo Kaiser Committed by Soumith Chintala
Browse files

added support for signed ints, removed support for unsigned

parent c1a835f0
...@@ -169,13 +169,35 @@ class Tester(unittest.TestCase): ...@@ -169,13 +169,35 @@ class Tester(unittest.TestCase):
l, = img.split() l, = img.split()
assert np.allclose(l, img_data[:, :, 0]) assert np.allclose(l, img_data[:, :, 0])
def test_ndarray16_to_pil_image(self): def test_ndarray_bad_types_to_pil_image(self):
trans = transforms.ToPILImage() trans = transforms.ToPILImage()
img_data = np.random.randint(0, 65535, [4, 4, 1], np.uint16) with self.assertRaises(AssertionError):
trans(np.ones([4, 4, 1], np.int64))
trans(np.ones([4, 4, 1], np.uint16))
trans(np.ones([4, 4, 1], np.uint32))
trans(np.ones([4, 4, 1], np.float64))
def test_ndarray_gray_float32_to_pil_image(self):
trans = transforms.ToPILImage()
img_data = torch.FloatTensor(4, 4, 1).random_().numpy()
img = trans(img_data)
assert img.mode == 'F'
assert np.allclose(img, img_data[:, :, 0])
def test_ndarray_gray_int16_to_pil_image(self):
trans = transforms.ToPILImage()
img_data = torch.ShortTensor(4, 4, 1).random_().numpy()
img = trans(img_data) img = trans(img_data)
assert img.mode == 'I;16' assert img.mode == 'I;16'
assert np.allclose(img, img_data[:, :, 0]) assert np.allclose(img, img_data[:, :, 0])
def test_ndarray_gray_int32_to_pil_image(self):
trans = transforms.ToPILImage()
img_data = torch.IntTensor(4, 4, 1).random_().numpy()
img = trans(img_data)
assert img.mode == 'I'
assert np.allclose(img, img_data[:, :, 0])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -73,8 +73,10 @@ class ToPILImage(object): ...@@ -73,8 +73,10 @@ class ToPILImage(object):
if npimg.dtype == np.uint8: if npimg.dtype == np.uint8:
mode = 'L' mode = 'L'
if npimg.dtype == np.uint16: if npimg.dtype == np.int16:
mode = 'I;16' mode = 'I;16'
if npimg.dtype == np.int32:
mode = 'I'
elif npimg.dtype == np.float32: elif npimg.dtype == np.float32:
mode = 'F' mode = 'F'
else: else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment