如何使用imageio调整图像大小? [英] How to resize an image using imageio?
问题描述
考虑类型为 imageio.core.util.Array
的图像 img
.
img
的形状为(256,256,3)
.我想将其调整为(128,128,3)
.
The shape of img
is (256, 256, 3)
. I want to resize it to (128, 128, 3)
.
我至少尝试了以下三个:
I tried at least the following three:
img.resize(img_res, pilmode="RGB")
img.resize(img_res)
img = cv2.resize(img, self.img_res)
此处 img_res =(128,128)
.
没有一个人运作良好.如何将图像调整为所需的大小?
None of them worked well. How can I resize my image to the desired size?
推荐答案
根据 imageio.core.util.Array
上的文档, Array
是" np.ndarray
[...]".因此,在某些类型为 Array
的 img
上调用 resize
时,实际的调用将转到 np.resize
!这很重要.
According to the documentation on imageio.core.util.Array
, Array
is "a subclass of np.ndarray
[...]". Thus, when calling resize
on some img
of type Array
, the actual call goes to np.ndarray.resize
– which IS NOT np.resize
! That's important here.
摘自 np.ndarray.resize
上的文档:
提高:
ValueError
如果 a
不拥有自己的数据[...]
If a
does not own its own data [...]
这就是为什么,像这样的一些代码
That's why, some code like
import imageio
img = imageio.imread('path/to/your/image.png')
img.resize((128, 128))
将以这种方式失败:
Traceback (most recent call last):
img.resize((128, 128))
ValueError: cannot resize this array: it does not own its data
该错误似乎与 Array
类绑定,因为以下代码也因相同的错误消息而失败:
That error seems to be bound to the Array
class, because the following code also fails with the same error message:
from imageio.core.util import Array
import numpy as np
img = Array(np.zeros((200, 200, 3), np.uint8))
img.resize((128, 128))
很明显, Array
类仅将视图存储到一些无法直接访问的NumPy数组,也许是一些内部内存缓冲区!?
Obviously, the Array
class only stores a view to some not directly accessible NumPy array, maybe some internal memory buffer!?
现在,让我们看看可能的解决方法:
Now, let's see possible workarounds:
实际上,像
那样使用 np.resize
import imageio
import numpy as np
img = imageio.imread('path/to/your/image.png')
img = np.resize(img, (128, 128, 3))
不是一个好选择,因为 np.resize
并非旨在正确调整图像大小.因此,结果会失真.
is not a good choice, because np.resize
is not designed to properly resize images. So, the result is distorted.
使用OpenCV对我来说很好:
Using OpenCV works fine for me:
import cv2
import imageio
img = imageio.imread('path/to/your/image.png')
img = cv2.resize(img, (128, 128))
请记住,OpenCV使用BGR排序,而imageio使用RGB排序-例如,在还使用 cv2.imshow
时,这一点很重要.
Keep in mind, that OpenCV uses BGR ordering, while imageio uses RGB ordering – that's important when also using cv2.imshow
for example.
使用枕头也可以正常工作:
Using Pillow also works without problems:
import imageio
from PIL import Image
img = imageio.imread('path/to/your/image.png')
img = Image.fromarray(img).resize((128, 128))
最后,还有 skimage.transform.resize
:
import imageio
from skimage.transform import resize
img = imageio.imread('path/tp/your/image.png')
img = resize(img, (128, 128))
选择最适合您的需求!
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
imageio: 2.9.0
NumPy: 1.19.5
OpenCV: 4.5.1
Pillow: 8.1.0
scikit-image: 0.18.1
----------------------------------------
这篇关于如何使用imageio调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!