<filename>python/pillow/create_and_save_greyscale.py<gh_stars>10-100
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2015 <NAME> (http://www.jdhp.org)

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import PIL.Image as pil_img     # PIL.Image is a module not a class...

SIZE_X = 320
SIZE_Y = 200

def main():
    """Main function"""

    # Make the image
    mode = "L"              # Grayscale
    size = (SIZE_X, SIZE_Y)
    img = pil_img.new(mode, size)

    # Make the data (pixels value in [0;255])
    # WARNING: nested list and 2D numpy arrays are silently rejected!!!
    #          data *must* be a list or a 1D numpy array!
    data = [(x+y)/(size[0]+size[1])*255 for y in range(size[1]) for x in range(size[0])]
    img.putdata(data) 

    # Save the image
    img.save("create_and_save_greyscale.png")

if __name__ == '__main__':
    main()
