1

I want to reduce the size of an image using Python

    import numpy as np
    from skimage import io

    img = io.imread('img.jpg')
    smallImg = img[::2, ::2]

gives me image which 50 percent of the original because the step size of the slice is 2. How can i make it to be let's say 90 percent of original one?

Regular python slice did not help me. It seems that i don't know how to slice a list, so it will return me for example, 2nd, 3rd, 5th, 7th etc. elements. Let's say i have something like:

    arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    arr[::2]

Running the code above gives me:

   array([1, 3, 5, 7, 9])

However, i want opposite to this result:

   array([2, 3, 5, 6, 8, 9])
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Vladimir
  • 31
  • 4

3 Answers3

2

we can easily achieve that specifiying correct index values:

res = arr[np.arange(len(arr)) % 3 != 0]

result:

In [59]: res
Out[59]: array([2, 3, 5, 6, 8, 9])
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
0

Use this slicing method

arr[1:3,4:6,7:9]...

Or you can slice the list 2 values at a time, store it in different variable, and append or extend the list together.

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
seunmi
  • 1
-2

To get array([2, 3, 5, 6, 8, 9]) You have to alter the code to: arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) arr[1:-1:2] i forgot the step earlier

Negative indexes start form the last item So the item indexed -1 is the last (10) And the item indexed - 2 is before the last (9) and so on

Rami Isam
  • 1,412
  • 1
  • 7
  • 6