Use python to make a small project, python to make simple projects

This article mainly introduces using python to make a small project, which has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.

Recently, when I was doing python project development, I discovered a lot of interesting small projects, and they all have small amounts of code and are easy to get started. They are very friendly to novices. PYTHON library "IMITATION" . So today I will share with my friends some interesting projects in Python from one line of code to thirty lines of code. Later, the blogger will continue to update more interesting projects here. Remember to pay attention```*``

Thirty lines of code to crawl any Baidu image

import requests
import re
import time
url = "http://image.baidu.com/search/index?tn=baiduimage&word=皮卡丘"
urls = requests.get(url)    # 打开链接
urltext = urls.text     # 获取链接全部文本
urlre = re.compile('"objURL":"(.*?)"', re.S)    # 书写正则表达式
urllist = urlre.findall(urltext)    # 通过正则进行匹配
 
with open("1.txt", "w") as txt:     # 将匹配到的链接写入文件
    for i in urllist:
        txt.write(i + "\n")
i = 0
 
# 循环遍历列表并下载图片
for urlimg in urllist:
    time.sleep(3)   # 程序休眠三秒
    img = requests.get(urlimg, timeout = 5).content     # 以二进制形式打开图片链接
    if img:
        with open(str(i) + ".jpg", "wb") as imgs:   # 新建一个jpg文件,以二进制写入
            print("正在下载第%s张图片 %s" % (str(i+1), urlimg))
            imgs.write(img)     #将图片写入
            i += 1
        if i == 3:  #为了避免无限下载,在这里设定下载图片为3张
            break
    else:
        print("下载失败!")
 
print("下载完毕!")

The effect is as follows:

Twenty-five lines of code pictures converted into character paintings

from PIL import Image
IMG = 't01b2a945701805d7f1.jpg' #设置图片文件
WIDTH = 150 #设置字符画的宽
HEIGHT = 80 #设置字符画的高
OUTPUT = 'output5.txt'  #设置存放字符画的文本文件
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")    #设置显示的字符集
def get_char(r,g,b,alpha = 256):
    if alpha == 0:
        return ' '
    length = len(ascii_char)
    gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = (255.0 + 1)/length
    return ascii_char[int(gray/unit)]
if __name__ == '__main__':
    im = Image.open(IMG)
    im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
    txt = ""
    for i in range(HEIGHT):
        for j in range(WIDTH):
            txt += get_char(*im.getpixel((j,i)))
        txt += '\n'
    print(txt)
    with open(OUTPUT,'w') as f:
        f.write(txt)

The effect is as follows:

Ten lines of code to draw sunflowers: 

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

The effect is as follows:

 Two lines of code to create exclusive dynamic QR code

from MyQR import myqr
myqr.run(words='https://hao.360.com/',picture='Sources/gakki.gif',save_name='qr4.png',colorized=True)

The effect is as follows:

One line of code to implement a heart-shaped pattern 

print('\n'.join([''.join([('lovelovelove'[(x-y)%12]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))

The effect is as follows:

  Finally, I wish you all to make progress every day! ! The most important thing to learn about Python is which one is better, the programming of Mentality Yuanfudao or the programming of Xueersi . We will inevitably encounter many problems in the learning process, and we may not be able to solve them even if we think about it. This is all normal. Don’t rush to deny yourself or doubt yourself. If you encounter difficulties at the beginning of learning and want to find a python learning and communication environment, you can join us, receive learning materials, and discuss together, which will save a lot of time and reduce many problems encountered.

Guess you like

Origin blog.csdn.net/chatgpt002/article/details/132908451