A sample for drawing points with PIL (Python Imaging Library).

Runs on Python 2.7. It seems that PIL does not yet support Python 3 series.

What do you want to do

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 ")

Execution result

First from the results. Dot like this.

image-set-flower.png

script

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

A sample for drawing points with PIL (Python Imaging Library).
Convert images to sepia toning with PIL (Python Imaging Library)
PIL (Python Imaging Library) installation memorandum
Library for specifying a name server and dig with python
Create a dummy image with Python + PIL.
[Python] Drawing a swirl pattern with turtle
Try HTML scraping with a Python library
Try drawing a map with python + cartopy 0.18.0
Get a ticket for a theme park with python
Create a LINE BOT with Minette for Python
Procedure for creating a LineBot made with Python
Introduction of Python Imaging Library (PIL) using HomeBrew
Commands for creating a python3 environment with virtualenv
Drawing with Python Tinker
Create an image file using PIL (Python Imaging Library).
Build a python environment for each directory with pyenv-virtualenv
PIL with Python on Windows 8 (for Google App Engine)
Create a Layer for AWS Lambda Python with Docker
Sample data created with python
[python] How to use the library Matplotlib for drawing graphs
Create a child account for connect with Stripe in Python
Experiment to make a self-catering PDF for Kindle with Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
Installation procedure for Python and Ansible with a specific version
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Make a fortune with Python
[Python] Sample code for Python grammar
Create a directory with python
Turn an array of strings with a for statement (Python3)
[Python] Create a screen for HTTP status code 403/404/500 with Django
Create a Python execution environment for Windows with VScode + Remote WSL
I made a library to easily read config files with Python
Building a Windows 7 environment for getting started with machine learning with Python
Created a library for python that can easily handle morpheme division
Create a color picker for the color wheel with Python + Qt (PySide)
I made a lot of files for RDP connection with Python
The story of making a standard driver for db with python.
[For beginners] How to register a library created in Python in PyPI
I made a Python wrapper library for docomo image recognition API.
Create a USB boot Ubuntu with a Python environment for data analysis
A memo for when pip3 is installed with python2.7 for some reason
[Python] What is a with statement?
Use pymol as a python library
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Drawing a silverstone curve using python
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
<For beginners> python library <For machine learning>
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
[Hyperledger Iroha] Query with Python library
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables