对于编程很新,并且在没有使用PIL等的情况下无法找到任何方法,如果我错了,请指出我正确的方向!
我需要修改以下代码,以便将图像以缩放输入选择的大小复制到TGT。在这个阶段,我已经得到它,以便将图片复制到画布上,画布的大小由缩放输入决定,但无法锻炼如何使图像本身缩放。
非常感谢任何帮助。
def driver():
pic = makePicture(pickAFile())
repaint(pic)
modifiedPic = scale(pic,2.0)
repaint(modifiedPic)
def scale(srcPic,scale):
xStart = 0
yStart = 0
xEnd = getWidth(srcPic)
yEnd = getHeight(srcPic)
width = getWidth(srcPic)
height = getHeight(srcPic)
tgt = makeEmptyPicture(width*int(scale),height*int(scale))
for x in range(0,width):
for y in range(0,height):
pxSrc=getPixel(srcPic,x,y)
pxTgt=getPixel(tgt,x,y)
setColor(pxTgt,getColor(pxSrc))
srcWidth = xEnd -xStart
srcHeight = yEnd - yStart
tgtWidth = int(srcWidth*scale)
tgtHeight = int(srcHeight*scale)
for y in range(0,tgtHeight):
for x in range(0,tgtWidth):
pxSrc=getPixel(srcPic,int(x/scale)+xStart,int(y/scale)+yStart)
pxTgt=getPixel(tgt,int(xStart*scale),int(yStart*scale))
setColor(pxTgt,getColor(pxSrc))
return tgt
答案 0 :(得分:0)
试试这个
def scale(srcPic,scale):
width = getWidth(srcPic)
height = getHeight(srcPic)
tgt = makeEmptyPicture(width*int(scale),height*int(scale))
# get the pixel from original source
for x in range(0, width):
for y in range(0,height):
pxSrc=getPixel(srcPic,x,y)
#copy source pixel by scale amount along x and y axis
for i in range(x * int(scale), x * int(scale) + int(scale)):
for j in range(y * int(scale), y * int(scale) + int(scale)):
pxTgt=getPixel(tgt,i,j)
setColor(pxTgt,getColor(pxSrc))
return txt