2013-06-22 43 views
1

我試圖實現一個程序,該程序會將圖像的寬度增加一個像素。然後,我想要採用新的最大x座標,並將其與隨機的y座標(即在圖像的範圍內)進行比較,以創建一個新的像素。將圖像寬度增加一個像素

for x in range (0,getWidth(pic)): 
    for y in range (0,getHeight(pic)): 
     X=getWidth(pic) 
     newX = (X+1) 
     colr=(255,0,0) 
     newPixel = getPixel (pic, newX, y)//line 25 
     setColor(newPixel, colr) 
     Y=getHeight(pic) 
     newY= (Y+1) 
     newPixel = getPixel(pic,x, newY) 
     setColor(newPixel, colr) 

我得到這個錯誤:

getPixel(picture,x,y): x (= 226) is less than 0 or bigger than the width (= 224) 
The error was: 
Inappropriate argument value (of correct type). 
An error occurred attempting to pass an argument to a function. 
Please check line 25 of D:\bla bla 

我的理解是超出範圍。我究竟做錯了什麼?

回答

3

這裏是廣義的方法來提高圖像保持其當前內容的大小:

隨意調整。

# Increase a picture given an offset, a color and the anciant 
# content must be centered or not. 
# Offsets must be positive. 
def increaseAndCopy(pic, offsetX, offsetY, bg_color=black, center=True): 

    # Offsets must be positive 
    if (offsetX < 0.0) or (offsetY < 0.0): 
    printNow("Error: Offsets must be positive !") 
    return None 

    new_w = pic.getWidth() + int(2*offsetX) 
    new_h = pic.getHeight() + int(2*offsetY) 

    startX = 0 
    startY = 0 
    if (center) and (offsetX > 1.0): 
    startX = int(offsetX) 
    if (center) and (offsetY > 1.0): 
    startY = int(offsetY) 

    new_pic = makeEmptyPicture(new_w, new_h) 
    # Fill with background color 
    setAllPixelsToAColor(new_pic, bg_color) 

    # Process copy 
    for x in xrange(pic.getWidth()): 
    for y in xrange(pic.getHeight()): 
     px = getPixel(pic, x, y) 
     new_px = getPixel(new_pic, x + startX, y + startY) 
     setColor(new_px, getColor(px)) 

    return new_pic 

file = pickAFile() 
picture = makePicture(file) 

# Pass an offset of 0.5 to increase by 1 pixel 
#new_picture = increaseAndCopy(picture, 0.5, 0, blue) 
new_picture = increaseAndCopy(picture, 10, 20, gray, True) 

if (new_picture): 
    writePictureTo(new_picture, "/home/biggerPic.png") 
    show(new_picture) 


輸出(繪畫由讓 - 米歇爾·巴斯奎特):


................ enter image description here ... ........................ enter image description here ................


+1

是啊!你見過那個! ;)但是我也要感謝你,因爲在遇到你之前我對JES一無所知,而且我喜歡玩這個遊戲。我會在一天結束時給你寫一封電子郵件...... –

+1

修復:'如果(中心)和(偏移X> 1.0)'應該是'如果(中心)和(偏移X> = 1.0)'。 'offsetY'一樣。 –

3

你怎麼能得到一個物體沒有的東西?

newPixel = getPixel (pic, newX, y)//line 25 

原始圖像保持在尺寸的getWidth(PIC),但你所要求的在getWidth(pic) + 1像素不存在。

可以通過將圖片複製到類似to this answer的新圖片來放大圖片。

... 
newPic=makeEmptyPicture(newX,newY) 
xstart=0 
ystart=0 

for y in range(ystart,newY): 

    for x in range(xstart, newX): 

    if x == newX or y == newY: 
     colour=(255,0,0) 
    else: 
     oldPixel=getPixel(oldPic,x,y) 
     colour=getColor(oldPixel) 

    newPixel=getPixel(newPic,x,y) 
    setColor(newPixel,colour) 


    explore(newPic) 
+0

它複製它像素每像素到新圖像。然後將新圖像中最後一行或一列像素設置爲所需的顏色。我不確定這是最有效的答案,我只是找不到確切的複製方法,但我相信這應該起作用。 – ingyhere

+0

@ingyhere您提供的代碼不起作用。 '如果x = newX或者y = newY'應該是'if x == newX or y == newY'。 –

+0

@Golgauth你的意思是代碼中有錯誤,對吧?!我修好了錯別字。這是一個答案,而不是生產就緒代碼。 – ingyhere