473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

creating color gradients using PIL


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
Nov 21 '07 #1
1 4283
On 21 Nov, 06:30, Ramdas <ram...@gmail.comwrote:
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
Nov 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Irmen de Jong | last post by:
Hi I'm trying to create e-mail content using the email.MIMEText module. It basically works, until I tried to send mail in non-ascii format. What I did, to test both iso-8859-15 and UTF-8...
2
by: Frederik S | last post by:
Hello, I'm making a nice little login box in Javascript but having problems Posting the value inside a textfield. In a nutshell: I have a function: function getPostData (value)
10
by: Julian Smith | last post by:
I've been playing with a function that creates an anonymous function by compiling a string parameter, and it seems to work pretty well: def fn( text): exec 'def foo' + text.strip() return foo ...
8
by: nbaiju | last post by:
Hi, I am building a asp.net application which has satellite assemblies. When building the satellite assemblies dll's from Visual Studio 2003 GUI the application works fine . i.e. the resource...
2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
2
by: Ranier Dunno | last post by:
Hi, I'd like to create typed arrays during runtime, sort of like this: Array vals = Array.CreateInstance(objType, noElements); The problem is that objType will not be a "base" type like...
3
by: Sindarian | last post by:
I have a Schema from the folk that shows how they want the data received. I have my own database that has that data, but in a different way (they use 33 freaking tables to store what I do in 1)....
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
5
by: ravysters | last post by:
hi.. i am creating a .dll file from an object file using gcc... i want to create a shared library... i want to use the dll to implement a new functionality.. but when i compile the file for creating...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.