32位转换8位图像

from PIL import Image
import numpy as np
import math
import os

path = 'C:\\Users\\Administrator\\Desktop\\old_label\\'
newpath = 'C:\\Users\\Administrator\\Desktop\\mask\\'


def toeight():
    filelist = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
    for file in filelist:
        whole_path = os.path.join(path, file)
        img = Image.open(whole_path)  # 打开图片img = Image.open(dir)#打开图片
        img = np.array(img)
        # img = Image.fromarray(np.uint8(img / float(math.pow(2, 16) - 1) * 255))
        img = Image.fromarray(np.uint8(img))
        img.save(newpath + file)


toeight()

有点问题 32位图像转换出来是24位的,但是代码能用

 

import os
from scipy import ndimage
import pickle as plt
import cv2 as cv
from PIL import Image
import numpy as np
import math
import os

ii = cv.imread("0.bmp")
gray =  cv.cvtColor(ii, cv.COLOR_BGR2GRAY)
print(gray)
cv.imshow("gray", gray)
cv.imwrite('1.jpg',gray)

这一段可以转换~~~

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
将8位灰度图像转换为32位灰度图像需要进行以下步骤: 1. 创建一个新的32位灰度图像对象,大小与原始8位灰度图像相同。 2. 遍历原始8位灰度图像的每个像素,读取该像素的灰度值。 3. 将该灰度值赋值给新的32位灰度图像对象的相应像素,同时将该像素的其他三个通道(红色、绿色和蓝色)设置为0。 4. 重复步骤2和3,直到遍历完整个8位灰度图像。 5. 最终,新的32位灰度图像对象将包含与原始8位灰度图像相同的灰度信息,但是每个像素的其他三个通道均为0。 以下是使用Python语言实现该过程的代码示例: ```python import cv2 # 读取8位灰度图像 gray_8bit = cv2.imread('gray_8bit.png', cv2.IMREAD_GRAYSCALE) # 创建新的32位灰度图像对象,大小与原始8位灰度图像相同 gray_32bit = cv2.cvtColor(gray_8bit, cv2.COLOR_GRAY2BGRA) # 将每个像素的灰度值赋值给新的32位灰度图像对象的相应像素 for i in range(gray_8bit.shape[0]): for j in range(gray_8bit.shape[1]): gray_32bit[i,j] = [gray_8bit[i,j], gray_8bit[i,j], gray_8bit[i,j], 0] # 保存新的32位灰度图像 cv2.imwrite('gray_32bit.png', gray_32bit) ``` 在上述代码中,我们首先使用OpenCV库中的cv2.imread()函数读取8位灰度图像。然后,我们使用cv2.cvtColor()函数将其转换为新的32位灰度图像对象。接下来,我们使用两个嵌套的for循环遍历原始8位灰度图像的每个像素,并将该像素的灰度值赋值给新的32位灰度图像对象的相应像素。最后,我们使用cv2.imwrite()函数将新的32位灰度图像保存到磁盘上。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值