我注意到 scipy.ndimage.zoom 的结果取决于原始图像的大小。在以下代码示例中,会生成一个棋盘图像,然后使用 ndimage.zoom 进行缩放。如果一个棋盘格只有 2x2 像素,则缩放系数似乎太大并且生成的图像会被裁剪。相反,如果图块的尺寸为 10x10,则结果看起来不错。
from __future__ import division
import numpy as np
from scipy import ndimage, misc
import wx
y,x = 2,2 # change tile size here
imgdata = np.zeros((y,x),dtype='uint8')
imgdata[y/2:,x/2:] = 255
imgdata[:y/2,:x/2] = 255
imgdata = np.tile(imgdata,(4,4))
imgdata = np.array((imgdata,imgdata,imgdata))
d,y,x = imgdata.shape
zoom = 200.0/y
w, h = int(x*zoom), int(y*zoom)
app = wx.App(None)
zoomed = np.ascontiguousarray(ndimage.interpolation.zoom(imgdata,[1,zoom, zoom],order=0).transpose((1,2,0)), dtype='uint8')
image = wx.ImageFromBuffer(w, h, zoomed)
image.SaveFile('zoomed.png',wx.BITMAP_TYPE_PNG)
据了解,我一直在使用 scipy.misc.imresize ,它没有显示这种行为,但我想避免对 PIL 的额外依赖。
我做错了什么还是这是缩放中的错误?