Output "Draw ferns programmatically" to the drawing process in Python

Overview

** The drawing part uses the code of @ noc06140728. ** ** http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4

I wanted to see the drawing process, so I made it. Please use it as a reference for creating applications with PySide.

code

GUI version

** * Drawing is started with mousePressEvent ** ** * Not assumed if drawing is started again during drawing **

fern_gui.py


import sys
import random
from PySide.QtGui import QApplication
from PySide.QtGui import QMainWindow
from PySide.QtGui import QLabel
from PySide.QtGui import QPixmap
from PySide.QtGui import QPainter
from PySide.QtGui import QColor

# Quot Begin
# From http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4
N = 20
xm = 0
ym = 0.5
h = 0.6

width = 500
height = 500

W1x = lambda x, y: 0.836 * x + 0.044 * y
W1y = lambda x, y: -0.044 * x + 0.836 * y + 0.169
W2x = lambda x, y: -0.141 * x + 0.302 * y
W2y = lambda x, y: 0.302 * x + 0.141 * y + 0.127
W3x = lambda x, y: 0.141 * x - 0.302 * y
W3y = lambda x, y: 0.302 * x + 0.141 * y + 0.169
W4x = lambda x, y: 0
W4y = lambda x, y: 0.175337 * y

def f(k, x, y):
    if 0 < k:
        for p in f(k - 1, W1x(x, y), W1y(x, y)):
            yield p
        if random.random() < 0.3:
            for p in f(k - 1, W2x(x, y), W2y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W3x(x, y), W3y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W4x(x, y), W4y(x, y)):
                yield p
    else:
        s = 490
        yield x * s + width * 0.5, height - y * s
# Quot End


class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setFixedSize(width, height)
        self.refresh_rate = 200
        self.pixmap = QPixmap(width, height)
        self.pixmap.fill()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pixmap)

    def mousePressEvent(self, event):
        painter = QPainter(self.pixmap)
        painter.setPen(QColor(0, 128, 0))
        for i, p in enumerate(f(N, 0, 0)):
            painter.drawPoint(p[0], p[1])
            if i % self.refresh_rate == 0:
                self.repaint()
        self.repaint()
        painter.end()
        print("finished")

def main():
    app = QApplication(sys.argv)

    window = MainWindow()
    app.setActiveWindow(window)
    window.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

CUI version (PNG serial number output)

fern_cui.py


import sys
import random
from PySide.QtCore import QDir
from PySide.QtGui import QDesktopServices
from PySide.QtGui import QApplication
from PySide.QtGui import QPixmap
from PySide.QtGui import QPainter
from PySide.QtGui import QColor

# Quot Begin
# From http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4
N = 20
xm = 0
ym = 0.5
h = 0.6

width = 500
height = 500

W1x = lambda x, y: 0.836 * x + 0.044 * y
W1y = lambda x, y: -0.044 * x + 0.836 * y + 0.169
W2x = lambda x, y: -0.141 * x + 0.302 * y
W2y = lambda x, y: 0.302 * x + 0.141 * y + 0.127
W3x = lambda x, y: 0.141 * x - 0.302 * y
W3y = lambda x, y: 0.302 * x + 0.141 * y + 0.169
W4x = lambda x, y: 0
W4y = lambda x, y: 0.175337 * y

def f(k, x, y):
    if 0 < k:
        for p in f(k - 1, W1x(x, y), W1y(x, y)):
            yield p
        if random.random() < 0.3:
            for p in f(k - 1, W2x(x, y), W2y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W3x(x, y), W3y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W4x(x, y), W4y(x, y)):
                yield p
    else:
        s = 490
        yield x * s + width * 0.5, height - y * s
# Quot End

def main():
    app = QApplication(sys.argv)

    # Output Directory
    # <Desktop>/ferns/
    directory = QDir(QDesktopServices.storageLocation(QDesktopServices.DesktopLocation))
    directory.mkdir("ferns")
    if directory.cd("ferns") == False:
        print("directory error")
        app.quit()
        return

    # Output
    # <Desktop>/ferns/fern_*****.png
    output_rate = 2000
    pixmap = QPixmap(width, height)
    pixmap.fill()
    painter = QPainter(pixmap)
    painter.setPen(QColor(0, 128, 0))
    for i, p in enumerate(f(N, 0, 0)):
        painter.drawPoint(p[0], p[1])
        if i % output_rate == 0:
            pixmap.save("%s/fern_%05d.png " % (directory.path(), i / output_rate))
    pixmap.save("%s/fern_%05d.png " % (directory.path(), i / output_rate + 1))
    painter.end()

    print("finished")
    app.quit()


if __name__ == '__main__':
    main()

Output sample (GIF animation)

animated.gif

Recommended Posts

Output "Draw ferns programmatically" to the drawing process in Python
Draw "Draw Ferns Programmatically" in Python
Draw graphs in Julia ... Leave the graphs to Python
[Python] How to output the list values in order
Change the standard output destination to a file in Python
In the python command python points to python3.8
Output the contents of ~ .xlsx in the folder to HTML with Python
How to use the C library in Python
Output the number of CPU cores in Python
3.14 π day, so try to output in Python
From file to graph drawing in Python. Elementary elementary
Draw "Draw Ferns Programmatically" in Python-No Recursion + Generator Version-
To dynamically replace the next method in python
The trick to write flatten concisely in python
How to erase the characters output by Python
How to get the files in the [Python] folder
Output in the form of a python array
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
I want to display the progress in Python!
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Graph drawing in python
Japanese output in Python
Draw graph in python
How to retrieve the nth largest value in Python
I tried to graph the packages installed in Python
How to get the variable name itself in python
How to get the number of digits in Python
How to know the current directory in Python in Blender
Output the time from the time the program was started in python
[python] option to turn off the output of click.progressbar
Carefully understand the exponential distribution and draw in Python
Convert the image in .zip to PDF with Python
Carefully understand the Poisson distribution and draw in Python
I want to write in Python! (3) Utilize the mock
How to use the model learned in Lobe in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
Run the output code with tkinter, saying "A, pretending to be B" in python
Output the specified table of Oracle database in Python to Excel for each file
[Mac] A super-easy way to execute system commands in Python and output the results
It is easy to execute SQL with Python and output the result in Excel
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
[python] How to check if the Key exists in the dictionary
Drawing candle charts in python
To flush stdout in Python
Draw mp3 waveform in Python
Download the file in Python
How to debug the Python standard library in Visual Studio
[python] How to use the library Matplotlib for drawing graphs
Find the difference in Python
How to use the __call__ method in a Python class
Login to website in Python
Draw Poincare's disk in Python
Sort and output the elements in the list as elements and multiples in Python.
Various ways to calculate the similarity between data in python
How to get the last (last) value in a list in Python
Draw implicit function in python
Debug by attaching to the Python process of the SSH destination
Speech to speech in python [text to speech]