Resizing image algorithm in python

Posted by hippocampus on Stack Overflow See other posts from Stack Overflow or by hippocampus
Published on 2012-09-16T14:09:42Z Indexed on 2012/09/16 15:38 UTC
Read the original article Hit count: 467

So, I'm learning my self python by this tutorial and I'm stuck with exercise number 13 which says:

Write a function to uniformly shrink or enlarge an image. Your function should take an image along with a scaling factor. To shrink the image the scale factor should be between 0 and 1 to enlarge the image the scaling factor should be greater than 1.

This is not meant as a question about PIL, but to ask which algorithm to use so I can code it myself.

I've found some similar questions like this, but I dunno how to translate this into python.

Any help would be appreciated.

I've come to this:

import image

win = image.ImageWin()
img = image.Image("cy.png")

factor = 2

W = img.getWidth()
H = img.getHeight()

newW = int(W*factor)
newH = int(H*factor)

newImage = image.EmptyImage(newW, newH)

for col in range(newW):
    for row in range(newH):
        p = img.getPixel(col,row)
        newImage.setPixel(col*factor,row*factor,p)

newImage.draw(win)

win.exitonclick()

I should do this in a function, but this doesn't matter right now. Arguments for function would be (image, factor). You can try it on OP tutorial in ActiveCode. It makes a stretched image with empty columns :.

© Stack Overflow or respective owner

Related posts about python

Related posts about algorithm