Moving colors in a bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Moving colors in a bitmap

    I want to take a simple bitmap which has 6 horizontal lines of color in it like this:
    red
    blue
    green
    red
    blue
    green
    I want to move the lines of colors around like this:
    red
    red
    blue
    blue
    green
    green
    The code below is what I was trying to work with but I get an error message that the tuple is out of range. Is there some other code I could use for this or is it feasible to use this code with some changes?

    Code:
    import sys
    import Image
    import ImageFilter
    
    im = Image.open("7.bmp").convert("L")
    source = im.split()
    
    R, G, B = 0, 1, 2
    
    # select regions where red is less than 100
    mask = source[R].point(lambda i: i < 100 and 255)
    
    # process the green band
    out = source[G].point(lambda i: i * 0.7)
    
    # paste the processed band back, but only where red was < 100
    source[G].paste(out, None, mask)
    
    # build a new multiband image
    im = Image.merge(im.mode, source)
  • Thekid
    New Member
    • Feb 2007
    • 145

    #2
    I've decided to try a different approach to this. I made a white bitmap with 2 black squares in it to play with. I'm wondering if I could do something like this:
    Code:
    f = open("xmple.bmp", rb)
    s = f.read()
    print (repr)s
    Could I now use some type of sorting algorithm to arrange things, then save the new bmp? I tried it like this, but it doesn't work:

    Code:
    f=open("xmple.bmp",rb)
    s=f.read()
    s1=s.sort
    s1.save("xmple2.bmp")

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      I don't really understand your problem. Do you have an image, and want to sort the pixel values of all the pixels in the image?

      -kudos


      Originally posted by Thekid
      I've decided to try a different approach to this. I made a white bitmap with 2 black squares in it to play with. I'm wondering if I could do something like this:
      Code:
      f = open("xmple.bmp", rb)
      s = f.read()
      print (repr)s
      Could I now use some type of sorting algorithm to arrange things, then save the new bmp? I tried it like this, but it doesn't work:

      Code:
      f=open("xmple.bmp",rb)
      s=f.read()
      s1=s.sort
      s1.save("xmple2.bmp")

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        Yes, I'm just not sure how to go about it so I was trying to 'read binary' and sort somehow like that.

        Comment

        • kudos
          Recognized Expert New Member
          • Jul 2006
          • 127

          #5
          Are you familiar with PIL(if not, check it out, it a nice (but a bit dated) API) ? Here is how I would do it

          Code:
          from PIL import Image
          
          iy = Image.open("kudos.png")
          ysize = iy.size()
          iwidth = ysize[0]
          iheight = ysize[1]
          ix = Image.new("RGB",(iwidth,iheight+1))
          
          
          colors = {}
          for i in range(iheight):
           for j in range(iwidth):
            a = iy.getpixel((j,i))
            if(not(a in colors)):
             colors[a] = 0
            else:
             b = colors[a]
             b+=1
             colors[a] = b
          
          x = 0
          y = 0
          s = colors.keys()   
          for v in s:
           for i in range(colors[v]+1):
            if(x + 1 >= iwidth):
             x=0
             y+=1
            else:
             x+=1
            ix.putpixel((x,y),v)
          ix.save("result.png","PNG")
          -kudos

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #6
            Thank you for the reply Kudos. When I try that code I get this error message:

            File "C:\Python25\bm p2.py", line 4, in <module>
            ysize = iy.size()
            TypeError: 'tuple' object is not callable

            Comment

            • kudos
              Recognized Expert New Member
              • Jul 2006
              • 127

              #7
              did you change the image that was going to be loaded? maybe change the format, paste in the code & image that creates trouble

              -kudos


              Originally posted by Thekid
              Thank you for the reply Kudos. When I try that code I get this error message:

              File "C:\Python25\bm p2.py", line 4, in <module>
              ysize = iy.size()
              TypeError: 'tuple' object is not callable

              Comment

              • Thekid
                New Member
                • Feb 2007
                • 145

                #8
                Kudos, I took your code and just change the name of the file to my file. I tried it as png format and bmp but got the same error message either way. I don't know how to put the images in here so I uploaded them on imageshack. The first one is just a plain one with 2 black squares, the second one is striped with red,blue,green, red,blue,green lines. I made the plain one because I thought it would be easier to tell the black pixels from the white ones.
                http://img90.imageshac k.us/my.php?image=x7 xxzq4.png
                http://img80.imageshac k.us/my.php?image=xm ple2sb5.png

                Comment

                • kudos
                  Recognized Expert New Member
                  • Jul 2006
                  • 127

                  #9
                  aaa...it turns out that there was a type in the code!

                  try this:

                  Code:
                  from PIL import Image
                  iy = Image.open("testme.png")
                  
                  
                  ysize = iy.size
                  iwidth = ysize[0]
                  iheight = ysize[1]
                  ix = Image.new("RGB",(iwidth,iheight+1))
                  
                  
                  colors = {}
                  for i in range(iheight):
                   for j in range(iwidth):
                    a = iy.getpixel((j,i))
                    if(not(a in colors)):
                     colors[a] = 0
                    else:
                     b = colors[a]
                     b+=1
                     colors[a] = b
                  
                  x = 0
                  y = 0
                  s = colors.keys()   
                  for v in s:
                   for i in range(colors[v]+1):
                    if(x + 1 >= iwidth):
                     x=0
                     y+=1
                    else:
                     x+=1
                    ix.putpixel((x,y),v)
                  ix.save("result.png","PNG")
                  -kudos

                  Originally posted by Thekid
                  Kudos, I took your code and just change the name of the file to my file. I tried it as png format and bmp but got the same error message either way. I don't know how to put the images in here so I uploaded them on imageshack. The first one is just a plain one with 2 black squares, the second one is striped with red,blue,green, red,blue,green lines. I made the plain one because I thought it would be easier to tell the black pixels from the white ones.
                  http://img90.imageshac k.us/my.php?image=x7 xxzq4.png
                  http://img80.imageshac k.us/my.php?image=xm ple2sb5.png

                  Comment

                  • Thekid
                    New Member
                    • Feb 2007
                    • 145

                    #10
                    Awesome! Works great....thank you!

                    Comment

                    Working...