Skip to content

PixelAccess class does not support Numpy int indexing #5048

@X-Bruce-Y

Description

@X-Bruce-Y

Hi developers, I happen to find out that PixelAcess class fails to use Numpy int as index, while Python built-in int, Python built-in float and even Numpy float all work fine. I'm using Python 3.6.1, Numpy 1.15.0, PIL 7.2.0.

An example is provided as follows.

code:

import numpy as np
from PIL import Image
import os

base = os.path.dirname(os.path.realpath(__file__))
im_path = os.path.join(base, 'xxxx.bmp')
pix = Image.open(im_path).load()

default_float_coor = [1.1, 9.9]
default_int_coor = [1,10]
numpy_float_coor = np.array([1.1, 9.9])
numpy_int_coor = np.array([1, 10])

print(type(pix))
print('pix at default_float_coor: ', pix[default_float_coor[0], default_float_coor[1]])
print('pix at default_int_coor: ', pix[default_int_coor[0], default_int_coor[1]])
print(type(numpy_float_coor[0]))
print('pix at numpy_float_coor: ', pix[numpy_float_coor[0], numpy_float_coor[1]])
print(type(numpy_int_coor[0]))
print('pix at numpy_int_coor: ', pix[numpy_int_coor[0],numpy_int_coor[1]])

Output:

<class 'PixelAccess'>
pix at default_float_coor:  (71, 71, 75)
pix at default_int_coor:  (70, 70, 70)
<class 'numpy.float64'>
pix at numpy_float_coor:  (71, 71, 75)
<class 'numpy.int32'>
Traceback (most recent call last):
  File "e:/HanLab_MyLab/Updated Codes/PreProcessing/draft.py", line 22, in <module>
    print('pix at numpy_int_coor: ', pix[numpy_int_coor[0],numpy_int_coor[1]])
TypeError: an integer is required

np.int64 fails too as I trialed.
It would add to much convenience if Numpy int could serve as index.
Thx.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions