Guy
Guy

Reputation: 115

resizing image with numpy

lets say i have an image presented as this numpy array:

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)

this is 7X9 matrix and i want to double the width of the image to 7x18. i know what to do when you want to compress an image, but im not sure what i supposed to do if i want to increase the size.

thanks!

`

Upvotes: 1

Views: 2480

Answers (1)

Ben Schmidt
Ben Schmidt

Reputation: 401

Put your array in a, then

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

gives

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]])

Which has shape 7x18.

Upvotes: 1

Related Questions