2010-02-18 72 views
0

试图模糊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 
+0

可能是因为你调用秀()在原始图片上,而不是模糊的? –

+0

我想返回会显示它。 :/如何正确显示它?我尝试在main()函数的末尾放置show(blurPic),但这不起作用。 – roger34

+0

只是猜测:我怀疑你的部门:'rTotal/blurAmount'。既是rTotal又是blurAmount整数?如果是这样,你可能需要一个截断除法(整数结果),当你可能想要一个真正的除法,与浮点结果。编辑:不,废话。整数除法在这里看起来很好。 –

回答

3

的问题是,你是从外循环覆盖变量px这是模糊图像中具有来自原始图像的像素值的像素。
所以只是代替你的内部循环:

for i in range(0,blurAmount): 
    origPx=getPixel(pic,x+i,y) 
    rTotal=rTotal+getRed(origPx) 
    gTotal=gTotal+getGreen(origPx) 
    bTotal=bTotal+getBlue(origPx) 

为了显示模糊画面更改的最后一行在你main

show(makeBlurredPicture(pic,blurAmount)) 
+0

非常感谢!就是这样。在主帖子中修改了正确的代码。 Upvoted,Checked等 – roger34

1

下面是简单的方法来做到这一点:

import ImageFilter 

def filterBlur(im): 

    im1 = im.filter(ImageFilter.BLUR) 

    im1.save("BLUR" + ext) 

filterBlur(im1) 

对于一个完整的参考图片库见:http://www.riisen.dk/dop/pil.html

+0

我希望这会很容易,但我是一名学生,教授希望它能够长时间完成。 – roger34

0
def blur_image(image, radius): 
    blur = image.filter(ImageFilter.GaussianBlur(radius)) 
    image.paste(blur,(0,0)) 
    return image 
+1

欢迎来到StackOverflow!答案总是值得赞赏的,但这个问题在6年前就已经提出,并且已经有了一个可以接受的解决方案请尽量避免通过向他们提供答案来'碰撞'问题,除非问题还没有被标记为已解决,或者您找到了一个更好的替代方法来解决问题:) –