pythonimagenumpymatplotlibfloat32

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)

enter image description here

the image I use


Solution

  • 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]
    

    enter image description here