I want to scale up a part of a picture, in this example, a nose.
I have a function to select the part of the picture I want to enlarge.
def copyAndPa
This is just for the record and for fun, not an answer...
But as mentioned abarnert ("Are you sure they just want you to leave 3 white pixels for every copied pixel, rather than copying the same pixel 4 times?"), which is quite ridiculous as a scaling algorithm...
A lot more interesting yet basic approach to scale an image is the Nearest Neighbor Algorithm.
def EnlargeNearestNeighbor(picture, multiplier):
w1 = getWidth(picture)
h1 = getHeight(picture)
w2 = getWidth(picture) * multiplier
h2 = getHeight(picture) * multiplier
x_ratio = w1/float(w2)
y_ratio = h1/float(h2)
newPicture = makeEmptyPicture(w2, h2)
for x in range(0, w2):
for y in range(0, h2):
newPx = getPixel(newPicture, x, y)
px = floor(x*x_ratio);
py = floor(y*y_ratio);
oldPx = getPixel(picture, int(px), int(py))
setColor(newPx, getColor(oldPx))
return newPicture
file = pickAFile()
picture = makePicture(file)
pic = EnlargeEagle(picture)
pic2 = EnlargeNearestNeighbor(picture, 3)
........................................
..........................................
...........................................
Other interesting algorithms here.
Here is a basic implementation of the Eagle Algorithm (works fine with images with a low number of different colors):
def EnlargeEagle(picture):
w = getWidth(picture)
h = getHeight(picture)
w2 = getWidth(picture)*2
h2 = getHeight(picture)*2
newPicture = makeEmptyPicture(w2, h2)
x2 = 0
for x in range(1, w-1):
y2 = 0
for y in range(1, h-1):
oldPxS = getPixel(picture, x-1, y-1)
oldPxT = getPixel(picture, x, y-1)
oldPxU = getPixel(picture, x+1, y-1)
oldPxV = getPixel(picture, x-1, y)
oldPxC = getPixel(picture, x, y)
oldPxW = getPixel(picture, x+1, y)
oldPxX = getPixel(picture, x-1, y+1)
oldPxY = getPixel(picture, x, y+1)
oldPxZ = getPixel(picture, x+1, y+1)
newPx1 = getPixel(newPicture, x2, y2)
newPx2 = getPixel(newPicture, x2+1, y2)
newPx3 = getPixel(newPicture, x2, y2+1)
newPx4 = getPixel(newPicture, x2+1, y2+1)
# Step 1
c = getColor(oldPxC)
setColor(newPx1, c)
setColor(newPx2, c)
setColor(newPx3, c)
setColor(newPx4, c)
# Step 2
if (getColor(oldPxV) == getColor(oldPxS)) and (getColor(oldPxS) == getColor(oldPxT)):
setColor(newPx1, getColor(oldPxS))
if (getColor(oldPxT) == getColor(oldPxU)) and (getColor(oldPxU) == getColor(oldPxW)):
setColor(newPx2, getColor(oldPxU))
if (getColor(oldPxV) == getColor(oldPxX)) and (getColor(oldPxX) == getColor(oldPxY)):
setColor(newPx3, getColor(oldPxX))
if (getColor(oldPxW) == getColor(oldPxZ)) and (getColor(oldPxZ) == getColor(oldPxY)):
setColor(newPx4, getColor(oldPxZ))
y2 += 2
x2 += 2
Original:

Nearest Neighbor:

Eagle:

Enjoy!