×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    pypng and graphics.Image

    Hi everyone,

    I have recently found a pypng module ( http://packages.python.org/pypng/index.html ) which is an excellent tool to work with .png images. However, I can't figure out if this module can be used to create a standard graphics.Image objects directly. The reason why I want to use pypng and not the graphics.Image.open function is that pypng can extract alpha channel from the file. My goal is to create two graphics.Image objects, one which contains RGB data from file, and the other one which contains an alpha channel from file, to be used as a mask in blit operations. Could somebody help me with that?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Re: pypng and graphics.Image

    You don't need that module for make a mask, see this:

    PHP Code:
    def automask(im):
        
    widthheight im.size
        mask 
    Image.new(im.size'1'# black and white
        
    tran im.getpixel((0,0))[0]   # transparent top-left
        
    for y in range(height):
            
    line im.getpixel([(xy) for x in range(width)])
            for 
    x in range(width):
                if 
    line[x] == tran:
                    
    mask.point((x,y), 0)  # mask on the point
        
    return mask 
    example of usage:

    PHP Code:
    from graphics import Image

    ship 
    Image.open('E:\\Images\\ship.gif')
    mask automask(ship)

    canvas.blit(shipmask=mask)  # don't forget to create canvas first 
    Regards.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Re: pypng and graphics.Image

    I've seen that article, thanks, but that's not the thing I want. PNG files have an alpha channel, not just one transparent color, so this easy way doesn't really work.

  4. #4
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India
    Posts
    138

    Re: pypng and graphics.Image

    Hi Uhm57,

    For masking and transparency look this article.

    Unleash the power of PNG transparency with masks

    Hope it helps.
    Pavan Pareta
    Sr. Software Engineer
    http://windows-mobile-dev.blogspot.com

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Re: pypng and graphics.Image

    Sorry, guys, but it doesn't help (the previous answer was a part of that article, too).

    To clarify what I need, let's change the question a bit. Suppose that I have an array of numbers (or RGB triplets with R=G=B). Is there any easy way to convert it to a grayscale Image object (as in graphics.Image)? Alternatively, is there some documentation or code which explains how graphics.Image objects are created?

  6. #6
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: pypng and graphics.Image

    Quote Originally Posted by uhm57 View Post
    Sorry, guys, but it doesn't help (the previous answer was a part of that article, too).

    To clarify what I need, let's change the question a bit. Suppose that I have an array of numbers (or RGB triplets with R=G=B). Is there any easy way to convert it to a grayscale Image object (as in graphics.Image)? Alternatively, is there some documentation or code which explains how graphics.Image objects are created?
    I think you can create the image using the desired size and color scheme. After, you can set all points using the expression Y = 0.3*R + 0.59*G + 0.11*B (see here for details). If you have created the image using colors, just use same Y value for R, G and B.

    Marcelo

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Re: pypng and graphics.Image

    If by "set points" you mean using point method, yes, that should work, but I think copying an image from an array pixel-by-pixel would be terribly slow (correct me if I'm wrong, though). Isn't there a faster way, like working with Image as an array too?

  8. #8
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: pypng and graphics.Image

    Quote Originally Posted by uhm57 View Post
    If by "set points" you mean using point method, yes, that should work, but I think copying an image from an array pixel-by-pixel would be terribly slow (correct me if I'm wrong, though). Isn't there a faster way, like working with Image as an array too?
    Yes, it will be slow but I can not realize any other method to accomplish this task
    Let's wait more answers ...

    Marcelo

Similar Threads

  1. Replies: 2
    Last Post: 2009-12-08, 21:43
  2. graphics.Image - load from stream
    By neil.young in forum Symbian
    Replies: 4
    Last Post: 2009-03-24, 13:05
  3. graphics.Image mode
    By lpvalente in forum Symbian
    Replies: 4
    Last Post: 2008-10-29, 21:11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×