Runs on Python 2.7. It seems that PIL does not yet support Python 3 series.
There are two basics of CG.
One is to put a point at any coordinate.
The other is to read points with arbitrary coordinates.
With these two functions, in theory, you can draw any picture ...
Here, I will draw the former point.
The latter will be discussed later (here →) [PIL (Python Imaging Library) to convert images to sepia toning --Qiita](http://qiita.com/suto3/items/7c3f2d392ad60977d49e "PIL (Python Imaging Library)" Convert image to sepia-Qiita ")
First from the results. Dot like this.

Create an image file using PIL (Python Imaging Library). --Qiita
I wrote as follows based on.
image-set-flower.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Dot sample
'''
import Image
import ImageDraw
def drawing(img, gap):
    """
Drawing (editing the image data written in the file)
Dot
    """
    x,y = img.size
    draw = ImageDraw.Draw(img)
    for i in range(0,x,gap):
        for j in range(0,y,gap):
            #↓ Draw such a pattern
            #■■■□
            #■□■□
            #■■■□
            #□□□□
            draw.point((i  , j  ),(0xff,0x00,0x00)) #red
            draw.point((i+1, j  ),(0xff,0x00,0x00)) #red
            draw.point((i+2, j  ),(0xff,0x00,0x00)) #red
            draw.point((i+3, j  ),(0x00,0xff,0x00)) #green
            
            draw.point((i  , j+1),(0xff,0x00,0x00)) #red
            draw.point((i+1, j+1),(0xff,0xff,0x00)) #yellow
            draw.point((i+2, j+1),(0xff,0x00,0x00)) #red
            draw.point((i+3, j+1),(0x00,0xff,0x00)) #green
            
            draw.point((i  , j+2),(0xff,0x00,0x00)) #red
            draw.point((i+1, j+2),(0xff,0x00,0x00)) #red
            draw.point((i+2, j+2),(0xff,0x00,0x00)) #red
            draw.point((i+3, j+2),(0x00,0xff,0x00)) #green
            draw.point((i  , j+3),(0x00,0xff,0x00)) #green
            draw.point((i+1, j+3),(0x00,0xff,0x00)) #green
            draw.point((i+2, j+3),(0x00,0xff,0x00)) #green
            draw.point((i+3, j+3),(0x00,0xff,0x00)) #green
    return img
def make_image(screen, bgcolor, filename):
    """
Create image file
    """
    img = Image.new('RGB', screen, bgcolor)
    #Interval (about 5 to 32)
    gap = 8
    img = drawing(img, gap)
    img.save(filename)
if __name__ == '__main__':
    #Image size
    screen = (800,600)
    #Image background color (RGB)
    bgcolor=(0xdd,0xdd,0xdd)
    #File name to save (File format is automatically determined from the extension)
    filename = "image-set-flower.png "
    make_image(screen, bgcolor, filename)
#EOF
Recommended Posts