0

I believe I have most of the code correct until I get to last line of code. My first time writing a class method. Not sure how specify image location.

Not sure how to proceed

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'
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    change `image_play()` to `image_play("foo.png")` – eyllanesc May 26 '19 at 23:55
  • The class init method has a required argument `im_name`, and you're trying to make a class object without passing any argument. – John Gordon May 27 '19 at 00:01
  • Now I receive the following traceback: Traceback (most recent call last): File "C:/Users/Kelly/.PyCharmCE2019.1/config/scratches/scratch_1.py", line 1, in from PIL import Image File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 94, in from . import _imaging as core ImportError: DLL load failed: The specified module could not be found. Process finished with exit code 1 –  May 27 '19 at 01:08
  • from your traceback it seems like you have anaconda3 as well as pycharm installed on your OS, so this might create incompatibility issues between modules, so make sure `pillow` is installed in pycharm by checking it on project interpreter. If that doesn't work for you, [This](https://stackoverflow.com/questions/43264773/pil-dll-load-failed-specified-procedure-could-not-be-found) or [This](https://stackoverflow.com/questions/20201868/importerror-dll-load-failed-the-specified-module-could-not-be-found) might help ^_^ – Vasu Deo.S May 27 '19 at 04:38

2 Answers2

1

You have to pass the name of the image when you instantiate the class. In your __init__ you say it takes an input im_name

So you would write something like

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

Raf
  • 26
  • 1
0

I added the following code and now all is well.

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