Ten‑Line Python Projects: QR Code, Word Cloud, Image Segmentation, Sentiment Analysis, Mask Detection, Message Spam, OCR, and a Simple Game
This article presents a series of concise Python examples—each under ten lines—demonstrating how to generate QR codes, create word clouds, perform image segmentation, conduct sentiment analysis, detect masks, automate message sending, extract text with OCR, and build a basic number‑guessing game, showcasing the versatility of Python for quick prototyping across AI and utility tasks.
Python’s concise syntax makes it popular among developers, enabling rapid creation of useful utilities with just a few lines of code. The following sections illustrate eight distinct projects, each implemented in ten lines or fewer.
1. QR Code Generation
QR codes (Quick Response codes) are widely used for encoding URLs and text. After installing the qrcode package, a short script creates and saves a QR image.
<code>pip install qrcode</code> <code>import qrcode
text = input('Enter text or URL:')
img = qrcode.make(text)
img.save('qr.png')
img.show()</code>For richer QR codes, the myqr library can add colors, logos, and background images.
<code>pip install myqr</code> <code>def gakki_code():
version, level, qr_name = myqr.run(
words='https://520mg.com/it/#/main/2',
version=1,
level='H',
picture='gakki.gif',
colorized=True,
contrast=1.0,
brightness=1.0,
save_name='gakki_code.gif',
save_dir=os.getcwd())
gakki_code()</code>2. Word Cloud Creation
A word cloud visualizes the frequency of keywords in a text. After installing wordcloud , jieba , and matplotlib , the script reads a local file, performs Chinese segmentation, and renders the cloud.
<code>pip install wordcloud
pip install jieba
pip install matplotlib</code> <code>import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
text = open('/Users/hecom/23tips.txt').read()
wordlist = jieba.cut(text, cut_all=True)
wl_space = ' '.join(wordlist)
my_wordcloud = WordCloud().generate(wl_space)
plt.imshow(my_wordcloud)
plt.axis('off')
plt.show()</code>3. Batch Image Segmentation (Background Removal)
Using Baidu’s PaddlePaddle deep‑learning framework, the paddlehub model deeplabv3p_xception65_humanseg removes backgrounds from a folder of images.
<code>python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple</code> <code>pip install -i https://mirror.baidu.com/pypi/simple paddlehub</code> <code>import os, paddlehub as hub
humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')
path = 'D:/CodeField/Workplace/PythonWorkplace/GrapImage/'
files = [path + i for i in os.listdir(path)]
results = humanseg.segmentation(data={'image': files})</code>4. Text Sentiment Analysis
Leveraging the senta_lstm model from PaddleHub, the script classifies a list of Chinese sentences as positive or negative.
<code>import paddlehub as hub
senta = hub.Module(name='senta_lstm')
sentence = ['你真美','你真丑','我好难过','我不开心','这个游戏好好玩','什么垃圾游戏']
results = senta.sentiment_classify(data={'text': sentence})
for result in results:
print(result)</code>5. Mask Detection
The pyramidbox_lite_mobile_mask model detects whether faces in images wear masks.
<code>import paddlehub as hub
module = hub.Module(name='pyramidbox_lite_mobile_mask')
image_list = ['face.jpg']
input_dict = {'image': image_list}
module.face_detection(data=input_dict)</code>6. Simple Message Spam (Keyboard Automation)
Using pynput , the script repeatedly types a message and presses Enter, simulating a rapid‑fire chat bot.
<code>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pynput</code> <code>from pynput import mouse, keyboard
import time
m_mouse = mouse.Controller()
m_keyboard = keyboard.Controller()
m_mouse.position = (850, 670)
m_mouse.click(mouse.Button.left)
while True:
m_keyboard.type('你好')
m_keyboard.press(keyboard.Key.enter)
m_keyboard.release(keyboard.Key.enter)
time.sleep(0.5)</code>7. OCR with Tesseract
The pytesseract wrapper reads text from an image file.
<code>import pytesseract
from PIL import Image
img = Image.open('text.jpg')
text = pytesseract.image_to_string(img)
print(text)</code>8. Simple Number‑Guessing Game
A classic console game where the user guesses a random number between 1 and 100.
<code>import random
print('1-100数字猜谜游戏!')
num = random.randint(1,100)
guess = None
i = 0
while guess != num:
i += 1
guess = int(input('请输入你猜的数字:'))
if guess == num:
print('恭喜,你猜对了!')
elif guess < num:
print('你猜的数小了...')
else:
print('你猜的数大了...')
print('你总共猜了%d 次' % i)
</code>These compact examples demonstrate how Python can quickly prototype a wide range of utilities, from AI‑driven image analysis to simple interactive games.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.