I simply want to darken an image that I've loaded using scikit-image in python.
我只是想把我用sorkit-Image加载的一张图片变暗。
If I do this:
如果我这么做了:
test_image = io.imread("TestProvs2.bmp")
test_image = test_image * 0.5
show_map(test_image)
The array of RGB values does indeed get scaled down, so that each RGB value is 127.5.
RGB值的数组确实会缩小,因此每个RGB值都是127.5。
Yet the resulting image has maximum values (left = original, right = new):
然而,生成的图像具有最大值(左=原始,右=新):
更多回答
What's the data type of the array, and how are you displaying the image?
该数组的数据类型是什么,以及如何显示图像?
what is show_map
?
什么是show_map?
please amend your question to explain how you display your data. someone removed the matplotlib tag even though you use matplotlib and that's the source of your problem.
请修改你的问题,解释你如何显示你的数据。有人删除了matplotlib标记,尽管您使用的是matplotlib,这就是问题的根源。
When you read an image, the data type of your image is uint8
. When you multiply it by 0.5
, Python casts it to float64
but an image must be uint8
, so it gives this error
当您读取图像时,图像的数据类型为uint8。当您将其乘以0.5时,Python会将其强制转换为Float64,但图像必须为uint8,因此会出现此错误
Lossy conversion from float64 to uint8. Range [0.0, 127.5]. Convert image to uint8 prior to saving to suppress this warning.
What you have to do is to cast it to uint8
manually.
您需要做的是手动将其转换为uint8。
test_image = (test_image * 0.5).astype(np.uint8)
Don't forget to import numpy
别忘了导入麻木
import numpy as np
Generally, it's better to use OpenCV for image processing.
一般来说,最好使用OpenCV进行图像处理。
The issue is indeed data types, and how matplotlib handles them.
问题确实在于数据类型,以及matplotlib如何处理它们。
When you have data of type uint8
, the value range is expected to be from 0 to 255. An image needs not be uint8. It can have other data types too.
当您有uint8类型的数据时,该值的范围应该是从0到255。不需要使用图像8。它还可以有其他数据类型。
When you have data of type float/float64, the value range is probably expected to be 0.0 .. 1.0. Matplotlib expects this.
当您有类型为FLOAT/FLOAT64的数据时,该值的范围可能是0.0.1.0.Matplotlib预料到了这一点。
If you see a warning like this, that would be an indication of the issue:
如果您看到这样的警告,则表示该问题:
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
使用RGB数据将输入数据剪裁到imshow的有效范围(对于浮点数为[0..1],对于整数为[0..255])。
Since you merely multiplied your values by 0.5, your value range is 0.0 .. 127.5
由于您只是将值乘以0.5,因此您的值范围是0.0.127.5
Taking that, while expecting a range of upto 1.0, blows out all the pixels.
当期望范围达到1.0的时候,所有的像素都被打乱了。
You fix that either by scaling appropriately, or converting to uint8
explicitly.
您可以通过适当缩放或显式转换为uint8来修复该问题。
matplotlib is perfectly fine to use, for displaying purposes. You just have to check the documentation for what any API will do with your data.
出于显示目的,matplotlib非常适合使用。您只需查看文档,了解任何API将如何处理您的数据。
更多回答
That did it, tysm!
就是这样,泰斯姆!
OpenCV is not all that great for image processing.
OpenCV对于图像处理来说并不是那么好。
While matplotlib supports non uint8
dtypes, skimage gives a warning and doesn't save the image correctly,. Moreover, OpenCV throws an error.
虽然matplotlib支持非uint8数据类型,但skImage会给出警告,并且不能正确保存图像。此外,OpenCV还会抛出一个错误。
the question does not involve trying to save the image. OP hasn't clarified the nature of show_map
at all. I had to deduce the use of matplotlib from the screenshots. -- opencv is also not used in OP's question. it can't throw an error. if you see an error, please point it out, including your minimal reproducible example
这个问题并不涉及试图保存图像。OP根本没有澄清show_map的性质。我不得不从截图中推断出matplotlib的用法。--OP的问题中也没有使用OpenCV。它不能抛出错误。如果您看到错误,请指出它,包括您最小的可重现的例子
我是一名优秀的程序员,十分优秀!