4 Replies - 3883 Views - Last Post: 17 October 2012 - 07:26 PM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Iterate through pixels in Python and shift High bits to the right

Posted 09 October 2012 - 06:58 PM

Hello,

I have an assignnment to write a code in python. These are the instructions:
Write a function called shiftHighBits that has two input arguments. The first one is the file name of an image file; the second one is the maxshift value. Your function will first display the original image and then do the following:

Repeat for each k in range 1 to maxshift:

1. Iterate through every pixel in the image and right shift the color bits by k bits.

2. Save the transformed image in a file having as name: filename+“shiftbits”+str(k)+“mod.gif”

So far I have this much of my code. I get the original picture and then I get an all black picture which is the picture that I am suppose to get last but I don't think I am doing the "Iterate through every pixel in the image and right shift the color bits by k bits." part correctly in my 3rd for loop. Any suggestions?

def shiftHighBits(filename,maxshift):
    myImage = Image.open(filename)
    myImage = myImage.convert('RGB')
    width, height = myImage.size
    myImage.show()

    for x in range(width):
        for y in range(height):
            for k in range(1,maxshift):
                r,b,g = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>k,g>>k,b>>k))

    newfilename = filename[:-4]
    newfilename = newfilename + "_shiftbits"+str(k)+"mod.gif"
    myImage.save(newfilename)
    myImage.show()


Is This A Good Question/Topic? 0
  • +

Replies To: Iterate through pixels in Python and shift High bits to the right

#2 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Iterate through pixels in Python and shift High bits to the right

Posted 10 October 2012 - 03:19 PM

Any Suggestions? I revised it to this: But it still doesn't output the right information

def shiftHighBits(filename,maxshift):
    myImage = Image.open(filename)
    myImage = myImage.convert('RGB')
    width, height = myImage.size
    myImage.show()

    for k in range(1,maxshift):
        for x in range(width):
            for y in range(height):
                r,b,g = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>k,g>>k,b>>k))
                    

    newfilename = filename[:-4]
    newfilename = newfilename + "_shiftbits"+str(k)+"mod.gif"
    myImage.save(newfilename)
    myImage.show()

Was This Post Helpful? 0
  • +
  • -

#3 croger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: Iterate through pixels in Python and shift High bits to the right

Posted 11 October 2012 - 04:10 PM

In putpixel change your k's to 1 and put your code for the new image in the loop for k
Was This Post Helpful? 0
  • +
  • -

#4 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Iterate through pixels in Python and shift High bits to the right

Posted 11 October 2012 - 04:50 PM

Thank you. I got this completed!
Was This Post Helpful? 0
  • +
  • -

#5 confuzzledgrl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-October 12

Re: Iterate through pixels in Python and shift High bits to the right

Posted 17 October 2012 - 07:26 PM

Hey I'm in the same class. How did you get this to work without listing them out one by one? Hope you can get back to me in like the next hour. Here is my code, if you see what I did wrong (besides that its all listed out, please tell me. Thank you.

def shifthighbits(filename,maxshift):
    myImage = Image.open(filename)
    myImage = myImage.convert('RGB')
    width, height = myImage.size
    myImage.show()
    for k in range(1,maxshift):
        for x in range(width):
            for y in range(height):
                r,g,b = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>1,g>>1,b>>1))
    myIMage.show()
    for k in range(1,maxshift):
        for x in range(width):
            for y in range(height):
                r,g,b = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>2,g>>2,b>>2))
    myImage.show()
    for k in range(1,maxshift):
        for x in range(width):
            for y in range(height):
                r,g,b = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>3,g>>3,b>>3))
    myImage.show()
    for k in range(1,maxshift):
        for x in range(width):
            for y in range(height):
                r,g,b = myImage.getpixel((x,y))
                myImage.putpixel((x,y),(r>>4,g>>4,b>>4))
    newfilename = filename[:-4]
    newfilename = newfilename + "_shiftbits"+str(k)+"mod.gif"
    myImage.save(newfilename)
    myImage.show(newfilename)
shifthighbits('orangefish.gif',5)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1