gpt4 book ai didi

Python图像转换

转载 作者:行者123 更新时间:2023-12-01 04:20:18 25 4
gpt4 key购买 nike

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

这是我正在尝试转换的图像。

enter image description here

from PIL import Image
sample_img = Image.open('sample.png')
sample_img=sample_img.convert('L')

最佳答案

这对你有用吗?

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 位灰度图像。

关于Python图像转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com