用numpy调整图像大小

时间:2017-01-24 23:48:16

标签: python image size

假设我有一个图像呈现为这个numpy数组:

array([[ 55, 229, 185,  21, 128,  50, 109, 121, 251],
   [138,   0, 143, 153,  22, 244, 102,   6,  63],
   [250, 235,  57,  28, 220,  15, 217, 147,  70],
   [121, 164, 128, 224,  56, 206, 104,  87, 154],
   [232,  51,  20, 235,   8, 200, 119, 234, 180],
   [182,  79,  79,  22, 221, 233,  54,  11, 209],
   [249,  64,  92,  70, 167, 151, 214, 188, 213]], dtype=uint8)

这是7X9矩阵,我希望将图像的宽度加倍到7x18。 我想知道当你想要压缩图像时该怎么做,但我不知道如果我想增加尺寸我应该怎么做。

谢谢!

`

1 个答案:

答案 0 :(得分:1)

将您的数组放入a,然后

np.repeat(a, 2, axis=1)

给出

array([[ 55,  55, 229, 229, 185, 185,  21,  21, 128, 128,  50,  50, 109,
    109, 121, 121, 251, 251],
   [138, 138,   0,   0, 143, 143, 153, 153,  22,  22, 244, 244, 102,
    102,   6,   6,  63,  63],
   [250, 250, 235, 235,  57,  57,  28,  28, 220, 220,  15,  15, 217,
    217, 147, 147,  70,  70],
   [121, 121, 164, 164, 128, 128, 224, 224,  56,  56, 206, 206, 104,
    104,  87,  87, 154, 154],
   [232, 232,  51,  51,  20,  20, 235, 235,   8,   8, 200, 200, 119,
    119, 234, 234, 180, 180],
   [182, 182,  79,  79,  79,  79,  22,  22, 221, 221, 233, 233,  54,
     54,  11,  11, 209, 209],
   [249, 249,  64,  64,  92,  92,  70,  70, 167, 167, 151, 151, 214,
    214, 188, 188, 213, 213]])

其形状为7x18。