如何在我的示例中用 Pillow 替换 scipy.misc.imresize?
How to replace scipy.misc.imresize with Pillow in my example?
我阅读了一些关于 scipy.misc.imresize
在早期版本中被删除以及如何改用 Pillow 的话题。以下是我产生错误的代码行。
image_gray_resized = scipy.misc.imresize(arr=image_gray, size=fraction, interp='bicubic')
在我看到的例子中,这个语句的 Pillow 版本只使用了两个参数,不像我的使用了三个。我不知道我必须如何更改它才能使其在 Pillow 中工作,因为我不知道
中的参数如何
scipy.misc.imresize(*args, **kwds)
和枕头版
numpy.array(Image.fromarray(arr).resize())
相关。更重要的是我自己的 3 参数 imresize
如何适应这个。
让我们查看 scipy.misc.imresize
的旧文档页面:
arr
是作为 NumPy 数组的图像本身。
size
可以
- 一个
int
(表示某个百分比),
- a
float
(表示一些分数,这是你的例子),或
- a
tuple
(表示目标大小)。
interp
是要使用的插值方法。
现在,让我们检查一下 PIL.Image.resize
的功能:
size
必须是 tuple
。因此,在使用上述百分比或分数时,您需要事先确定目标大小。
resample
是resampling filter,基本上就是上面给出的插值方法
这就是我们导出正确代码所需知道的全部内容:
from imageio import imread # scipy.misc.imread is deprecated
import numpy as np
from PIL import Image # scipy.misc.imresize is deprecated
# Read image, get width and height
img = imread('path/to/your/image.png')
h, w = img.shape[:2]
print(img.shape)
# (241, 300, 3)
# Fraction as float
fraction = 0.2
img_resized = np.array(Image.fromarray(img).resize((int(fraction * w),
int(fraction * h)),
Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
# Percentage as integer
percentage = 20
img_resized = np.array(Image.fromarray(img).resize((int(percentage / 100 * w),
int(percentage / 100 * h)),
Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
# Size as tuple
size = (60, 48)
img_resized = np.array(Image.fromarray(img).resize(size, Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
该示例展示了如何处理三种不同的缩放方式(百分比、分数、目标大小)。对于不同的插值方法,您只需要从 nearest
映射到 PIL.Image.NEAREST
,依此类推。
编辑: 只是为了进一步解释:PIL.Image.fromarray
converts the input NumPy array to a Pillow PIL.Image.Image
对象,这样你就可以使用 PIL.Image.resize
。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
imageio: 2.9.0
NumPy: 1.20.2
Pillow: 8.2.0
----------------------------------------
我阅读了一些关于 scipy.misc.imresize
在早期版本中被删除以及如何改用 Pillow 的话题。以下是我产生错误的代码行。
image_gray_resized = scipy.misc.imresize(arr=image_gray, size=fraction, interp='bicubic')
在我看到的例子中,这个语句的 Pillow 版本只使用了两个参数,不像我的使用了三个。我不知道我必须如何更改它才能使其在 Pillow 中工作,因为我不知道
中的参数如何scipy.misc.imresize(*args, **kwds)
和枕头版
numpy.array(Image.fromarray(arr).resize())
相关。更重要的是我自己的 3 参数 imresize
如何适应这个。
让我们查看 scipy.misc.imresize
的旧文档页面:
arr
是作为 NumPy 数组的图像本身。size
可以- 一个
int
(表示某个百分比), - a
float
(表示一些分数,这是你的例子),或 - a
tuple
(表示目标大小)。
- 一个
interp
是要使用的插值方法。
现在,让我们检查一下 PIL.Image.resize
的功能:
size
必须是tuple
。因此,在使用上述百分比或分数时,您需要事先确定目标大小。resample
是resampling filter,基本上就是上面给出的插值方法
这就是我们导出正确代码所需知道的全部内容:
from imageio import imread # scipy.misc.imread is deprecated
import numpy as np
from PIL import Image # scipy.misc.imresize is deprecated
# Read image, get width and height
img = imread('path/to/your/image.png')
h, w = img.shape[:2]
print(img.shape)
# (241, 300, 3)
# Fraction as float
fraction = 0.2
img_resized = np.array(Image.fromarray(img).resize((int(fraction * w),
int(fraction * h)),
Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
# Percentage as integer
percentage = 20
img_resized = np.array(Image.fromarray(img).resize((int(percentage / 100 * w),
int(percentage / 100 * h)),
Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
# Size as tuple
size = (60, 48)
img_resized = np.array(Image.fromarray(img).resize(size, Image.BICUBIC))
print(img_resized.shape)
# (48, 60, 3)
该示例展示了如何处理三种不同的缩放方式(百分比、分数、目标大小)。对于不同的插值方法,您只需要从 nearest
映射到 PIL.Image.NEAREST
,依此类推。
编辑: 只是为了进一步解释:PIL.Image.fromarray
converts the input NumPy array to a Pillow PIL.Image.Image
对象,这样你就可以使用 PIL.Image.resize
。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
imageio: 2.9.0
NumPy: 1.20.2
Pillow: 8.2.0
----------------------------------------