-3

Can't figure out how to have everything compile and return my image.

from PIL import Image

def open_bird():

    filename = 'bird_scrambled.png'             #Call File 
    picture = Image.open(filename)              #Opens file for use 

    return(picture)

def unscramble_left(picture):

    width, height = picture.size                #Call image size, log

    for x in range (0, (width/2)):              # width is /2 
        for y in range (0, height):             # height remains same
            red, green, blue = picture.getpixel((x,y)) # call pixels in x,y grid
            picture.putpixel((x,y), (blue,green,red))  # replace pixels rgb -> bgr

    return(picture)

def unscramble_top(picture):

    width, height = picture.size

    for x in range (0, (width/2)):
        for y in range (0, (height/2)):
            red, green, blue = picture.getpixel((x,y))
            for red in picture:
                if red >= 127:
                    red = red - 127
                else:
                    red = red + 127 
    picture.show()

def put_together():
    open_bird()
    unscramble_left(picture)
    unscramble_top(picture)

So basically, I want to return the picture from each function after it's be set in the initial function. Passing the photo through unscramble_left(), to unscramble_top; and finally having it compile in a final function of put_together(). Every time I try running this though, I end up with the issue of the return value Picture not coming through in the final put_together function. I'm doing the final function as it all needs to be called from one function. So that's my issue. It's just not returning.

3
  • 1
    Hi Joel, can you please provide a description of your problem? Can you explain the problem, what are you struggling with and in general be a bit more specific so we can help? You can edit your question and improve it.
    – Rafael
    Nov 27, 2018 at 22:26
  • don't forget to actually store the values you return so you can use them. in put_together you do not store the return value of open_bird and then pass a newly instantiated picture variable, which will be None, to the 2 subsequent functions. so picture = open_bird() Nov 27, 2018 at 22:30
  • Can you explain your problem a bit better, Joe? =) Additional details may help readers understand what is the aim of the question. Happy coding! Nov 28, 2018 at 0:13

1 Answer 1

0

I am not entirely sure what you are trying to accomplish, but the code at least compiles now. I ended up editing a few things:

  • It seems like your put together function was an attempt to be the function which gets run, when the program starts. Therefore it turned it into main.
  • I it seemed like you were trying to pass around the picture, so I changed it to do actually do so.
  • In addition, I edited the range of several for loops. Given that the range must be from an integer to another integer, I added rounding. Currently it uses ceil() to round the value up, but you may want to use floor() to round the value down
  • It seemed like you were trying to edit each pixel in unscramble_top. So I edited it to do so. Originally, you had it so that it was trying to iterate though the pixel, which will not work because each individual pizel has only one value for r, one for g, and one for b.

Code

from PIL import Image
from math import ceil


def open_bird():
    filename = 'Image.jpeg'  # Call File
    picture = Image.open(filename)  # Opens file for use

    return picture


def unscramble_left(picture):
    width, height = picture.size  # Call image size, log

    for x in range(0, ceil(width / 2)):  # width is /2
        for y in range(0, height):  # height remains same
            red, green, blue = picture.getpixel((x, y))  # call pixels in x,y grid
            picture.putpixel((x, y), (blue, green, red))  # replace pixels rgb -> bgr

    return picture


def unscramble_top(picture):
    width, height = picture.size

    for x in range(0, ceil(width / 2)):
        for y in range(0, ceil(height / 2)):
            red, green, blue = picture.getpixel((x, y))
            if red >= 127:
                red = red - 127
            else:
                red = red + 127

    return picture


def main():
    picture = open_bird()
    picture = unscramble_left(picture)
    picture = unscramble_top(picture)
    picture.show()


if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.