Python is a language that is very suitable for writing some practical small scripts and running automation, crawlers, algorithms, etc. It is very convenient.
This is also the fun part of learning Python for many people. It may only take a week to get started with the syntax, and then you can use third-party libraries to solve practical problems.
I have seen many Python code projects on Github. A few dozen lines of code can implement a scene function, which is very practical.
For example, there are many good and practical Python scripts in the warehouse Python-master. Here are a few simple examples:
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)
# 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)
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))
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()
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")
This project is just some small scripts used by the author in his daily work, which may also help you. Although the author is not a programmer, his habit of using code to solve problems will greatly improve efficiency and spur more innovative thinking. I think everyone can write such code, as long as you accumulate it slowly and practice more.
The above is the detailed content of Python-master, a collection of practical Python scripts!. For more information, please follow other related articles on the PHP Chinese website!