1. 安装 scikit-image#

如何安装 scikit-image 取决于你的需求和技能:

1.1. 支持的平台#

  • Windows 64位在x86处理器上

  • x86 和 M (ARM) 处理器上的 macOS

  • Linux 64位在x86处理器上

虽然我们不正式支持其他平台,但你仍然可以尝试 从源码构建

1.2. 版本检查#

要查看 scikit-image 是否已经安装,或者检查安装是否成功,可以在 Python shell 或 Jupyter notebook 中运行以下命令:

import skimage as ski
print(ski.__version__)

或者,从命令行:

python -c "import skimage; print(skimage.__version__)"

(如果 python 不成功,请尝试 python3。)

如果 scikit-image 已安装,您将看到版本号;否则,将显示错误信息。

1.3. 通过 pip 和 conda 安装#

这些安装仅包含 scikit-image 及其依赖项;pip 有一个选项可以包含相关包。

1.3.1. pip#

pip安装的前提条件:您能够使用系统的命令行来安装软件包,并且正在使用一个 虚拟环境 <https://towardsdatascience.com/virtual-environments-104c62d48c54?gi=2532aa12906#ee81>`_(任何 `几种中的一种)。

虽然可以在不使用虚拟环境的情况下使用 pip,但不建议这样做:虚拟环境创建了一个干净的 Python 环境,不会干扰任何现有的系统安装,可以轻松删除,并且只包含应用程序所需的包版本。它们有助于避免一个常见的问题,称为 依赖地狱

要安装当前的 scikit-image,您至少需要 Python 3.10。如果您的 Python 版本较旧,pip 将找到最兼容的最新版本。

# Update pip
python -m pip install -U pip
# Install scikit-image
python -m pip install -U scikit-image

要访问完整的演示数据集选择,请使用 scikit-image[data]。要包含一系列扩展 scikit-image 功能的其他科学 Python 包,例如并行处理,您可以安装 scikit-image[optional] 包:

python -m pip install -U scikit-image[optional]

警告

请不要将 sudopip 命令一起使用,因为 pip 可能会覆盖关键的系统库,这可能需要您重新安装操作系统。

1.3.2. conda#

Miniconda 是 Anaconda 包的精简版;你需要自己安装像 scikit-image 这样的包。与 Anaconda 一样,它安装 Python 并提供虚拟环境。

一旦你设置好了 conda 环境,你可以使用以下命令安装 scikit-image

conda install scikit-image

1.4. 系统包管理器#

使用包管理器(如 yumapt-get 等)来安装 scikit-image 或其他 Python 包并不是最佳选择:

  • 你可能会得到一个较旧的版本。

  • 你可能希望在包管理器之外进行更新和添加新包,这会让你遇到与在没有虚拟环境的情况下使用pip时看到的类似的依赖冲突。

  • 由于操作系统使用 Python,因此存在额外的风险,所以如果你对 Python 进行系统范围的更改(以 root 身份安装或使用 sudo),你可能会破坏操作系统。

1.5. 下载所有演示数据集#

我们示例中使用的一些数据托管在线上,并且不会通过上述步骤默认安装。数据在第一次调用时下载一次,但这需要互联网连接。如果您希望下载所有演示数据集以便离线工作,请确保安装了 pooch 包,然后运行此命令:

python -c 'import skimage as ski; ski.data.download_all()'

或者在你喜欢的交互式 Python 环境中调用 ``ski.data.download_all()``(IPython, Jupyter notebook, …)。

1.6. 附加帮助#

如果你还有问题,可以通过以下方式联系我们。

要建议对这些说明进行更改,请在 GitHub 上提交问题

2. 为贡献者安装 scikit-image#

我们假设您已经在计算机上配置了默认的 Python 环境,并且您打算在其中安装 scikit-image

我们还会对你的系统做出一些额外的假设:

  • 你已经设置好了 C 编译器。

  • 你已经设置好了 C++ 编译器。

  • 您正在运行的 Python 版本与我们在 pyproject.toml 中列出的系统兼容。

  • 你已经将git仓库克隆到一个名为 scikit-image 的目录中。你已经将 upstream 远程设置为指向我们的仓库,并将 origin 指向你的分支。

此目录包含以下文件:

scikit-image
├── asv.conf.json
├── azure-pipelines.yml
├── benchmarks/
├── CITATION.bib
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.rst
├── CONTRIBUTORS.txt
├── doc/
├── INSTALL.rst
├── LICENSE.txt
├── MANIFEST.in
├── meson.build
├── meson.md
├── pyproject.toml
├── README.md
├── RELEASE.txt
├── requirements/
├── requirements.txt
├── skimage/
├── TODO.txt
└── tools/

以下所有命令均假定从包含上述文件的 scikit-image 目录中运行。

2.1. 构建环境设置#

