試圖模糊Jython中的圖片。我有的運行,但不會返回模糊的圖片。我有點不知所措。模糊圖片(Python,Jython,圖片編輯)
最終(工作)代碼編輯在下面。感謝幫助的人!
DEF主():
pic= makePicture(pickAFile())
show(pic)
blurAmount=10
makeBlurredPicture(pic,blurAmount)
show(makeBlurredPicture(pic,blurAmount))
DEF makeBlurredPicture(PIC,blurAmount):
w=getWidth(pic)
h=getHeight(pic)
blurPic= makeEmptyPicture(w-blurAmount, h)
for px in getPixels(blurPic):
x=getX(px)
y=getY(px)
if (x+blurAmount<w):
rTotal=0
gTotal=0
bTotal=0
for i in range(0,blurAmount):
origpx=getPixel(pic,x+i,y)
rTotal=rTotal+getRed(origpx)
gTotal=gTotal+getGreen(origpx)
bTotal=bTotal+getBlue(origpx)
rAverage=(rTotal/blurAmount)
gAverage=(gTotal/blurAmount)
bAverage=(bTotal/blurAmount)
setRed(px,rAverage)
setGreen(px,gAverage)
setBlue(px,bAverage)
return blurPic
的僞代碼是這樣:makeBlurredPicture(圖片,blur_amount) GET寬度和圖片的高度並製作一個尺寸爲 (w-blur_amount,h)的空圖片稱爲blurPic
for loop, looping through all the pixels (in blurPic)
get and save x and y locations of the pixel
#make sure you are not too close to edge (x+blur) is less than width
Intialize rTotal, gTotal, and bTotal to 0
# add up the rgb values for all the pixels in the blur
For loop that loops (blur_amount) times
rTotal= rTotal +the red pixel amount of the picture (input argument) at the location (x+loop number,y) then same for green and blue
find the average of red,green, blue values, this is just rTotal/blur_amount (same for green, and blue)
set the red value of blurPic pixel to the redAverage (same for green and blue)
return blurPic
可能是因爲你調用秀()在原始圖片上,而不是模糊的? –
我想返回會顯示它。 :/如何正確顯示它?我嘗試在main()函數的末尾放置show(blurPic),但這不起作用。 – roger34
只是猜測:我懷疑你的部門:'rTotal/blurAmount'。既是rTotal又是blurAmount整數?如果是這樣,你可能需要一個截斷除法(整數結果),當你可能想要一個真正的除法,與浮點結果。編輯:不,廢話。整數除法在這裏看起來很好。 –