(Pythonnumpy) how to resize and slice an numpy array with out a loop?
Tag : python , By : barefootChild
Date : March 29 2020, 07:55 AM
To fix this issue Yes you can use indexing with steps (in your example step would be 2): import numpy as np
a = np.array([[1,2,3,4], [5,6,7,8], [9,8,7,6], [5,4,3,2]])
a[::2, ::2]
array([[1, 3],
[9, 7]])

I am trying to convert a numpy array to PIL.Image. But it is giving black images. The numpy array is returned by pyramid
Tag : python , By : Simon Capewell
Date : March 29 2020, 07:55 AM
hop of those help? Check the values in pyr. Maybe they are values between 0 and `, while your RGB image is expected to be with values between 0 and 255.

How to resize Nd numpy image?
Date : November 11 2020, 03:01 PM
seems to work fine Reshape to split each axes into one more axis each of length 2, giving us a 6D array and then get the mean along those latter ones (axes : 1,3,5)  m,n,r = a.shape
out = a.reshape(m//2,2,n//2,2,r//2,2).mean((1,3,5))
def shrink(a, S=2): # S : shrink factor
new_shp = np.vstack((np.array(a.shape)//S,[S]*a.ndim)).ravel('F')
return a.reshape(new_shp).mean(tuple(1+2*np.arange(a.ndim)))
In [407]: a
Out[407]:
array([[[1, 5, 8, 2],
[5, 6, 4, 0],
[8, 5, 5, 5],
[1, 0, 0, 0]],
[[0, 0, 7, 6],
[3, 5, 4, 3],
[4, 5, 1, 3],
[6, 7, 4, 0]]])
In [408]: a[:2,:2,:2].mean()
Out[408]: 3.125
In [409]: a[:2,:2,2:4].mean()
Out[409]: 4.25
In [410]: a[:2,2:4,:2].mean()
Out[410]: 4.5
In [411]: a[:2,2:4,2:4].mean()
Out[411]: 2.25
In [412]: shrink(a, S=2)
Out[412]:
array([[[ 3.125, 4.25 ],
[ 4.5 , 2.25 ]]])

Numpy Resize/Rescale Image
Date : March 29 2020, 07:55 AM
should help you out Yeah, you can install opencv (this is a library used for image processing, and computer vision), and use the cv2.resize function. And for instance use: import cv2
import numpy as np
img = cv2.imread('your_image.jpg')
res = cv2.resize(img, dsize=(54, 140), interpolation=cv2.INTER_CUBIC)

Tensorflow tf.image.resize_image_with_crop_or_pad For 3D Images
Tag : python , By : Ben Humphrys
Date : March 29 2020, 07:55 AM
it should still fix some issue i think your best bet is tf.pad, below code was not tested. target_z = 170
# x is shape of (166, 256, 256)
zp = 170x.get_shape().as_list()[0]
# what if zp is negative ?
paddings = tf.constant([[0, zp], [0, 0], [0,0]])
tf.pad(x, paddings, "CONSTANT")

