>  기사  >  백엔드 개발  >  실용적인 Python 스크립트 모음, Python-master!

실용적인 Python 스크립트 모음, Python-master!

PHPz
PHPz앞으로
2023-04-11 17:04:031450검색

Python은 실용적인 작은 스크립트를 작성하고 자동화, 크롤러, 알고리즘 등을 실행하는 데 매우 적합한 언어입니다. 매우 편리합니다.

이것은 또한 많은 사람들에게 Python을 배우는 즐거움이기도 합니다. 구문을 시작하는 데 일주일 밖에 걸리지 않으며, 타사 라이브러리를 사용하여 실용적인 문제를 해결할 수 있습니다.

Github에서 많은 Python 코드 프로젝트를 보았습니다. 수십 줄의 코드로 장면 기능을 구현할 수 있는데 이는 매우 실용적입니다.

실용적인 Python 스크립트 모음, Python-master!

예를 들어 Python-master 웨어하우스에는 훌륭하고 실용적인 Python 스크립트가 많이 있습니다. 다음은 몇 가지 간단한 예입니다.

1. QR 코드 만들기

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 asa png file
url.show()
url.png(d, scale=6)

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)

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))

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()

실용적인 Python 스크립트 모음, Python-master!

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")

이 프로젝트는 작가가 일상 작업에서 사용하는 몇 가지 작은 스크립트일 뿐이며 도움이 될 수도 있습니다. 저자는 프로그래머는 아니지만 문제를 해결하기 위해 코드를 사용하는 그의 습관은 효율성을 크게 향상시키고 더 혁신적인 사고를 촉진할 것입니다. 천천히 쌓고 연습만 하면 누구나 이런 코드를 작성할 수 있다고 생각합니다.

위 내용은 실용적인 Python 스크립트 모음, Python-master!의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 51cto.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제