4

I am looking for a way to rescale the matrix given by reading in a png file using the matplotlib routine imread, e.g.

from pylab import imread, imshow, gray, mean
from matplotlib.pyplot import show
a = imread('spiral.png')
#generates a RGB image, so do
show()

but actually I want to manually specify the dimension of $a$, say 200x200 entries, so I need some magic command (which I assume exists but cannot be found by myself) to interpolate the matrix.

Thanks for any useful comments : )

Cheers

bios
  • 997
  • 3
  • 11
  • 20
  • Solved by http://stackoverflow.com/questions/5586719/scipy-interpolation-how-to-resize-resample-3x3-matrix-to-5x5 – bios Dec 10 '12 at 08:58

1 Answers1

3

You could try using the PIL (Image) module instead, together with numpy. Open and resize the image using Image then convert to array using numpy. Then display the image using pylab.

import pylab as pl
import numpy as np
from PIL import Image

path = r'\path\to\image\file.jpg'

img = Image.open(path)

img.resize((200,200))

a = np.asarray(img)

pl.imshow(a)
pl.show()

Hope this helps.

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • 3
    Seems like with the current version of PIL, resize doesn't modify an image but creates a new one, so you need `img = img.resize((200, 200))` instead – Jezzamon Feb 10 '16 at 02:34
  • Really? Does this resize the image? img.resize() makes no difference whatsoever! Also, whu do you re-convert it to ndarray? It makes no difference, pl.imshow(img) does already the job. Finally "import Image" doesn't work. It should be "from PIL import Image". Godssake, man! And yet, I don't downvote you, as lot do, since I never apply this cruel method invented by stackexchange. – Apostolos Jul 04 '18 at 21:40
  • 3
    Thanks for the feedback, note that the answer was written almost 6 years ago so what worked then might not work now. Also, if you have a better answer, it would be more helpful to add that to the answers instead of complaining about an existing one. – irenemeanspeace Jul 13 '18 at 09:05