linux 安装 python3.6 pip3 setuptools并用python解析dcm影像文件

一、安装 python3.6

1.安装依赖环境

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

2.下载 Python3

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

3.安装 python3
(1)创建文件夹

mkdir -p /home/mpx/python3

我是解压下载好的 Python-3.x.x.tgz 包(具体包名因你下载的 Python 具体版本不同⽽不同,如:我下载的是Python3.6.1那我这里就是 Python-3.6.1.tgz)
在这里插入图片描述
在这里插入图片描述
(2)解压

tar -zxvf Python-3.6.1.tgz

4.进入解压后的目录,编译安装

cd Python-3.6.1

./configure --prefix=/home/mpx/python3

如果遇上如下报错
在这里插入图片描述
由于没有gcc导致的,安装gcc即可解决:

yum install gcc

没有报错或者安装完gcc 向下继续

make
make install
或者
make && make install

5.建立 python3 的软链

ln -s /home/mpx/python3/bin/python3 /usr/bin/python3

6.将/usr/local/python3/bin加入 PATH

vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/home/mpx/python3/bin
export PATH

使配置文件生效

source ~/.bash_profile

校验是否安装成功 分别执行一下

python3 -V

pip3 -V

7.不行的话在创建一下 pip3 的软链接(我也不清楚这一步有什么用)

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

二、安装 pip 前需要前置安装setuptools 到官网下载

1.进行解压

unzip setuptools-41.6.0.zip

2.进入目录

cd setuptools-41.6.0/

编译

python3 setup.py build

安装

python3 setup.py install

安装pip

easy_install pip

安装成功
安装python第三方包

安装 SimpleITK

pip3 install SimpleITK -i https://pypi.tuna.tsinghua.edu.cn/simple

如果失败可以修改成阿里源

pip3 install SimpleITK -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

--------------------------------------------------------------------------------------------------------------------

安装 numpy

pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

如果失败可以修改成阿里源

pip3 install numpy -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

-----------------------------------------------------------------------------------------------------------------

安装 opencv-python

pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

如果失败可以修改成阿里源

pip3 install opencv-python -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

一般到这一步就能安装成功

但是不同的服务器可能基础环境不全 opencv-python一下装不成功 可能遇到如下问题

1.缺少 ModuleNotFoundError: No module named 'skbuild’
在这里插入图片描述
执行

pip3 install scikit-build -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

在这里插入图片描述
安装成功scikit-build

继续安装opencv-python 又出现一个错误

2.Problem with the CMake installation, aborting build. CMake executable is cmake

在这里插入图片描述
接下来安装最新cmake

下载

 wget https://cmake.org/files/v3.18/cmake-3.18.0-rc1.tar.gz

下载成功
在这里插入图片描述

解压下载好的安装包

tar -zxvf cmake-3.18.0-rc1.tar.gz

切换到目标目录下

cd cmake-3.18.0-rc1

运行当前目录下的一个文件 (默认安装路径为/usr/local)

./bootstrap

这一步如果提示失败没有找到openssl 那就安装下openssl

yum install openssl openssl-devel

运行命令(这步时间有点长)

gmake

进行安装

gmake install

做个软链接

ln -s /usr/local/bin/cmake /usr/bin/cmake

安装成功后查看版本号

cmake --version

在这里插入图片描述
最后再装一遍opencv-python(安装时间较长 快半小时 流汗~ )
在这里插入图片描述
终于成功了!
--------------------------------------------------------------------------------------------------------------------------

安装 pydicom

pip3 install pydicom -i https://pypi.tuna.tsinghua.edu.cn/simple

如果失败可以修改成阿里源

pip3 install pydicom -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

遇上下图这种错误
在这里插入图片描述
执行

sudo yum install -y python-qt4

usr下创建python文件夹并上传一个dcm文件

mkdir /usr/python

python将dcm源文件 转图片代码

import SimpleITK as sitk
import numpy as np
import cv2
import sys

