rawmode = RAWMODE[im.mode] KeyError: ‘RGBA‘(python带我起飞)

在这里插入图片描述

解决方法:将"all.jpg"改为"all.png"

在这里插入图片描述
源码:(注意:文件需要自带)

import matplotlib.pyplot as plt  # plt 用于显示图片
import matplotlib.image as mpimg  # mpimg 用于读取图片
import pydicom
import pylab
import PIL.Image as Image
import math
import numpy as np
# #引入dicom模块
import os
def load_scan(path):
    slices = [pydicom.read_file(path + '/' + s) for s in os.listdir(path)]
    return slices

INPUT_FOLDER = r"C:\Users\Lenovo\Desktop\python\test\patient32\P32dicom"
first_patient = load_scan(INPUT_FOLDER)
# # print(first_patient[0])
# # print (first_patient [0].dir())
# # print (first_patient[0].dir("pat"))
# # print (first_patient [0].PatientName)
#
# pylab.imshow(first_patient[0].pixel_array,cmap=pylab.cm.bone)
# pylab.show()

def plot_ct_scan(scan):
    #获取文件夹内的文件个数
    length = len(scan)
    #根据总面积求每一个的大小
    each_size = int(math.sqrt(float(810*810)/length))
    #每一行可以放多少个
    lines = int(810/each_size)
    #生成白色背景新图片
    image = Image.new('RGBA', (810, 810),'white')
    x = 0
    y = 0
    for i in range(0,length):
      img=Image.fromarray(scan[i].pixel_array.astype(int))#numpy.ndarray类型,还得是整数

      img = img.resize((each_size, each_size), Image.ANTIALIAS) #resize image with high-quality
      image.paste(img, (x * each_size, y * each_size))
      x += 1
      if x == lines:
       x = 0
       y += 1
    image.save('./' + "all.png")


plot_ct_scan(first_patient)

lena = mpimg.imread('all.png')  # 读取和代码处于同一目录下的 lena.png
# 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
lena.shape  # (512, 512, 3)

plt.imshow(lena)  # 显示图片
plt.axis('on')  # 不显示坐标轴
plt.show()

本人工程:
在这里插入图片描述

如果想输出jpg图片可参考:

输出jpg图片:https://blog.csdn.net/vivian7fan/article/details/125613888

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

入世浮尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值