Draw line in python using PIL with explanation

line drawing using python

A short explanation to draw crossed lines in python using PIL

Importing required packages

from PIL import Image
from collections import namedtuple

The namedtuple in python language is used to create a lightweight object easily. Here, we create an object name 'Point' and this object variables are x and y

Point = namedtuple('Point', ['x', 'y'])

Finding the values of x and y from 0 to its screen size (last position that will be defined by the user)

if x < 0 or x >= screen_height_size or y < 0 or y >= screen_height_size:
return

what is interpolation and why we are using this process?

In computer graphics, interpolation is a process to resize an image's pixel from one position to another. We need to find the value of x and y coordination in an image to draw a line or something. The interpolation process increases and decreases the sum of pixels for the dependent variable and independent variable.

As we know from the basics knowledge in the matrix,

x=independent variable

y=dependent variable

that means, y's value changes depending on the x's value

This is how we find x and y co-ordinations to draw a line. Ex. (x, y)=(1, 0), (1, 1), (1, 2), (1, 3), and so on

interpolate(point_0.x, point_0.y, point_1.x, point_1.y)

 

calculating the difference between x co-ordination

dx = point_1.x - point_0.x 

calculating the difference between y co-ordination

dy = point_1.y - point_0.y

Pixels refer to the (x, y) coordination that which point will be filled with black and which is white.

pixels[x, y] = color

We are using the RGB colors parameter (R, G, B) = (Red, Green, Blue).

(255, 255, 255) refers to white color.

(0, 0, 0) refers to black color.

Taking or loading an image matrix from the Pillow image library.

background_color = (255, 255, 255)
image = Image.new("RGB", (screen_width_size, screen_height_size), background_color)

Generated image ready to show in windows dialog and to save it locally.

image.show()
image.save("crossed lines.png")

Code for drawing crossed line in python using PIL library

from PIL import Image
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])

def putPixel(pixels, x, y, color):
x = screen_width_size / 2 + x
y = screen_height_size / 2 - y
if x < 0 or x >= screen_height_size or y < 0 or y >= screen_height_size:
return

pixels[x, y] = color


def interpolate(independent_value_0, dependent_value_0, independent_value_1, dependent_value_1):
if independent_value_0 == independent_value_1:
return [dependent_value_0]
values = []
a = (dependent_value_1 - dependent_value_0) / (independent_value_1 - independent_value_0)
d = dependent_value_0
for i in range(independent_value_0, independent_value_1):
values.append(d)
d = d + a
return values

def drawLine(point_0, point_1, color):
dx = point_1.x - point_0.x
dy = point_1.y - point_0.y

if abs(dx) > abs(dy):
if dx < 0:
point_0, point_1 = point_1, point_0

ys = interpolate(point_0.x, point_0.y, point_1.x, point_1.y)
for x in range(point_0.x, point_1.x):
putPixel(pixels, x, ys[(x - point_0.x)], color)
else:
if dy < 0:
point_0, point_1 = point_1, point_0

xs = interpolate(point_0.y, point_0.x, point_1.y, point_1.x)
for y in range(point_0.y, point_1.y):
putPixel(pixels, xs[(y - point_0.y)], y, color)



if __name__=='__main__':
screen_width_size = 500
screen_height_size = 500
background_color = (255, 255, 255)
image = Image.new("RGB", (screen_width_size, screen_height_size), background_color) #Taking or loading an image matrix from the Pillow image library
pixels = image.load()
drawLine(Point(-200, -100), Point(240,120), (0,0,0))
drawLine(Point(-50, -200), Point(60, 240), (0,0,0))
image.show()
image.save("crossed lines.png")


Output of the above code: 
Crossed line output


Post a Comment

Previous Post Next Post