def coverJPG(inputfile, outputfile):
    ds_array = sitk.ReadImage(inputfile)  # 读取dicom文件的相关信息
    img_array = sitk.GetArrayFromImage(ds_array)  # 获取array
    # SimpleITK读取的图像数据的坐标顺序为zyx,即从多少张切片到单张切片的宽和高,此处我们读取单张,因此img_array的shape
    # 类似于 (1,height,width)的形式
    shape = img_array.shape
    img_array = np.reshape(img_array, (shape[1], shape[2]))  # 获取array中的height和width
    low = np.min(img_array)
    high = np.max(img_array)

    lungwin = np.array([low * 1., high * 1.])
    newimg = (img_array - lungwin[0]) / (lungwin[1] - lungwin[0])  # 归一化
    newimg = (newimg * 255).astype('uint8')  # 将像素值扩展到[0,255]
    cv2.imwrite(outputfile, newimg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    print('FINISHED')

if __name__ == "__main__":
    coverJPG(sys.argv[1], sys.argv[2])

测试转换图片是否成功

python3 /usr/python/dicomJPG.py /usr/python/aa.DCM /usr/python/bb.jpg

如下图成功
在这里插入图片描述
在这里插入图片描述
python解析dcm源文件代码

import pydicom
import sys

def loadFileInformation(filename):

    information = {}

    ds = pydicom.read_file(filename)
    if(hasattr(ds, 'Modality')):
        information['Modality'] = ds.Modality
    if(hasattr(ds, 'PatientBirthDate')):
        information['PatientBirthDate'] = ds.PatientBirthDate
    if(hasattr(ds, 'PatientSex')):
        information['PatientSex'] = ds.PatientSex
    if(hasattr(ds, 'SOPInstanceUID')):
        information['SOPInstanceUID'] = ds.SOPInstanceUID
    if(hasattr(ds, 'ImageType')):
        information['ImageType'] = ds.ImageType
    if(hasattr(ds, 'SamplesPerPixel')):
        information['SamplesPerPixel'] = ds.SamplesPerPixel
    if(hasattr(ds, 'PhotometricInterpretation')):
        information['PhotometricInterpretation'] = ds.PhotometricInterpretation
    if(hasattr(ds, 'Rows')):
        information['Rows'] = ds.Rows
    if(hasattr(ds, 'Columns')):
        information['Columns'] = ds.Columns
    if(hasattr(ds, 'PixelSpacing')):
        information['PixelSpacing'] = ds.PixelSpacing
    if(hasattr(ds, 'BitsAllocated')):
        information['BitsAllocated'] = ds.BitsAllocated
    if(hasattr(ds, 'HighBit')):
        information['HighBit'] = ds.HighBit
    if(hasattr(ds, 'PixelRepresentation')):
        information['PixelRepresentation'] = ds.PixelRepresentation
    if(hasattr(ds, 'WindowCenter')):
        information['WindowCenter'] = ds.WindowCenter
    if(hasattr(ds, 'WindowWidth')):
        information['WindowWidth'] = ds.WindowWidth
    if(hasattr(ds, 'RescaleIntercept')):
        information['RescaleIntercept'] = ds.RescaleIntercept
    if(hasattr(ds, 'RescaleSlope')):
        information['RescaleSlope'] = ds.RescaleSlope
    if(hasattr(ds, 'RescaleType')):
        information['RescaleType'] = ds.RescaleType
    if(hasattr(ds, 'ImagePositionPatient')):
        information['ImagePositionPatient'] = ds.ImagePositionPatient
    if(hasattr(ds, 'Manufacturer')):
        information['Manufacturer'] = ds.Manufacturer
    if(hasattr(ds, 'PatientName')):
        information["PatientName"] = ds.PatientName
    if(hasattr(ds, 'PatientID')):
        information["PatientID"] = ds.PatientID
    if(hasattr(ds, 'PatientAge')):
        information["PatientAge"] = ds.PatientAge
    if(hasattr(ds, 'BodyPartExamined')):
        information["BodyPartExamined"] = ds.BodyPartExamined
    if(hasattr(ds, 'SeriesInstanceUID')):
        information["SeriesInstanceUID"] = ds.SeriesInstanceUID
    if(hasattr(ds, 'SeriesNumber')):
        information["SeriesNumber"] = ds.SeriesNumber
    if(hasattr(ds, 'SeriesDate')):
        information["SeriesDate"] = ds.SeriesDate
    if(hasattr(ds, 'SeriesTime')):
        information["SeriesTime"] = ds.SeriesTime
    if(hasattr(ds, 'SeriesDescription')):
        information["SeriesDescription"] = ds.SeriesDescription
    if(hasattr(ds, 'ImageOrientationPatient')):
        information["ImageOrientationPatient"] = ds.ImageOrientationPatient
    if(hasattr(ds, 'InstanceNumber')):
        information["InstanceNumber"] = ds.InstanceNumber
    if(hasattr(ds, 'SpacingBetweenSlices')):
        information["SpacingBetweenSlices"] = ds.SpacingBetweenSlices
    if(hasattr(ds, 'SliceThickness')):
        information["SliceThickness"] = ds.SliceThickness
    if(hasattr(ds, 'SliceLocation')):
        information["SliceLocation"] = ds.SliceLocation
    if(hasattr(ds, 'StudyInstanceUID')):
        information["StudyInstanceUID"] = ds.StudyInstanceUID
    if(hasattr(ds, 'AccessionNumber')):
        information["AccessionNumber"] = ds.AccessionNumber
    if(hasattr(ds, 'StudyID')):
        information["StudyID"] = ds.StudyID
    if(hasattr(ds, 'StudyDate')):
        information["StudyDate"] = ds.StudyDate
    if(hasattr(ds, 'StudyTime')):
        information["StudyTime"] = ds.StudyTime
    if(hasattr(ds, 'PatientWeight')):
        information["PatientWeight"] = ds.PatientWeight
    if(hasattr(ds, 'InstitutionName')):
        information["InstitutionName"] = ds.InstitutionName
    if(hasattr(ds, 'ModalitiesInStudy')):
        information["ModalitiesInStudy"] = ds.ModalitiesInStudy
    if(hasattr(ds, 'StudyDescription')):
        information["StudyDescription"] = ds.StudyDescription
    if(hasattr(ds, 'ProtocolName')):
        information["ProtocolName"] = ds.ProtocolName
    # print (ds.dir())
    #
    # print (ds)
    print (information)
    return information

# a=loadFileInformation(path)
# #
# # print (a)


if __name__ == "__main__":
     loadFileInformation(sys.argv[1]);

如下图成功:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值