I am using the following code segment to input image from file. The code itself works fine. However, I have a question regarding line 7


I understand that it aims to scale the img value to 0 and 1. But I am not sure whether I should add some function/operator to enforce img/255 to be of integer value, or the result will be integer value by default due to the operator "/"

  1. import numpy as np
  2. from PIL import Image
  3. from skimage import io
  4. io.use_plugin('pil')
  5. from skimage import img_as_uint
  6. img = io.imread(os.path.join(train_data_path, image_mask_name))
  7. img = img/255

Hi Gribouillis, thanks for the reply. If the array size is pretty big, e.g., (200, 200), it will be pretty hard to examine each pixel value of it. Are there any good ways to check the value? Moreover, if some pixel values are of real value, while others are of integer, what will print(img.dtype) output.

Moreover, I scanned the result, looks like it img/255 returns 0 and 1 values. But the python document does not indicate that / operator will ensure to generate the integer value. This is why I am not confident about this kind of usage.

Ok, I tried it on my computer. The answer is that with python 3 or python 2 with from __future__ import division, the result is an array of float, but you can use // instead of / to get an array of ints. In python 2 without the from __future__ import division, the result is an array of ints.

# -*-coding: utf8-*-
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
import numpy as np
import sys

if __name__ == '__main__':
    print('python version', sys.version_info[:3])
    a = np.array(range(10))
    print('a', type(a), a.dtype, a)
    b = a / 256
    print('b', type(b), b.dtype, b)
    c = a // 256
    print('c', type(c), c.dtype, c)

""" my output -->
python version (2, 7, 6)
a <type 'numpy.ndarray'> int64 [0 1 2 3 4 5 6 7 8 9]
b <type 'numpy.ndarray'> float64 [ 0.          0.00390625  0.0078125   0.01171875  0.015625    0.01953125
  0.0234375   0.02734375  0.03125     0.03515625]
c <type 'numpy.ndarray'> int64 [0 0 0 0 0 0 0 0 0 0]