如何启动这个python图像过滤器?

问题描述

我是这里的蟒蛇菜鸟。我正在为我的班级做一个项目,我们必须在其中制作一个图像过滤器。我能够让当前存在的两个过滤器(像素化和添加/删除颜色)分别工作,但是当一起使用时,它们只显示在更改颜色之前是否完成了像素化。有没有办法让它们最终都显示出来,无论它们的调用顺序如何?我的代码如下:

#####################################
#    Your Filters with User Input   #
#####################################

def filter2():
  print("Code for filter2")

  addr = int(input("How much red would you like to add to the image (between -255 and 255)? "))
  addg = int(input("How much green would you like to add to the image (between -255 and 255)? "))
  addb = int(input("How much blue would you like to add to the image (between -255 and 255)? "))

  pixels = img.getdata()
 
  new_pixels = []
  
  for p in pixels:
    new_pixels.append(p)
  
  location = 0
  
  while location < len(new_pixels):
   
    p = new_pixels[location]
      
    r = p[0]
    g = p[1]
    b = p[2]
      
    newr = r + addr
    newg = g + addg
    newb = b + addb
      
    new_pixels[location] = (newr,newg,newb)
      
    location = location + 1
 
  newImage = Image.new("RGB",img.size)
  
  newImage.putdata(new_pixels)

  return newImage

def filter3():
  print("Code for filter3")
  img = Image.open('image.jpg')
  setting = input("How much pixelization would you like (low,medium,or high)? ")

  if (setting == "low"):
    imgSmall = img.resize((128,128),resample=Image.BILINEAR)
    newImage = imgSmall.resize(img.size,Image.NEAREST)

  elif (setting == "medium"):
    imgSmall = img.resize((64,64),Image.NEAREST)

  elif (setting == "high"):
    imgSmall = img.resize((32,32),Image.NEAREST)

  else:
    print("Invalid setting")

  return newImage

# Creates the four filter images and saves them to our files
a = grey()
a.save("grey.jpg")
b = filter1()
b.save("filter1.jpg")
c = filter2()
c.save("filter2.jpg")
d = filter3()
d.save("filter3.jpg")


# Create the combined filter image and saves it to our files

img.save("combinedFilters.jpg")

我已经为此工作了很长时间,如果能帮助解决我的问题,我们将不胜感激。

编辑:这是我能够将代码减少到最小的,同时保持对它应该做什么的想法。

解决方法

您似乎对如何创建和使用带参数的函数的基本知识有疑问。您应该可以在任何教程中找到它。

用参数定义函数 - 即 image

def filter1(image):

并在函数内部使用这个变量

def filter1(image):
    
    # ... work with variable `image` ...
    
    return image

然后你就可以运行

img = Image.open('image.jpg')

img_a = filter1(img)

如果您对其他功能也一样,那么您可以将图像从一个功能发送到另一个功能。

img = Image.open('image.jpg')

img_a = grey(img)

img_b = filter1(img_a)

img_c = filter2(img_b)

img_d = filter3(img_c)

img_d.save('combinedFilters.jpg')

def grey(image):

    # ... work with variable `image` ...

    return image.

def filter1(image):
    
    # ... work with variable `image` ...
    
    return image


def filter2(image):
    
    # ... work with variable `image` ...
    
    return image


def filter2(image):
    
    # ... work with variable `image` ...
    
    return image

# --- main ---

img = Image.open('image.jpg')

img_a = grey(img)
#img_a.save("grey.jpg")

img_b = filter1(img_a)
#img_b.save("filter1.jpg")

img_c = filter2(img_b)
#img_c.save("filter2.jpg")

img_d = filter3(img_c)
img_d.save("filter3.jpg")

或者你可以写得更短

img = Image.open('image.jpg')

img_d = filter3(filter2(filter1(grey(img))))

img_d.save("filter3.jpg")

顺便说一句:如果您以不同的顺序运行过滤器,那么您可能会得到相同或不同的结果 - 一切都取决于过滤器的工作方式。

,

我让它按预期工作,我只需要从“filter3”函数中删除“img = Image.open('image.jpg')”行。