dor*_*bin 3 python scipy image-resizing python-imaging-library
我正在尝试拍摄一个小图像并使用 Python 制作它的大(“像素化”)版本。我已经尝试在两者中使用最近邻调整大小方法scipy,PIL.Image并且两者都输出相同的错误结果。这是尝试将图像上采样 3 倍的示例:
import numpy as np
from scipy.misc import imresize
from PIL import Image
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
img_nn_scipy = imresize(img, (9, 9), interp='nearest')
img_nn_pil = np.array(Image.fromarray(img).resize((9, 9), Image.NEAREST))
print img_nn_scipy
print img_nn_pil
Run Code Online (Sandbox Code Playgroud)
两个打印语句输出相同的结果:
[[1 1 1 2 2 2 2 3 3]
[1 1 1 2 2 2 2 3 3]
[1 1 1 2 2 2 2 3 3]
[4 4 4 5 5 5 5 6 6]
[4 4 4 5 5 5 5 6 6]
[4 4 4 5 5 5 5 6 6]
[4 4 4 5 5 5 5 6 6]
[7 7 7 8 8 8 8 9 9]
[7 7 7 8 8 8 8 9 9]]
Run Code Online (Sandbox Code Playgroud)
我希望原始图像中的每个数字现在都将替换为一个 3x3 块,所有块都包含相同的值。相反,块有不同的大小,只有1块是 3x3。
是否有一个原因?有没有办法在 Python 中合理地解决这个问题?
谢谢!
试试最新的Pillow而不是 PIL。
In [1]: import numpy as np
...: from PIL import Image
...:
In [2]: img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
In [3]: img_nn_pil = np.array(Image.fromarray(img).resize((9, 9), Image.NEAREST))
In [4]: print img_nn_pil
[[1 1 1 2 2 2 3 3 3]
[1 1 1 2 2 2 3 3 3]
[1 1 1 2 2 2 3 3 3]
[4 4 4 5 5 5 6 6 6]
[4 4 4 5 5 5 6 6 6]
[4 4 4 5 5 5 6 6 6]
[7 7 7 8 8 8 9 9 9]
[7 7 7 8 8 8 9 9 9]
[7 7 7 8 8 8 9 9 9]]
Run Code Online (Sandbox Code Playgroud)
这只是一个已修复的错误。
| 归档时间: |
|
| 查看次数: |
5133 次 |
| 最近记录: |