我正在尝试编写执行图像处理的类对象

时间:2019-05-26 23:53:10

标签: python python-3.x image python-imaging-library

在进入最后一行代码之前,我相信大多数代码都是正确的。我第一次写类方法。不确定如何指定图像位置。

不确定如何进行

from PIL import Image

class image_play(object):
    def __init__(self, im_name):
        self.im_name = im_name


    def rgb_to_gray_image(self):
        im = Image.open(self.im_name)
        im = im.convert('LA')
        return im

    # editing pixels of image to white
    def loop_over_image(self):
        im = Image.open(self.im_name)
        width, height = im.size
        # nested loop over all pixels of image
        temp = []
        for i in range(width):
            for j in range(height):
                temp.append((255,255,255))#append a tuple for the RGB channel values for each pixel

        image_out = Image.new(im.mode,im.size) #create a new image usig PIl
        image_out.putdata(temp) #use the temp list to create the image
        return image_out

obj = image_play()    
TypeError                                 Traceback (most recent call last)
<ipython-input-8-d6175b134ccc> in <module>
     25         return image_out
     26 
---> 27 obj = image_play()

TypeError: __init__() missing 1 required positional argument: 'im_name'

2 个答案:

答案 0 :(得分:1)

实例化类时,您必须传递图像的名称。在您的__init__中,您说需要输入im_name

所以你会写类似

obj = image_play("./image.png")

答案 1 :(得分:0)

我添加了以下代码,现在一切正常。

pic = image_play('test.png')
picGray = pic.rgb_to_gray_image()
picGray.show()
picWhite = pic.loop_over_image()
picWhite.show()