[ActiveCode] Image scaling algorithm

This is the place for queries that don't fit in any of the other categories.

[ActiveCode] Image scaling algorithm

Postby reggaelizard » Mon Jul 29, 2013 3:39 pm

I'm doing the exercises in this page and I'm having some trouble with number 11, which asks for a function that takes an image and a scaling factor and uniformly shrinks it if the factor is less than 1 and enlarges it if it's greater than 1. This is my entire code:

Code: Select all
import image

img = image.Image('goldygopher.png')
wn = image.ImageWin()

factor = 2

newimg = image.EmptyImage(img.getWidth()*factor,img.getHeight()*factor)

def scale(im,newim,fact):
    for col in range(im.getWidth()):
        for row in range(im.getHeight()):
            p = im.getPixel(col,row)
           
            for i in range(factor):
                newim.setPixel(col*factor,row*factor,p)

scale(img,newimg,factor)


It's clearly doing something, since my cursor indicates that it's loading for two or three seconds after I click 'run', but nothing shows up on the image window. Any help would be appreciated.
Last edited by Yoriz on Mon Jul 29, 2013 6:45 pm, edited 1 time in total.
Reason: Added to title that this is programmed in ActiveCode
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Image scaling algorithm

Postby reggaelizard » Mon Jul 29, 2013 5:38 pm

OK, never mind, I was using the wrong methods. I figured out what was wrong with them and now it's drawing something, though not exactly what I wanted it to. This is my new code:

Code: Select all
import image

goldy = image.Image('goldygopher.png')

factor = 2

steklovata = image.EmptyImage(goldy.getWidth()*factor,goldy.getHeight()*factor)

def scale(im,newim,f):
    wn = image.ImageWin()
    neww = int(im.getWidth()*f)
    newh = int(im.getHeight()*f)
   
    for col in range(neww):
        for row in range(newh):
            p = im.getPixel(col,row)
            newim.setPixel(col*f,row*f,p)
           
            newim.draw(wn)
           
scale(goldy,steklovata,factor)


Because the for loop is really only telling it to set the pixels exactly on column*factor and row*factor and not on the three pixels surrounding each of those as well, this is what I'm getting:
Image
(it's also being a right twit and telling me that it's exceeded run time limit and that I should increase the run time with the sys.setExecutionLimit(secs) method, even though it's apparently not ported into activecode, but that has nothing to do with my buggy function)

I suppose there should be more statements inside the loop so it can fill in more patterns of pixels, though I have yet to figure out which.
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], W3C [Linkcheck] and 7 guests