Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python图像转换_Python_Image_Python Imaging Library - Fatal编程技术网

Python图像转换

Python图像转换,python,image,python-imaging-library,Python,Image,Python Imaging Library,我正在尝试将具有模式I(32位有符号整数像素)的图像转换为标准灰度或“RGB”图像。问题是当我试图转换它时,它只是变成一个空白的白色图像。我正在使用PIL模块 这是我试图转换的图像 这对你有用吗 from PIL import Image import numpy as np sample_img = Image.open('sample.png') rescaled = 255 * np.asarray(sample_img)/2**16 img = Image.fromarray(np

我正在尝试将具有模式I(32位有符号整数像素)的图像转换为标准灰度或“RGB”图像。问题是当我试图转换它时,它只是变成一个空白的白色图像。我正在使用PIL模块

这是我试图转换的图像


这对你有用吗

from PIL import Image
import numpy as np

sample_img = Image.open('sample.png') 
rescaled = 255 * np.asarray(sample_img)/2**16
img = Image.fromarray(np.uint8(rescaled))
其中:

>>> np.asarray(img)

array([[ 95,  96,  98, ...,  98, 105, 107],
       [ 93,  97,  99, ..., 100, 105, 108],
       [ 94,  99, 100, ..., 102, 105, 110],
       ..., 
       [130, 125, 125, ...,  97,  98, 100],
       [128, 120, 123, ...,  99,  99, 101],
       [125, 119, 120, ..., 101, 100, 101]], dtype=uint8)

这是一个“标准”的8位灰度图像。

PIL是我曾经尝试使用的最为灵巧的软件包之一。这是一个非常直接的转换,您给出的示例代码应该可以正常工作

这里有一个解决办法

def ItoL(im):
    w, h = im.size
    result = Image.new('L', (w, h))
    pix1 = im.load()
    pix2 = result.load()
    for y in range(h):
        for x in range(w):
            pix2[x,y] = pix1[x,y] >> 8
return result

你能展示你的非工作代码吗?也许会有帮助。它处理了一个类似的问题,但使用了16位灰度PNG。@Yuriko这也是一个16位灰度PNG,这个问题可能应该作为一个副本来解决。正确答案隐藏在对另一个问题的注释中:将
函数与适当的表一起使用
sample\u img=sample\u img.point([i//256表示范围内的i(0x10000)],'L')
这两种解决方案都有效,但我发现另一种更适合我的代码。
def ItoL(im):
    w, h = im.size
    result = Image.new('L', (w, h))
    pix1 = im.load()
    pix2 = result.load()
    for y in range(h):
        for x in range(w):
            pix2[x,y] = pix1[x,y] >> 8
return result