ホームページ  >  記事  >  バックエンド開発  >  実践的なPythonスクリプト集「Python-master」!

実践的なPythonスクリプト集「Python-master」!

PHPz
PHPz転載
2023-04-11 17:04:031450ブラウズ

Python は、実用的な小さなスクリプトを作成し、自動化、クローラー、アルゴリズムなどを実行するのに非常に適した言語です。非常に便利です。

これは、多くの人にとって Python 学習の楽しい部分でもあります。構文を使い始めるのに 1 週​​間しかかからない場合があり、その後はサードパーティのライブラリを使用して実際的な問題を解決できます。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事は51cto.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。