首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模糊图片(Python,Jython,图片编辑)

模糊图片(Python,Jython,图片编辑)
EN

Stack Overflow用户
提问于 2010-02-18 21:13:36
回答 3查看 10.3K关注 0票数 0

试图模糊Jython中的一张照片。我所拥有的是奔跑,但没有返回模糊的图片。我有点不知道这是怎么回事。

下面编辑的最终(工作)代码。谢谢帮忙,伙计们!

def main():

代码语言:javascript
复制
pic= makePicture( pickAFile() )
show( pic )
blurAmount=10
makeBlurredPicture(pic,blurAmount)
show(makeBlurredPicture(pic,blurAmount))

def makeBlurredPicture(pic,blurAmount):

代码语言:javascript
复制
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)获取图片的宽度和高度,并用维数(w-blur_ blurPic,h)生成一个空图片,调用这个blurPic。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-19 09:11:08

问题是,您正在覆盖外部循环中的变量px,该变量是模糊图像中的像素,其像素值来自原始图像。

因此,只需将您的内部循环替换为:

代码语言:javascript
复制
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中的最后一行更改为

代码语言:javascript
复制
show( makeBlurredPicture(pic,blurAmount) )
票数 3
EN

Stack Overflow用户

发布于 2010-02-18 21:18:30

下面是简单的方法:

代码语言:javascript
复制
import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

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

票数 1
EN

Stack Overflow用户

发布于 2017-02-06 21:09:36

代码语言:javascript
复制
def blur_image(image, radius):
    blur = image.filter(ImageFilter.GaussianBlur(radius))
    image.paste(blur,(0,0))
    return image
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2292233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档