[英]How to convert 0-1 image float array to 0-255 int array
import matplotlib
import numpy as np
photo=plt.imread('Feynman.png')
plt.figure
plt.subplot(121)
plt.imshow(photo)
photo*=255
plt.subplot(122)
plt.imshow(photo)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
and only displays an image in the first subplot并且只在第一个子图中显示图像photo*=255
is still an array of floats.问题是photo*=255
仍然是一组浮点数。
photo = photo.astype(int)
after photo*=255
.在photo*=255
之后添加photo = photo.astype(int)
。X
in .imshow
should be int
type when the array is 0-255: (M, N, 3): an image with RGB values (0-1 float or 0-255 int)当数组为0-255时, .imshow
中的X
应该是int
类型: (M, N, 3): RGB值的图像(0-1 float or 0-255 int)photo = plt.imread('Feynman.png')
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 5))
print(photo[0][0])
ax1.imshow(photo)
photo*=255
print(photo[0][0])
photo = photo.astype(int)
print(photo[0][0])
ax2.imshow(photo)
[output]:
[0.16470589 0.16470589 0.16470589]
[42. 42. 42.]
[42 42 42]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.