Results 1 to 6 of 6
  1. #1
    King of the Jews
    Join Date
    Jul 2007
    Posts
    4,701
    BG Level
    7
    FFXI Server
    Ifrit

    Python 3 and inverting colours

    Hey guys, I was wondering if anyone could give me a hint on how to fucking set up a loop that loops over all the pixels in an image and inverts them.
    http://pastebin.com/Ydfkr7nx

    That's my code, which asks you for the name of a file, and saves it to a file as what ever you calll it with .gif
    also the hashtag before my 2nd for loop is there cause i wanna make sure im doing the x's correct before i do the y's

    For this project, you create an Image object by passing the Image constructor two things:

    An "anchor point" where the image should be centered
    The name of the file containing the image
    Since you are not going to be drawing the image on the screen, the anchor point is not important. You can just create a random Point (e.g. Point(0,0) and pass it to the constructor.

    Sample usage:

    img = Image(Point(0,0), 'somefile.gif')
    Getting the dimensions of an image

    If you have an Image object, you can call the getWidth or getHeight methods to return its width or height.

    Sample usage (using our object from the previous example):

    area = img.getWidth() * img.getHeight()
    Reading the color values of a pixel

    To get the color values of a pixel, you can call getPixel. You will need to pass it the x and y coordinates of the pixel you are interested in. The top left pixel is at (0,0).

    GetPixel will return a list of 3 numbers, all between 0 and 255. The first number is the red value, the second is the green value, and the third is the blue value.

    Here is some sample code that prints the color value of the top left pixel in our image:

    # Indices of the different color values
    RED = 0
    GREEN = 1
    BLUE = 2

    colors = img.getPixel(0,0)
    print('Red =', colors[RED])
    print('Green =', colors[GREEN])
    print('Blue =', colors[BLUE])
    Setting the color values of a pixel

    To set the color values of a pixel, you can call setPixel. You will need to pass it the x and y coordinates of the pixel as well as a special string with the color information. You can create this string by first calling a function called color_rgb and passing it the desired red, green, and blue values.

    Here is an example. In this example, we set the top left pixel in our image to blue:

    colorstring = color_rgb(0, 0, 255) # blue
    img.setPixel(0, 0, colorstring)
    Thats the Image class that my teacher wants me to use. I'm just stumped on how to fucking get the loop set up and change the colors. Any help would be fantastic and thanks in advance if somebody can help me, otherwise thanks for trying!

  2. #2
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,731
    BG Level
    8
    FFXI Server
    Carbuncle

    Do you seriously not know how to do a fucking loop? (Seriously, that part you should google yourself, although you probably should have learned it in class) I don't personally use python (ruby is superior), but from my own googling, that looping syntax looks fine.

    Uh, I really don't know how much I should "help" and how much I should do this for you, so I'll just give you some hints based on what I see wrong:

    1. The stuff you're doing inside of your loop will need both an X and a Y value.
    2. colours = x.getPixel() #This line is incredibly wrong, from your documentation:
    To get the color values of a pixel, you can call getPixel. You will need to pass it the x and y coordinates of the pixel you are interested in. The top left pixel is at (0,0).
    GetPixel will return a list of 3 numbers, all between 0 and 255. The first number is the red value, the second is the green value, and the third is the blue value.
    colors = img.getPixel(0,0)
    3. colorstring looks fine
    4. The setPixel, however, does not. Read your own documentation thing you have to follow:
    "To set the color values of a pixel, you can call setPixel. You will need to pass it the x and y coordinates of the pixel as well as a special string with the color information. You can create this string by first calling a function called color_rgb and passing it the desired red, green, and blue values."
    The first and second parameters are the X, Y coordinates, and the 3rd one is the string that contains rgb info.

    Everything else in the inverted_color function looks fine. Why not test your code by actually running it?

    edit: In your main code, shouldn't you be saving the cloned image, since that's what you're modifying?
    edit2: God I feel dirty now after doing all of that python stuff

  3. #3
    Ridill
    Join Date
    May 2005
    Posts
    13,568
    BG Level
    9

    Fuck ruby.

    Python is awesome.

  4. #4
    King of the Jews
    Join Date
    Jul 2007
    Posts
    4,701
    BG Level
    7
    FFXI Server
    Ifrit

    Spoiler: show
    Quote Originally Posted by Julian View Post
    Do you seriously not know how to do a fucking loop? (Seriously, that part you should google yourself, although you probably should have learned it in class) I don't personally use python (ruby is superior), but from my own googling, that looping syntax looks fine.

    Uh, I really don't know how much I should "help" and how much I should do this for you, so I'll just give you some hints based on what I see wrong:

    1. The stuff you're doing inside of your loop will need both an X and a Y value.
    2. colours = x.getPixel() #This line is incredibly wrong, from your documentation:
    To get the color values of a pixel, you can call getPixel. You will need to pass it the x and y coordinates of the pixel you are interested in. The top left pixel is at (0,0).
    GetPixel will return a list of 3 numbers, all between 0 and 255. The first number is the red value, the second is the green value, and the third is the blue value.
    colors = img.getPixel(0,0)
    3. colorstring looks fine
    4. The setPixel, however, does not. Read your own documentation thing you have to follow:
    "To set the color values of a pixel, you can call setPixel. You will need to pass it the x and y coordinates of the pixel as well as a special string with the color information. You can create this string by first calling a function called color_rgb and passing it the desired red, green, and blue values."
    The first and second parameters are the X, Y coordinates, and the 3rd one is the string that contains rgb info.

    Everything else in the inverted_color function looks fine. Why not test your code by actually running it?

    edit: In your main code, shouldn't you be saving the cloned image, since that's what you're modifying?
    edit2: God I feel dirty now after doing all of that python stuff

    I do know how to do a loop, that was never the problem Julian. The problem came from using the Image class that the teacher gave us. I already had a loop within a loop for x and y. I never asked somebody to "do it for me" because then I wouldn't learn or understand. I really appreciate the help, and feel stupid as shit asking, but I was stuck on this for a long long time.

  5. #5
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,731
    BG Level
    8
    FFXI Server
    Carbuncle

    I didn't mean to come off as rude or anything, I had just woken up <_< but the syntax with the range(#) should be fine as a looping construct, and the X and Y represent the value it's at. I meant that if you didn't know how to set that part up, you really should google and find out yourself, because it's not difficult information to find.

    I think I answered most of the stuff regarding the image class, I think. If something still doesn't work then just repost updated code and say what the problem is, and I (or others) can try to help more.

  6. #6
    King of the Jews
    Join Date
    Jul 2007
    Posts
    4,701
    BG Level
    7
    FFXI Server
    Ifrit

    I got it working, mod can close this now, thanks for all the help

Similar Threads

  1. /wave Peng and Ondori
    By layoneil in forum General Discussion
    Replies: 3
    Last Post: 2004-08-19, 17:09
  2. A question to Yummy and Clistophy!!
    By Hirronimus in forum General Discussion
    Replies: 2
    Last Post: 2004-08-11, 00:14