Размытие картинки (Python, Jython, редактирование картинки) - PullRequest
0 голосов
/ 19 февраля 2010

Попытка размыть картинку в Jython. То, что у меня есть, работает, но не возвращает размытое изображение. Я вроде в недоумении, что с ним не так.

ЗАКЛЮЧИТЕЛЬНЫЙ (РАБОЧИЙ) КОД, ИЗМЕНЕННЫЙ НИЖЕ. СПАСИБО ЗА ПОМОЩЬ ПАРНИМ!

def main ():

pic= makePicture( pickAFile() )
show( pic )
blurAmount=10
makeBlurredPicture(pic,blurAmount)
show(makeBlurredPicture(pic,blurAmount))

def makeBlotedPicture (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

Ответы [ 3 ]

3 голосов
/ 19 февраля 2010

Проблема в том, что вы перезаписываете переменную 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) )
1 голос
/ 19 февраля 2010

Вот простой способ сделать это:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

Для полной ссылки на библиотеку изображений см .: http://www.riisen.dk/dop/pil.html

0 голосов
/ 07 февраля 2017
def blur_image(image, radius):
    blur = image.filter(ImageFilter.GaussianBlur(radius))
    image.paste(blur,(0,0))
    return image
...