DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning dissapear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly.
时间: 2023-06-10 09:06:01 浏览: 1749
该警告表示使用的是 ImageIO 库的 v2 版本,但是它将从 v3 版本开始更改函数的行为。为了避免这个警告,可以使用 `import imageio.v2 as imageio` 或直接调用 `imageio.v2.imread` 函数来保持当前行为。如果您想使用新的行为,可以使用 ImageIO 的 v3 版本。
相关问题
C:\Users\John\AppData\Local\Temp\ipykernel_4148\3128955772.py:4: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning dissapear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly. kitten = imread('cs231n/notebook_images/kitten.jpg') C:\Users\John\AppData\Local\Temp\ipykernel_4148\3128955772.py:5: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning dissapear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly. puppy = imread('cs231n/notebook_images/puppy.jpg')
<think>我们遇到了两个DeprecationWarning,它们都来自imageio.imread函数。警告信息表明,从ImageIO v3开始,imread函数的行为将发生变化,为了保持当前行为并消除警告,我们应该使用imageio.v2.imread或者直接调用imageio.v2.imread。
因此,解决方案是修改代码,将原来的:
kitten = imread('cs231n/notebook_images/kitten.jpg')
puppy = imread('cs231n/notebook_images/puppy.jpg')
改为:
kitten = imageio.v2.imread('cs231n/notebook_images/kitten.jpg')
puppy = imageio.v2.imread('cs231n/notebook_images/puppy.jpg')
或者,在导入时使用:
import imageio.v2 as imageio
然后使用:
kitten = imageio.imread('cs231n/notebook_images/kitten.jpg')
puppy = imageio.imread('cs231n/notebook_images/puppy.jpg')
两种方式都可以消除警告。
下面我们提供修改后的代码:</think>### 解决 `imageio.imread` 弃用警告的方法
这个警告是由于 `imageio` 库从 v3 版本开始改变 `imread` 的行为引起的。以下是两种修复方案:
#### 方案一:显式使用 v2 版本的读取方式
```python
# 修改前
kitten = imread('cs231n/notebook_images/kitten.jpg')
puppy = imread('cs231n/notebook_images/puppy.jpg')
# 修改后
import imageio.v2 as imageio # 显式导入v2版本
kitten = imageio.imread('cs231n/notebook_images/kitten.jpg')
puppy = imageio.imread('cs231n/notebook_images/puppy.jpg')
```
#### 方案二:直接调用 v2 接口
```python
# 修改后(无需修改导入语句)
kitten = imageio.v2.imread('cs231n/notebook_images/kitten.jpg')
puppy = imageio.v2.imread('cs231n/notebook_images/puppy.jpg')
```
### 警告原因说明:
1. **API 变更**:
- ImageIO v3 改变了 `imread()` 的行为
- 新版本默认使用 `iio.v3.imread`,与旧版行为不同
2. **向后兼容性**:
- 使用 `imageio.v2` 可保持旧版行为
- 避免现有代码因API变更而中断
3. **推荐做法**:
```python
# 最佳实践:明确指定版本
import imageio.v2 as iio
img = iio.imread('image.jpg')
```
### 完整修复示例
```python
# 修复后的代码
import imageio.v2 as imageio
import numpy as np
# 读取图像(无警告)
kitten = imageio.imread('cs231n/notebook_images/kitten.jpg')
puppy = imageio.imread('cs231n/notebook_images/puppy.jpg')
# 图像处理代码...
# kitten = kitten.astype(np.float32).transpose(2, 0, 1)
# puppy = puppy.astype(np.float32).transpose(2, 0, 1)
```
### 额外建议:
1. **检查依赖版本**:
```bash
pip show imageio
```
如果版本 ≥ 3.0,必须使用上述修复
2. **长期解决方案**:
- 更新代码适应 v3 API
- 参考官方迁移指南:
[ImageIO v3 Migration](https://imageio.readthedocs.io/en/stable/migration.html)
DeprecationWarning: Shapely 2.0 is installed, but because PyGEOS is also installed, GeoPandas still uses PyGEOS by default. However, starting with version 0.14, the default will switch to Shapely. To force to use Shapely 2.0 now, you can either uninstall PyGEOS or set the environment variable USE_PYGEOS=0. You can do this before starting the Python process, or in your code before importing geopandas:
这是一个关于 Shapely 和 GeoPandas 的警告信息。它表明您的系统上同时安装了 Shapely 2.0 和 PyGEOS,但 GeoPandas 目前仍然默认使用 PyGEOS。然而,从版本0.14开始,GeoPandas 的默认设置将更改为使用 Shapely。如果您想现在强制使用 Shapely 2.0,您可以通过以下两种方式之一来实现:
1. 卸载 PyGEOS:您可以通过在命令行中运行 `pip uninstall pygeos` 来卸载 PyGEOS。这将使 GeoPandas 强制使用 Shapely 2.0。
2. 设置环境变量 `USE_PYGEOS=0`:您可以在启动 Python 进程之前或在导入 geopandas 之前的代码中设置环境变量 `USE_PYGEOS=0`。具体设置的方法取决于您使用的操作系统和终端。
请注意,在做出任何更改之前,建议您先了解您的代码和依赖项是否对 PyGEOS 有任何特定要求。如果没有特定要求,卸载 PyGEOS 或设置环境变量 `USE_PYGEOS=0` 应该是解决该警告的有效方法。
希望能帮到您!如果还有其他问题,请随时提问。
阅读全文
相关推荐

















