【问题标题】:Blurring a picture (Python, Jython, picture editing)模糊图片(Python、Jython、图片编辑)
【发布时间】:2011-01-18 12:46:36
【问题描述】:

试图在 Jython 中模糊图片。我所拥有的确实运行但不会返回模糊的图片。我有点不知道它有什么问题。

在下面编辑的最终(工作)代码。感谢大家的帮助!

def main():

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(picture, blur_amount) 获取图片的宽度和高度并制作具有尺寸的空图片 (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

【问题讨论】:

  • 可能是因为您在原始图片上调用 show() 而不是模糊的图片?
  • 我认为 return 会显示它。 :/ 如何正确显示?我尝试将 show(blurPic) 放在 main() 函数的末尾,但这不起作用。
  • 只是猜测:我怀疑你的部门:rTotal/blurAmount。 rTotal 和 blurAmount 都是整数吗?如果是这样,当你可能想要一个真正的除法时,你正在做一个截断除法(整数结果),浮点结果。编辑:不,废弃它。整数除法在这里看起来不错。
  • 我觉得它只是遗漏了一些非常基本的东西,比如错误循环下的错误内容或遗漏了一行。
  • 在最里面的循环中,将 px 重命名为其他名称。我认为您正在从外部循环覆盖 px。

标签: python image-manipulation jython


【解决方案1】:

问题是您正在覆盖外部循环中的变量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) )

【讨论】:

  • 非常感谢!就是这样。在主帖中编辑了正确的代码。赞成、检查等。
【解决方案2】:

这是一个简单的方法:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

有关图像库的完整参考,请参阅:http://www.riisen.dk/dop/pil.html

【讨论】:

  • 我希望它会那么容易,但我是一名学生,教授希望它长期完成。
【解决方案3】:
def blur_image(image, radius):
    blur = image.filter(ImageFilter.GaussianBlur(radius))
    image.paste(blur,(0,0))
    return image

【讨论】:

  • 欢迎来到 StackOverflow!虽然答案总是很受欢迎,但这个问题是 6 年前提出的,并且已经有了一个公认的解决方案。请尝试通过提供答案来避免将问题“颠倒”到顶部,除非该问题尚未标记为已解决,或者您找到了解决该问题的更好的替代方法:)
猜你喜欢
  • 2016-09-10
  • 2018-10-01
  • 1970-01-01
  • 2021-07-20
  • 2014-01-03
  • 2022-06-15
  • 2013-01-06
  • 1970-01-01
  • 2022-12-14
相关资源
最近更新 更多