5 Replies - 1349 Views - Last Post: 17 October 2012 - 12:29 PM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

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

Setting Low Bits to Zero in Python

Posted 07 October 2012 - 08:48 AM

I have a section in my assignment that I cant get to work.

These are the instructions:
Write a function called zeroLowBits that has as input argument the file name of an image file, then sets the lower 4 bits of each pixel (red, green, and blue components) of the image to zero. Save the new image in a file called filename+“_zerolowbits_mod.gif” Your function will display the original image first, and then modified image with low bit set to zero. Notice that the new image looks very similar to the original.


This is my code, but it doesnt give the right output. it just show the original picture many times.

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

    for x in range(width):
        for y in range(height):
            r,b,g = myImage.getpixel((x,y))
            newr = r & 0b0000
            newg = g & 0b0000
            newb = b & 0b0000
            myImage.putpixel((x,y),(newr,newg,newb))
        newfilename = filename[:-4]
        newfilename = newfilename + "_zerolowbits_mod.gif"
        myImage.save(newfilename)
        myImage.show(newfilename)


I have attached the picture. On the left is the original picture but on the right is the picture I am trying to get as the output. Any Suggestions?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Setting Low Bits to Zero in Python

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,582
  • Joined: 16-October 07

Re: Setting Low Bits to Zero in Python

Posted 07 October 2012 - 01:07 PM

First, 0b0000 is just zero. Anything & zero is zero. I'm kind of at a loss as to how your result image is anything but black. If you want to zero out the last four bits you must use a mask that preserves the rest of the bits.
Was This Post Helpful? 0
  • +
  • -

#3 kehara15  Icon User is offline

  • New D.I.C Head

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

Re: Setting Low Bits to Zero in Python

Posted 07 October 2012 - 06:10 PM

I dont understand what you mean by a mask
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,582
  • Joined: 16-October 07

Re: Setting Low Bits to Zero in Python

Posted 08 October 2012 - 11:45 AM

Sorry, since it seemed you were attempting to use a bit mask...

If you have a value expressed in bits and you apply an AND(&) operation to another value expressed in bits, the resulting value is all the live bits present in both values.

Quick code:
>>> mask = 0b11001 # keep bits 5,4,1, drop 3 and 2
>>> for i in range(0b10000, 0b100000):
...     print('{0} & {1} = {2}'.format(bin(mask), bin(i), bin(i & mask)))
... 
0b11001 & 0b10000 = 0b10000
0b11001 & 0b10001 = 0b10001
0b11001 & 0b10010 = 0b10000
0b11001 & 0b10011 = 0b10001
0b11001 & 0b10100 = 0b10000
0b11001 & 0b10101 = 0b10001
0b11001 & 0b10110 = 0b10000
0b11001 & 0b10111 = 0b10001
0b11001 & 0b11000 = 0b11000
0b11001 & 0b11001 = 0b11001
0b11001 & 0b11010 = 0b11000
0b11001 & 0b11011 = 0b11001
0b11001 & 0b11100 = 0b11000
0b11001 & 0b11101 = 0b11001
0b11001 & 0b11110 = 0b11000
0b11001 & 0b11111 = 0b11001
>>> 



Test example:
  11001 
& 10111
-------
  10001



A "mask" is simply thought of as a filter that only leaves desired bits alive. This technique is use extensively in image transformations and particularly games. In a game where a sprite moves across a background, bit mask are constantly being applied to merge the two images.
Was This Post Helpful? 0
  • +
  • -

#5 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Setting Low Bits to Zero in Python

Posted 08 October 2012 - 12:12 PM

You're calling myImage.show(newfilename) inside a for-loop. That's why it's being shown many times. I don't know about setting the colors, though, that's something I've never really played with.
Was This Post Helpful? 0
  • +
  • -

#6 confuzzledgrl  Icon User is offline

  • New D.I.C Head

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

Re: Setting Low Bits to Zero in Python

Posted 17 October 2012 - 12:29 PM

I dont know if you still need this but your code is right with everything but the zeros.

it needs to say
r & 0b11110000
g & 0b11110000
b & 0b11110000

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1