你绝对没见过的船新版本,利用Python代码制作过年春联。

大家好我是渣渣辉,呸不好意思,最近广告看多了,大家好我是北凡春节来了兄弟们,it程序员的快乐就是用代码码出一切,这里呢教兄弟们一个好玩又简单的春联代码,

一、春联一

1.代码展示

index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!DOCTYPE html>

<html lang="en" >

<head>

<meta charset="UTF-8">

<title>css3春联切换</title>

<link rel="stylesheet" href="css/style.css" rel="external nofollow" >

</head>

<body>

<div class="rotating-text">

    <p>春联Show</p>

    <p>

        <span class="word alizarin">Python Java C++ very verySo Easy</span>#三种编程语言非常非常简单

        <span class="word wisteria">Years Months Weeks Day Day No Bug</span>#年年月月周周天天都没有漏洞

        <span class="word peter-river">Happy New Year</span>#新年快乐

    </p>

</div>

<script  src="js/script.js"></script>

</body>

</html>

style.css

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

@import url(https://fonts.googleapis.com/css?family=Lato:600);

body {

  display: flex;

  justify-content: center;

  align-items: center;

  height: 100vh;

  background: #222;

}

.rotating-text {

  font-family: Lato, sans-serif;

  font-weight: 600;

  font-size: 36px;

  color: white;

  transform: translateX(-80px);

}

.rotating-text p {

  display: inline-flex;

  margin: 0;

  vertical-align: top;

}

.rotating-text p .word {

  position: absolute;

  display: flex;

  opacity: 0;

}

.rotating-text p .word .letter {

  transform-origin: center center 25px;

}

.rotating-text p .word .letter.out {

  transform: rotateX(90deg);

  transition: 0.32s cubic-bezier(0.6, 0, 0.7, 0.2);

}

.rotating-text p .word .letter.in {

  transition: 0.38s ease;

}

.rotating-text p .word .letter.behind {

  transform: rotateX(-90deg);

}

.alizarin {

  color: #e74c3c;

}

.wisteria {

  color: #8e44ad;

}

.peter-river {

  color: #3498db;

}

.emerald {

  color: #2ecc71;

}

.sun-flower {

  color: #f1c40f;

}

script.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

var words = document.querySelectorAll(".word");

words.forEach(function (word) {

    var letters = word.textContent.split("");

    word.textContent = "";

    letters.forEach(function (letter) {

        var span = document.createElement("span");

        span.textContent = letter;

        span.className = "letter";

        word.append(span);

    });

});

var currentWordIndex = 0;

var maxWordIndex = words.length - 1;

words[currentWordIndex].style.opacity = "1";

var rotateText = function () {

    var currentWord = words[currentWordIndex];

    var nextWord = currentWordIndex === maxWordIndex ? words[0] : words[currentWordIndex + 1];

    // rotate out letters of current word

    Array.from(currentWord.children).forEach(function (letter, i) {

        setTimeout(function () {

            letter.className = "letter out";

        }, i * 80);

    });

    // reveal and rotate in letters of next word

    nextWord.style.opacity = "1";

    Array.from(nextWord.children).forEach(function (letter, i) {

        letter.className = "letter behind";

        setTimeout(function () {

            letter.className = "letter in";

        }, 340 + i * 80);

    });

    currentWordIndex =

        currentWordIndex === maxWordIndex ? 0 : currentWordIndex + 1;

};

rotateText();

setInterval(rotateText, 4000);

二、春联二

1.环境准备

当缺少库时会有相应提示 黑窗口执行下方命令+包名即可下载安装

凡哥此处用的idea 直接Alt+Enter直接下载就成了

idea配置python环境也可以参考此文:

十年测试经验的阿里p10讲解python初阶:基础语法 python全栈自动化测试系类4-1_测试架构师北凡的博客-CSDN博客

2.效果展示

3.代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

import io

from PIL import Image

#import numpy as np

import requests

def get_word(ch, quality):

    """获取单个汉字(字符)的图片

    ch          - 单个汉字或英文字母(仅支持大写)

    quality     - 单字分辨率,H-640像素,M-480像素,L-320像素

    """

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

    """获取春联背景的图片"""

    return get_word('bg', quality)

def write_couplets(text, HorV='V', quality='L', out_file=None):

    """生成春联

    text        - 春联内容,以空格断行

    HorV        - H-横排,V-竖排

    quality     - 单字分辨率,H-640像素,M-480像素,L-320像素

    out_file    - 输出文件名

    """

    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 = '思前想后几行代码筑万载春秋 扶内保外一千精英带五千干将' #对联内容

write_couplets(text, HorV='V', quality='M', out_file='春联.jpg') #生成普天同庆.jpg对联

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)


 ⑤ Python学习路线图(告别不入流的学习)

在我的QQ技术交流群里(技术交流和资源共享,广告进来腿给你打断)

可以自助拿走,群号913569736(备注“csdn000”)群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

学习资料可以找到我们呢的蓓蓓小姐姐【mashang-qq】备注【csdn000】免费领取哦

【必须备注】不然不会通过哦

猜你喜欢

转载自blog.csdn.net/csdnchengxi/article/details/122628026
今日推荐