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 to realize a Spring Festival couplet

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

Share

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

This article is about how to use Python to implement a Spring Festival couplet content. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

preface

Spring Festival couplets are the products of the perfect connection of Chinese traditional festivals with national culture. They express beautiful wishes with beautiful poems and characters, and are the unique literary forms of China. It was also the custom of the Chinese people since ancient times, and every household was indispensable.

As programmers, we do not know whether you can write calligraphy, anyway, I practiced when I was a child, but now do not write. However, the longing for a better life is no less than that of Li Bai, Du Fu and other big shots. We can also write beautiful Spring Festival couplets with code and send our best wishes to our relatives and friends.

Haha Python generation does not come out, font reasons, too lazy to change, just use Parsons, you can change the font yourself.

Code display "Module import"import iofrom PIL import Imageimport numpy as npimport requests"Typeface download" """Get a single Chinese character picture ch - Single Chinese character or English letter (only upper case supported) quality - Single word resolution, H-640 pixels, M-480 pixels, L-320 pixels """def get_word(ch, quality): 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))"""Get background image""def get_bg(quality): return get_word ('bg ', quality)"Generate Spring Festival couplet" """ text - Spring Festival couplet content, broken by spaces HorV - H-horizontal, V-vertical quality - Single word resolution, H-640 pixels, M-480 pixels, L-320 pixels out_file - output file name """def write_couplets(text, HorV='V', quality='L', out_file=None): 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 = 'high status, high position, high position,

vertical version

text = 'Red Phoenix presents auspicious dragon presents auspicious red peach congratulates new year apricot'write_couples (text, HorV ='V ', quality='M', out_file ='Fu Man Ren.jpg ')

Thank you for reading! About "how to use python to achieve a Spring Festival couplet" this article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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

*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