Reputation: 41
I would like to change the size of the image and it is a numpy array. the size of this picture is (480, 640, 3) and i would like to make it (55, 74, 3).
after searching in python libraries I saw, that the most function of resize the images takes the mean function or makes sum of pixels to reduce the size.
but in my case i would like to do something else, that i want to take the first pixel of the first column then drop the second pixel, take the third pixel of the third column then drop the fourth pixel and so on. for the rows stripping the second, fourth,sixth, .....out. the shape should look like that https://i.sstatic.net/Bd7z7.jpg only take the brown pixels.
how can I implement what is shown like in the picture ? is there any fuction or library for this? thanks
Upvotes: 1
Views: 492
Reputation: 207375
You can do that with Numpy, which is what all the Python libraries (OpenCV, PIL/Pillow, scikit-image, Wand, vips) use underneath.
So, supposing we create a dummy image like this:
import numpy as np
# Synthetically create big image
big = np.arange(48, dtype=np.uint8).reshape((6,8))
That will look like this:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47]], dtype=uint8)
If you now want every second row and every second column, use:
subsampled = big[0::2, 0::2]
and you will get this in your subsampled
image:
array([[ 0, 2, 4, 6],
[16, 18, 20, 22],
[32, 34, 36, 38]], dtype=uint8)
If you want to re-apply it, do the same thing again:
subsampledx2 = subsampled[0::2, 0::2]
and you will get this:
array([[ 0, 4],
[32, 36]], dtype=uint8)
I am not sure how you are hoping that taking every second row and every second column will reduce a 640x480 image to 74x55, maybe you plan to apply it recursively?
Sorry, if you have a colour image with 3 channels of RGB, you can use an extra colon to explicitly get all 3 RGB channels:
subsampled = big[0::2, 0::2, :]
Upvotes: 1