Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python code to make Spring Festival couplets

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Python code to make Spring Festival couplets. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

First, Spring Festival couplets 1. Effect display

two。 Code presentation

Index.html

Css3 Spring Festival couplet switching

Spring Festival couplets Show

Upper couplet: this demand is very simple: how to realize I don't care about horizontal approval: go online tomorrow!

Style.css

@ 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.letters {transform: rotateX (- 90deg);} .alizarin {color: # e74c3c;} .wisteria {color: # 8e44ad;} .peter-river {color: # 3498bb;} .emerald {color: # 2ecc71;} .sun-flower {color: # f1c40f;}

Script.js

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 = 0There 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). Environmental preparation

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

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

Idea configuration python environment can also refer to this article: Python and PyCharm download and install tutorials

two。 Effect display

3. Code import iofrom PIL import Image#import numpy as npimport requestsdef get_word (ch, quality): "" get a picture of a single Chinese character (character) ch-single Chinese character or English alphabet (uppercase only) quality-single word resolution, Hmur640 pixels, MMel 480 pixels Lmer 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 = =' fp: W, h = int (w * 0.75), int (0.75 * h) elif quality = ='fp: W, h = int (w * 0.5) Int (0.5 * h) return im.resize ((w, h)) def get_bg (quality): "get pictures of Spring Festival couplets background" return get_word ('bg', quality) def write_couplets (text, HorV='V', quality='L', out_file=None): "generate Spring Festival couplets text-Spring Festival couplets content Break lines with spaces HorV-H-horizontal, V-vertical quality-word resolution, Hmur640 pixels, Mmur480 pixels LMQ 320 Pixel out_file-output file name "" usize = {'Hang: (640,23),' MB: (480,18), 'Lemma: (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 = = 'rows: 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),' # f0f0f0f0') for row in range (rows): if HorV = 'Vince: 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 = = 'pos: 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 =' thought over and over a few lines of code to build thousands of years of Spring and Autumn support, a thousand elites with five thousand cadres will'# couplet content write_couplets (text) HorV='V', quality='M', out_file=' Spring Festival couplets .jpg') # generate universal celebration. JPG couplet pictures on "how to use Python code to make Spring Festival couplets" is here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 291

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report