12

Hello I have an image ( 1024 x 1024) and I used "fromfile" command in numpy to put every pixel of that image into a matrix.

How can I reduce the size of the image ( ex. to 512 x 512) by modify that matrix a?

a = numpy.fromfile(( - path - ,'uint8').reshape((1024,1024))

I have no idea how to modify the matrix a to reduce the size of the image. So if somebody has any idea, please share your knowledge and I will be appreciated. Thanks


EDIT:

When I look at the result, I found that the reader I got read the image and put it into a "matrix". So I changed the "array" to matrix.

Jose told me I can take only even column and even row and put it into a new matrix . That will reduce the image to half size. What command in scipy/numpy do I need to use to do that?

Thanks

4
  • 1
    Is the question that you don't understand how to do it in Python, or you don't understand how to do image scaling, or both? Commented May 22, 2011 at 7:34
  • The long answer is that it depends on the type of image that you just read in. The short answer/question is "why can't you use a library?". Are you able to have PIL resize it and then you can call numpy.reshape()? Commented May 22, 2011 at 7:37
  • the image is in .dat file extention, and the reader I got can read the image and put it into a matrix. I made a mistake so I edited the post and change "array" to "matrix" since it is put as a matrix form. Commented May 22, 2011 at 21:40
  • Now what I have problem is how to take only even row and even column from the matrix. Are there any function in scipy or numpy that can do this? Commented May 22, 2011 at 21:42

3 Answers 3

22

If you want to resize to specific resolution, use scipy.misc.imresize:

import scipy.misc

i_width = 640
i_height = 480

scipy.misc.imresize(original_image, (i_height, i_width))
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is the correct answer. Straightforward, works directly with numpy array format of image. Thanks!
it is deprecated in scipy 1.0.0, skimage.transform.resize should be used
Sadly the skimage alternative doesn't currently have a "nearest neighbor" option, so if you need to, say, downsize sensor data from 1280 to 224, you'll need to do extra math yourself.
18

Use the zoom function from scipy:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html#scipy.ndimage.zoom

from scipy.ndimage.interpolation import zoom
a = np.ones((1024, 1024))
small_a = zoom(a, 0.5)

Comments

3

I think the easyiest way is to take only some columns and some rows of the image. Makeing a sample of the array. Take for example, only those even rows and the even columns, put it in a new array and you would have a half size new image.

5 Comments

@LearnMore - It's as simple as y = x[::2, ::2] (or ::3 if you wanted every third row, etc). Just be aware that this will cause aliasing problems, as it's essentially just "nearest neighbor" interpolation (it's very, very fast, though). @tillsten's answer is a much better way in general, but will have more overhead. It all depends on exactly what you need to do.
@Joe Kington - thanks. y = x[::2, ::2] is the first "::2" means get every 2 ROWs and the second "::2" means get every 2 COLUMS? and is x the matrix the I have originally and y is the new matrix?
Yes I tried it and originally the size is 2048 x 2048. Now it is 1024 x 1024. Thanks a lot
The right way to do it is, however, described below. Use the zoom function.
this is the wrong way to resize. If your image contains black and white lines alternatively on every pixel column and row wise, you will end up with either a complete black or white image at the end. The correct way is to use interpolation as described below.

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.