0

I would like to take an image and change the scale of the image, while it is a numpy array. I would like to do it with native NumPy functions w/o PIL, cv2, SciPy etc

now I have this:

from copy import copy
import numpy as np
from scipy import misc


img = misc.face()  # racoon from SciPy(np.ndarray)
img2 = copy(img)  # copy of racoon, because misc.face() is Descriptor(?)
img2.shape()  # (768, 1024, 3)

Which I need shape = (3072, 4096, 3)

I can do it easy with Pillow

CONVERT_IMAGE = Image.fromarray(img.astype('uint8'), 'RGB')
CONVERT_IMAGE = CONVERT_IMAGE.resize((4096, 3072), Image.NEAREST)

IMAGE_AS_ARRAY = np.asarray(CONVERT_IMAGE)
IMAGE_AS_ARRAY.shape  # 3072 4096 3

but I realy need to do this only with NumPy functions w/o other libs

Can you help me ? I'm really weak in NumPy and 3D-arrays

3
  • numpy doesn't do that kind of resizing. It requires some sort of interpolation, which image libraries have developed. Commented Sep 25, 2020 at 11:34
  • @hpauji tnx for ur anwer, but i find downgrade of image size in numpy in some resources in Internet. But I cant find anywhere expand of image size Commented Sep 25, 2020 at 11:39
  • You can slice out a subset of the values, or as the answer shows replicate values. But image software can do more sophisticated resizing - averaging values, smoothing the image and so on. Commented Sep 25, 2020 at 18:32

1 Answer 1

2

Limited to whole integer upscaling with some scaling factor n and without actual interpolation, you could use np.repeat twice to get the described result:

import numpy as np

# Original image with shape (4, 3, 3)
img = np.random.randint(0, 255, (4, 3, 3), dtype=np.uint8)

# Scaling factor for whole integer upscaling
n = 4

# Actual upscaling (results to some image with shape (16, 12, 3)
img_up = np.repeat(np.repeat(img, n, axis=0), n, axis=1)

# Outputs
print(img[:, :, 1], '\n')
print(img_up[:, :, 1])

Here's some output:

[[148 242 171]
 [247  40 152]
 [151 131 198]
 [ 23 185 144]] 

[[148 148 148 148 242 242 242 242 171 171 171 171]
 [148 148 148 148 242 242 242 242 171 171 171 171]
 [148 148 148 148 242 242 242 242 171 171 171 171]
 [148 148 148 148 242 242 242 242 171 171 171 171]
 [247 247 247 247  40  40  40  40 152 152 152 152]
 [247 247 247 247  40  40  40  40 152 152 152 152]
 [247 247 247 247  40  40  40  40 152 152 152 152]
 [247 247 247 247  40  40  40  40 152 152 152 152]
 [151 151 151 151 131 131 131 131 198 198 198 198]
 [151 151 151 151 131 131 131 131 198 198 198 198]
 [151 151 151 151 131 131 131 131 198 198 198 198]
 [151 151 151 151 131 131 131 131 198 198 198 198]
 [ 23  23  23  23 185 185 185 185 144 144 144 144]
 [ 23  23  23  23 185 185 185 185 144 144 144 144]
 [ 23  23  23  23 185 185 185 185 144 144 144 144]
 [ 23  23  23  23 185 185 185 185 144 144 144 144]]
----------------------------------------
System information
----------------------------------------
Platform:     Windows-10-10.0.16299-SP0
Python:       3.8.5
NumPy:        1.19.2
----------------------------------------
Sign up to request clarification or add additional context in comments.

3 Comments

No Hans, I can't do this. third massive is RGB color and length of third massive is must be three, don't 12. Thanks for ur answer
Third dimension is still 3 in my solution!? You upscale from (768, 1024, 3) to (3072, 4096, 3), which I also do, if I would start at (768, 1024, 3). Also, please pay attention, I just output one channel of the final result - for readibility.
this is nearest-neighbor right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.