Conversion between Pillow Image object and numpy a

2019-02-01 20:12发布

I am using Pillow and numpy, but have a problem with conversion between Pillow Image object and numpy array.

when I execute following code, the result is weird.

im = Image.open(os.path.join(self.img_path, ifname))
print im.size
in_data = np.asarray(im, dtype=np.uint8)
print in_data.shape

result is

(1024, 768)
(768, 1024)

Why dimension is changed?

3条回答
趁早两清
2楼-- · 2019-02-01 20:49

actually this is because most image libraries give you images that are transpozed compared to numpy arrays. this is (i think) because you write image files line by line, so the first index (let's say x) refers to the line number (so x is the vertical axis) and the second index (y) refers to the subsequent pixel in line (so y is the horizontal axis), which is against our everyday coordinates sense.

If you want to handle it correctly you need to remember to write:

image = library.LoadImage(path)
array = (library.FromImageToNumpyArray(image)).T

and consequently:

image = library.FromNumpyArrayToImage(array.T)
library.WriteImage(image, path)

Which works also for 3D images. But i'm not promising this is the case for ALL image libraries - just these i worked with.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-01 20:57

If your image is greyscale do:

in_data = in_data.T

but if you are working with rbg images you want to make sure your transpose operation is along only two axis:

in_data = np.transpose(in_data, (1,0,2))
查看更多
放荡不羁爱自由
4楼-- · 2019-02-01 21:00

im maybe column-major while arrays in numpy are row-major

do in_data = in_data.T to transpose the python array

probably should check in_data with matplotlib's imshow to make sure the picture looks right.

But do you know that matplotlib comes with its own loading functions that gives you numpy arrays directly? See: http://matplotlib.org/users/image_tutorial.html

查看更多
登录 后发表回答