[code writing Spring Festival couplets] in 2022, show me the Spring Festival couplets I wrote in Python code

Posted by gaspower on Mon, 24 Jan 2022 14:03:40 +0100

Introduction: now many people, most of them are only in rough writing, and they don't know how to write with brush. But the yearning for a better life is no less than that of literati. Here programmers write beautiful Spring Festival couplets with the help of code. Now, let's show you how to write Spring Festival couplets in Python.

1. Environmental preparation

When the library is missing, there will be a corresponding prompt in the black window. Execute the following command + package name to download and install

The idea used here can be downloaded directly by Alt+Enter

For idea configuration of python environment, you can also refer to this article: Python and PyCharm download and installation tutorial

2. Effect display

3. Code

import io
from PIL import Image
#import numpy as np
import requests


def get\_word(ch, quality):
    """Get a picture of a single Chinese character (character)
    ch          - Single Chinese character or English letter (uppercase only)
    quality     - Single word resolution, H-640 Pixels, M-480 Pixels, L-320 pixel
    """

    fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch': ch}).content)
    im = Image.open(fp)
    w, h = im.size
    if quality == 'M':
        w, h = int(w \* 0.75), int(0.75 \* h)
    elif quality == 'L':
        w, h = int(w \* 0.5), int(0.5 \* h)

    return im.resize((w, h))


def get\_bg(quality):
    """Get the picture of the spring festival couplet background"""

    return get\_word('bg', quality)


def write\_couplets(text, HorV='V', quality='L', out\_file=None):
    """Generate Spring Festival couplets

    text        - The content of Spring Festival couplets shall be broken with spaces
    HorV        - H-Horizontally, V-Vertical row
    quality     - Single word resolution, H-640 Pixels, M-480 Pixels, L-320 pixel
    out\_file    - Output file name
    """

    usize = {'H': (640, 23), 'M': (480, 18), 'L': (320, 12)}
    bg\_im = get\_bg(quality)
    text\_list = \[list(item) for item in text.split()\]
    rows = len(text\_list)
    cols = max(\[len(item) for item in text\_list\])

    if HorV == 'V':
        ow, oh = 40 + rows \* usize\[quality\]\[0\] + (rows - 1) \* 10, 40 + cols \* usize\[quality\]\[0\]
    else:
        ow, oh = 40 + cols \* usize\[quality\]\[0\], 40 + rows \* usize\[quality\]\[0\] + (rows - 1) \* 10
    out\_im = Image.new('RGBA', (ow, oh), '#f0f0f0')

    for row in range(rows):
        if HorV == 'V':
            row\_im = Image.new('RGBA', (usize\[quality\]\[0\], cols \* usize\[quality\]\[0\]), 'white')
            offset = (ow - (usize\[quality\]\[0\] + 10) \* (row + 1) - 10, 20)
        else:
            row\_im = Image.new('RGBA', (cols \* usize\[quality\]\[0\], usize\[quality\]\[0\]), 'white')
            offset = (20, 20 + (usize\[quality\]\[0\] + 10) \* row)

        for col, ch in enumerate(text\_list\[row\]):
            if HorV == 'V':
                pos = (0, col \* usize\[quality\]\[0\])
            else:
                pos = (col \* usize\[quality\]\[0\], 0)

            ch\_im = get\_word(ch, quality)
            row\_im.paste(bg\_im, pos)
            row\_im.paste(ch\_im, (pos\[0\] + usize\[quality\]\[1\], pos\[1\] + usize\[quality\]\[1\]), mask=ch\_im)

        out\_im.paste(row\_im, offset)

    if out\_file:
        out\_im.convert('RGB').save(out\_file)
    out\_im.show()

text = 'Think before and after a few lines of code to build ten thousand years. In the spring and autumn, help 1000 elites and 5000 cadres' #Couplet content
write\_couplets(text, HorV='V', quality='M', out\_file='Spring Festival couplets.jpg') #The whole world is celebrating jpg couplet picture

The above is the details of making Spring Festival couplets using Python code

How to learn Python?

It's good to learn Python well, whether in employment or sideline, but to learn python, you still need to have a learning plan. Finally, let's share a full set of Python learning materials to help those who want to learn Python!

Python learning route

Here, the common technical points of Python are sorted out. There is a summary of knowledge points in various fields. You can find the corresponding learning resources according to the above knowledge points.

Learning software

Python commonly used development software will save you a lot of time.

Learning video

Programming learning must watch more videos. Only by combining books and videos can we get twice the result with half the effort.

Actual combat cases

Optical theory is useless. When learning programming, we must avoid talking on paper. We must practice and apply our knowledge to practice.

Interview materials

We study Python for different purposes, some for more efficient work and some for high paying jobs. The following interview questions are useful. They are the latest interview materials from front-line Internet companies such as Ali, Tencent and byte. The most important thing is that Tencent has given authoritative answers, After brushing this set of interview materials, I believe everyone can get a satisfactory offer.


The full set of Python learning materials above has been uploaded to CSDN. Friends in need can directly scan the official authentication QR code of CSDN below on wechat and get it for free [guaranteed to be 100% free].

Topics: Python Back-end Programmer