Achieve your dream of painting with python

Lead:

Are you still did not choose their own dreams when young is sad, if not also for his paintings become famous and distress, all without worry. python can help you achieve, eh! How can python painting it, some simple patterns no problem, but if I wanted to sketch it is certainly no way ah!

demand analysis:

By python script codes to achieve drawn sketch

Installation Tools

pip install pillow

pip install numpy

Code

First we need to look at the picture we need:

This is a buffalo, then how do we turn it into a sketch of it?

We look at the first option:

# -*- coding: utf-8 -*-
from PIL import Image
from random import randint

old = Image.open(r"da.jpg")
new = Image.new('L', old.size, 255)
w, d = old.size
old = old.convert('L')
PEN_SIZE = 3
COLOR_DIFF = 7
LINE_LEN = 2

for i in range(PEN_SIZE + 1, w - PEN_SIZE - 1):
    for j in range(PEN_SIZE + 1, d - PEN_SIZE - 1):
        originalcolor = 255
        lcolor = sum([old.getpixel((i - r, j))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        rcolor = sum([old.getpixel((i + r, j))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(lcolor - rcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i, j + p), originalcolor)

        ucolor = sum([old.getpixel((i, j - r))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        dcolor = sum([old.getpixel((i, j + r))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(ucolor - dcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i + p, j), originalcolor)

        lucolor = sum([old.getpixel((i - r, j - r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        rdcolor = sum([old.getpixel((i + r, j + r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(lucolor - rdcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i - p, j + p), originalcolor)

        rucolor = sum([old.getpixel((i + r, j - r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        ldcolor = sum([old.getpixel((i - r, j + r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(rucolor - ldcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i + p, j + p), originalcolor)

new.save(r"pencil_drawing.jpg")

To the line units sketch when we first sketch this pattern, but also increases the random function, the length of the pattern of lines uncertain, the creation of such a sketch looks softer, looks closer to the true human painting style .

However, this method has some drawbacks,

  • First, the amount of code more

  • Second, the execution speed is too slow

You want to implement a sketch pattern by the way, we need to wait a long time.

So is there a better way?

Come, we will look at, then we use a more friendly way to achieve this demand

from PIL import Image
import numpy as np

a = np.asarray(Image.open('牛.jpg').convert('L')).astype('float')
depth = 10.  # (0-100)
grad = np.gradient(a)  # 取图像灰度的梯度值
grad_x, grad_y = grad  # 分别取横纵图像梯度值
grad_x = grad_x * depth / 100.
grad_y = grad_y * depth / 100.
A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
uni_x = grad_x / A
uni_y = grad_y / A
uni_z = 1. / A
vec_el = np.pi / 2.2  # 光源的俯视角度,弧度值
vec_az = np.pi / 4.  # 光源的方位角度,弧度值
dx = np.cos(vec_el) * np.cos(vec_az)  # 光源对x 轴的影响
dy = np.cos(vec_el) * np.sin(vec_az)  # 光源对y 轴的影响
dz = np.sin(vec_el)  # 光源对z 轴的影响
b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z)  # 光源归一化
b = b.clip(0, 255)
im = Image.fromarray(b.astype('uint8'))  # 重构图像
im.save('new.jpg')

May be a little careful, you can see that I am using, data analysis, financial quantification, machine learning, artificial intelligence is an indispensable tool bag numpy, and shorten the amount of code of two dozen line, compared to the above effect in that way, even better, the speed of operation but also many times faster.

Guess you like

Origin www.cnblogs.com/Yang-Sen/p/11841070.html