ImageTk.PhotoImage can't display image properly
You're having overflow problems when you're casting things back to uint8
s.
You're converting with (img_plot1*255.999).round().astype(np.uint8)
, but this will overflow for values near or at 1. (anything greater than 0.998)
Assuming img_plot1
will always contain values between 0 and 1, I think you meant to just do either:
(img_plot1 * 255.99).astype(np.uint8)
or
(img_plot1 * 255).round().astype(np.uint8)
The round
call will round up or down, whereas a pure int cast is effectively a floor
call.
However, just guessing from the "bands" in the output image, your input data is overflowing and "wrapping" multiple times. Therefore, your input data probably has a greater range than 0-1.
Therefore, if you don't want to worry about the exact range of values in img_plot1
, you could just rescale it to 0-255 based on its range:
rescaled = 255 * (img_plot1 - img_plot1.min()) / img_plot1.ptp()foto = Image.fromarray(rescaled.astype(np.uint8), mode='L')
You can also use np.digitize
to rescale the array, but it winds up being less readable, i.m.o. Also have a look at np.clip
if you want to clip values above and below a threshold (e.g. 0 and 255).