I'm not sure how to go about scaling a 2-dimensional array. Given the array below, whose dimensions are 8x10, say I needed to scale it to 5x6 -- I've looked for concrete examples on wikipedia, but without much grounding in matrix math I'm a bit lost. If someone could point me in the right direction I'd really appreciate it!
[
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 0, 1, 1]
]
Since your array looks like it is a binary image of a lower case 'a' letter, I'm guessing that you mean scaling in the image sense.
To do that, I would recommend using the imresize
function in scipy.misc (which is taken from PIL, I believe). Here's an example:
import numpy as np
from scipy.misc import imresize
img = np.array([
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 0, 1, 1]
])
newimg = imresize(img, (6,5))
and newimg is then
array([[ 0, 0, 255, 255, 0],
[ 0, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255],
[255, 255, 255, 255, 255],
[255, 255, 0, 0, 255],
[255, 255, 255, 255, 255]], dtype=uint8)
which isn't perfect, but you can change the 255's to 1's easily enough. Also, if you get the development version of Scipy, currently version 9, then you have some other parameters(scroll down to imresize - no anchor) that you can input to imresize such as the interpolation method and PIL mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With