2024-09-30 16:23:46 发布
网友
我试图理解这种方法是如何在灰度中转换图像的(如果它使用简单的平均值或加权平均值),我必须参考这个方法。在
从documentation我知道这个方法调用convert(‘F’)方法。在
convert(‘F’)
从Pillow/PILsource code,我可以找到这个方法,但是,当mode参数设置为'F'时,我找不到它的作用。在
mode
谢谢。在
模式参数“F”代表“浮点”。在
from PIL import Image im = Image.new("F", (2,2)) pixels = im.load() pixels[0,0] = 255.0 pixels[1,0] = 200.0 pixels[0,1] = 100.0 pixels[1,1] = 20.0 im.show()
在Image对象的convert方法的docstring中(可以在链接到的代码中看到),有以下内容:
Image
convert
When translating a color image to black and white (mode "L"), the library uses the ITU-R 601-2 luma transform::L = R * 299/1000 + G * 587/1000 + B * 114/1000
When translating a color image to black and white (mode "L"), the library uses the ITU-R 601-2 luma transform::
L = R * 299/1000 + G * 587/1000 + B * 114/1000
显然这也是mode='F'的处理方式。在
mode='F'
下面是一个例子:
In [2]: from PIL import Image
为演示创建数组:
使用convert方法中的mode='F'将数组通过转换发送到图像并返回到数组:
In [4]: np.array(Image.fromarray(x).convert('F')) Out[4]: array([[ 3.24499989, 6.30499983, 1.86899996, 4.54400015], [ 3.54399991, 5.04300022, 4.63000011, 0.29899999], [ 2.0539999 , 3.29900002, 1.85800004, 1.76100004], [ 3.9289999 , 4.76100016, 5.76100016, 2.47799993]], dtype=float32)
将x乘以convert的docstring中显示的因子:
x
In [5]: f = np.array([0.299, 0.587, 0.114]) In [6]: x.dot(f) Out[6]: array([[ 3.245, 6.305, 1.869, 4.544], [ 3.544, 5.043, 4.63 , 0.299], [ 2.054, 3.299, 1.858, 1.761], [ 3.929, 4.761, 5.761, 2.478]])
如果我们转换为np.float32,我们看到的值与convert方法创建的值完全相同:
np.float32
In [7]: x.dot(f).astype(np.float32) Out[7]: array([[ 3.24499989, 6.30499983, 1.86899996, 4.54400015], [ 3.54399991, 5.04300022, 4.63000011, 0.29899999], [ 2.0539999 , 3.29900002, 1.85800004, 1.76100004], [ 3.9289999 , 4.76100016, 5.76100016, 2.47799993]], dtype=float32)
模式参数“F”代表“浮点”。在
在
Image
对象的convert
方法的docstring中(可以在链接到的代码中看到),有以下内容:显然这也是
mode='F'
的处理方式。在下面是一个例子:
为演示创建数组:
^{pr2}$使用
convert
方法中的mode='F'
将数组通过转换发送到图像并返回到数组:将
x
乘以convert
的docstring中显示的因子:如果我们转换为
np.float32
,我们看到的值与convert
方法创建的值完全相同:相关问题 更多 >
编程相关推荐