Matplotlib import image as float32

Markos markos at c2o.pro.br
Tue Jul 2 11:06:06 EDT 2019


Em 01-07-2019 18:03, Chris Angelico escreveu:
> On Tue, Jul 2, 2019 at 6:59 AM Markos <markos at c2o.pro.br> wrote:
>> Hi,
>>
>> I observed that matplotlib reads an image file (PNG) as float32:
>>
>> Please, how to read this file as int8 to get RGB in range of 0-255?
>>
>> Thank you,
>>
>> Markos
>>
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> import matplotlib.image as mpimg
>>> imagem = mpimg.imread('lenna.png')
>>> print (imagem)
>> [[[0.8862745  0.5372549  0.49019608]
>>     [0.8862745  0.5372549  0.49019608]
>>     [0.8745098  0.5372549  0.52156866]
>>     ...
>>
>>> print (imagem.dtype)
>> float32
>>
> Seems like matplotlib is rescaling everything to be on the range 0 to 1.
>
> https://matplotlib.org/users/image_tutorial.html
>
> ChrisA
Hi Chris,

I found a workaround at:

www.programcreek.com/python/example/99442/matplotlib.image.imread

I don't know if is the most elegant  but it worked.

I replaced:

imagem = mpimg.imread('lenna.png')


by the command:


  imagem = (mpimg.imread('lenna.png') * 255).astype('uint8')

Thanks,

Markos




More information about the Python-list mailing list