creating color gradients using PIL

Discussion in 'Python' started by Ramdas, Nov 21, 2007.

  1. Ramdas

    Ramdas Guest

    Any ideas how we can create a color gradient using Python Imaging
    Library. Has any got some sample code that can give me some idea. I
    need to create a horizontal and vertical color gradient for a college
    project

    Thanks
     
    Ramdas, Nov 21, 2007
    #1
    1. Advertising

  2. Ramdas

    Simon Hibbs Guest

    On 21 Nov, 06:30, Ramdas <> wrote:
    > Any ideas how we can create a color gradient using Python Imaging
    > Library. Has any got some sample code that can give me some idea. I
    > need to create a horizontal and vertical color gradient for a college
    > project
    >
    > Thanks


    I use these functions for building PIL images from numpy arrays

    # Convert a 2-d array with typecode 'b' to an image with mode 'P'
    def numpy2pil(arr):
    rows = arr.shape[0]
    cols = arr.shape[1]
    m = arr.tostring()
    out = Image.new('L', (cols, rows), 999 )
    #out.fromstring(m)
    out.fromstring(m, 'raw', 'L', 0, -1)
    return out

    def pil2numpy(im, typecode='b'):
    # tostring does something funny with '1' images (packs em
    tight).
    # For 'P' images, the image data is not pased through the
    palette.
    if im.mode != 'L' and im.mode != 'P':
    print 'im.mode must be "L" or "P"'
    raise 'terminate'
    xsize = im.size[0]
    ysize = im.size[1]
    m = im.tostring()
    t = fromstring(m, 'b')
    tt = asarray(t, typecode)
    # Note that ysize is first:
    return reshape(tt, (ysize, xsize))

    im = numpy2pil(myarray)
    im.putpalette(palette_list)
    im.save('myimage.png')

    You'll need to import numpy and Image. You'll need to generate the
    palette (juust a list) and image array (a numpy array) of course.

    Simon Hibbs
     
    Simon Hibbs, Nov 23, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Sikorsky
    Replies:
    2
    Views:
    545
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 3, 2007
  2. manish jain

    Gradients Text in Html

    manish jain, Dec 15, 2009, in forum: HTML and CSS
    Replies:
    0
    Views:
    525
    manish jain
    Dec 15, 2009
  3. Sverre
    Replies:
    2
    Views:
    3,696
    Sverre
    Dec 17, 2009
  4. Gerald Bauer
    Replies:
    1
    Views:
    278
    Gerald Bauer
    May 20, 2008
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    499
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page