Python-master,实用Python脚本合集!

开发 前端
我在Github上就看到过不少Python代码的项目,几十行代码就能实现一个场景功能,非常实用。比方说仓库Python-master里就有很多不错的实用Python脚本,下面,我们举几个简单例子。

Python这门语言很适合用来写些实用的小脚本,跑个自动化、爬虫、算法什么的,非常方便。

这也是很多人学习Python的乐趣所在,可能只需要花个礼拜入门语法,就能用第三方库去解决实际问题。

我在Github上就看到过不少Python代码的项目,几十行代码就能实现一个场景功能,非常实用。

比方说仓库Python-master里就有很多不错的实用Python脚本,举几个简单例子:

1. 创建二维码

import pyqrcode
import png
from pyqrcode import QRCode

# Text which is to be converted to QR code
print("Enter text to convert")
s = input(": ")
# Name of QR code png file
print("Enter image name to save")
n = input(": ")
# Adding extension as .pnf
d = n + ".png"
# Creating QR code
url = pyqrcode.create(s)
# Saving QR code as  a png file
url.show()
url.png(d, scale=6)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

2. 从图片中截取文字

# extract text from a img and its coordinates using the pytesseract module
import cv2
import pytesseract

# You need to add tesseract binary dependency to system variable for this to work

img = cv2.imread("img.png")
# We need to convert the img into RGB format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

hI, wI, k = img.shape
print(pytesseract.image_to_string(img))
boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
    b = b.split(" ")
    x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
    cv2.rectangle(img, (x, hI - y), (w, hI - h), (0, 0, 255), 0.2)

cv2.imshow("img", img)
cv2.waitKey(0)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

3. 判断闰年

def is_leap(year):
    leap = False
    if year % 4 == 0:
        leap = True
        if year % 100 == 0:
            leap = False
            if year % 400 == 0:
                leap = True
    return leap

year = int(input("Enter the year here: "))
print(is_leap(year))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

4. 简易日历

from tkinter import *
import calendar

root = Tk()
# root.geometry("400x300")
root.title("Calendar")

# Function

def text():
    month_int = int(month.get())
    year_int = int(year.get())
    cal = calendar.month(year_int, month_int)
    textfield.delete(0.0, END)
    textfield.insert(INSERT, cal)

# Creating Labels
label1 = Label(root, text="Month:")
label1.grid(row=0, column=0)

label2 = Label(root, text="Year:")
label2.grid(row=0, column=1)

# Creating spinbox
month = Spinbox(root, from_=1, to=12, width=8)
month.grid(row=1, column=0, padx=5)

year = Spinbox(root, from_=2000, to=2100, width=10)
year.grid(row=1, column=1, padx=10)

# Creating Button
button = Button(root, text="Go", command=text)
button.grid(row=1, column=2, padx=10)

# Creating Textfield
textfield = Text(root, width=25, height=10, fg="red")
textfield.grid(row=2, columnspan=2)

root.mainloop()
  • 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.

图片

5. 打印图片分辨率

def jpeg_res(filename):
   """"This function prints the resolution of the jpeg image file passed into it"""

   # open image for reading in binary mode
   with open(filename,'rb') as img_file:

       # height of image (in 2 bytes) is at 164th position
       img_file.seek(163)

       # read the 2 bytes
       a = img_file.read(2)

       # calculate height
       height = (a[0] << 8) + a[1]

       # next 2 bytes is width
       a = img_file.read(2)

       # calculate width
       width = (a[0] << 8) + a[1]

   print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

这个项目只是作者平时工作用到的一些小脚本,可能也会帮助到你。作者虽然不是程序员,但他这种用代码解决问题的习惯会极大的提升效率,也会迸发出更多的创新思维。我觉得这样的代码每个人都可以写出来,只要慢慢积累多练习就可以。

责任编辑:赵宁宁 来源: Python大数据分析
相关推荐

2019-10-09 16:08:21

PythonPython教程Python 开发

2024-11-13 13:14:38

2013-04-03 14:58:43

Android学习笔记实用代码合集

2021-07-27 15:20:47

PythonPDF文件

2024-02-23 18:17:57

Python脚本开发

2021-07-05 15:35:47

Vue前端代码

2013-11-13 10:49:50

2024-09-14 09:26:17

Python网络编程

2019-01-11 18:36:54

开发者技能代码

2022-05-07 14:08:42

Python自动化脚本

2022-07-21 14:38:17

PythonShell

2025-04-02 08:20:00

Python自动化文件管理脚本模板

2020-12-16 10:11:50

开发技能工具

2020-08-21 10:25:02

PythonWget

2020-09-14 11:10:43

Python代码

2022-06-24 10:02:43

PythonShell脚本

2017-04-26 09:00:23

Python发送邮件脚本

2021-11-30 07:01:19

Python自动化脚本

2011-08-08 10:10:21

Linux备份

2020-08-31 08:25:06

Python时间模块开发
点赞
收藏

51CTO技术栈公众号