模糊图片(Python、Jython、图片编辑)

发布于 2024-08-22 11:26:13 字数 1839 浏览 2 评论 0原文

尝试在 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

Trying to blur a picture in Jython. What I have does run but does not return a blurred picture. I'm kinda at a loss of what is wrong with it.

FINAL (WORKING) CODE EDITED IN BELOW. THANKS FOR HELP GUYS!

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

The pseudo-code was as such : makeBlurredPicture(picture, blur_amount)
get width and height of picture and make an empty picture with the dimensions
(w-blur_amount, h ) call this 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱她像谁 2024-08-29 11:26:13

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

The problem is that you are overwriting the variable px from the outer loop which is the pixel in the blurred image with a pixel value from the original image.
So just replace your inner loop with:

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

In order to show the blurred picture change the last line in you main to

show( makeBlurredPicture(pic,blurAmount) )
絕版丫頭 2024-08-29 11:26:13

以下是执行此操作的简单方法:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

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

Here is the simple way to do it:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

For a complete reference to the Image Library See: http://www.riisen.dk/dop/pil.html

如梦初醒的夏天 2024-08-29 11:26:13
def blur_image(image, radius):
    blur = image.filter(ImageFilter.GaussianBlur(radius))
    image.paste(blur,(0,0))
    return image
def blur_image(image, radius):
    blur = image.filter(ImageFilter.GaussianBlur(radius))
    image.paste(blur,(0,0))
    return image
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文