一旦你克隆了 scikit-image 仓库的分支,你应该设置一个适合 scikit-image 的 Python 开发环境。你可以选择你喜欢的环境管理器。这里我们为两种流行的环境管理器提供说明:``venv``(基于 pip)和 ``conda``(Anaconda 或 Miniconda)。

2.1.1. venv#

# Create a virtualenv named ``skimage-dev`` that lives outside of the repository.
# One common convention is to place it inside an ``envs`` directory under your home directory:
mkdir ~/envs
python -m venv ~/envs/skimage-dev
# Activate it
# (On Windows, please use ``skimage-dev\Scripts\activate``)
source ~/envs/skimage-dev/bin/activate
# Install main development and runtime dependencies
pip install -r requirements.txt
# Install build dependencies of scikit-image
pip install -r requirements/build.txt
# Build scikit-image from source
spin build
# The new version lives under `${PWD}/build-install/.../site-packages`.
# Test your installation
spin test
# Build docs
spin docs
# Try the new version in IPython
spin ipython

2.1.2. conda#

在使用 conda 进行开发时,我们建议添加 conda-forge 频道以获取许多依赖项的最新版本。我们使用的一些依赖项(用于测试和文档)在默认的 Anaconda 频道中不可用。请在开始之前遵循官方的 conda-forge 安装说明

# Create a conda environment named ``skimage-dev``
conda create --name skimage-dev
# Activate it
conda activate skimage-dev
# Install main development and runtime dependencies
conda install -c conda-forge --file requirements/default.txt
conda install -c conda-forge --file requirements/test.txt
conda install -c conda-forge pre-commit
# Install build dependencies of scikit-image
pip install -r requirements/build.txt
# Build scikit-image from source
spin build
# The new version lives under `${PWD}/build-install/.../site-packages`.
# Test your installation
spin test
# Build docs
spin docs
# Try the new version in IPython
spin ipython

有关构建和使用 spin 包的更多信息,请参阅 meson.md

2.2. 测试#

使用以下命令测试安装是否正确运行:

pytest skimage

2.3. 更新安装#

在更新您的安装之前,通常您会想要获取最新的源代码:

git checkout main
git pull upstream main

并且你可能希望从那里创建一个特性分支。当你在这个分支上工作时,你可以使用以下命令重新构建 scikit-image:

spin build

重复的、增量的构建通常工作得很好,但如果你注意到构建问题,请使用以下命令从头开始重新构建:

spin build --clean

2.4. 平台特定说明#

Windows

Windows 编译过程的概述包含在我们的 `Azure Pipelines 设置`_(一个持续集成服务)中。

Debian 和 Ubuntu

安装合适的编译器:

sudo apt-get install build-essential

2.5. 完整需求列表#

构建要求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
meson-python>=0.15
wheel
setuptools>=67
packaging>=21
ninja
Cython>=3.0.4
pythran
numpy>=2.0.0rc1
spin==0.8
build

运行时需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
numpy>=1.23
scipy>=1.9
networkx>=2.8
pillow>=9.1
imageio>=2.33
tifffile>=2022.8.12
packaging>=21
lazy-loader>=0.4

测试要求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
asv
numpydoc>=1.7
pooch>=1.6.0
pytest>=7.0
pytest-cov>=2.11.0
pytest-localserver
pytest-faulthandler
pytest-doctestplus

文档要求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
sphinx>=7.3
sphinx-gallery>=0.14
numpydoc>=1.7
sphinx-copybutton
pytest-runner
matplotlib>=3.6
dask[array]>=2022.9.2
pandas>=1.5
seaborn>=0.11
pooch>=1.6
tifffile>=2022.8.12
myst-parser
ipywidgets
ipykernel
plotly>=5.10
kaleido
scikit-learn>=1.1
sphinx_design>=0.5
pydata-sphinx-theme>=0.15.2
PyWavelets>=1.1.1
pytest-doctestplus

开发者要求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
pre-commit
ipython
tomli; python_version < '3.11'

数据要求

完整的演示数据集选择仅在安装以下内容后可用:

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
pooch>=1.6.0

可选要求

你可以使用 scikit-image 和上述基本要求,但某些功能仅在安装以下内容后才可用:

  • SimpleITK

    可选的 I/O 插件,提供多种 格式,包括在医学影像中使用的专业格式。

  • Astropy

    提供FITS I/O功能。

  • PyAMG

    pyamg 模块用于随机游走分割的快速 cg_mg 模式。

  • Dask

    dask 模块用于加速某些函数。

  • Matplotlib

    用于各种功能,例如绘图、分割、读取图像。

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
SimpleITK
astropy>=5.0
cloudpickle>=0.2.1
dask[array]>=2021.1.0
matplotlib>=3.6
pooch>=1.6.0
pyamg
PyWavelets>=1.1.1
scikit-learn>=1.1

2.6. 帮助贡献者安装#

请参阅上面的 附加帮助