Python-master,实用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)

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

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")
相关文章
|
15天前
|
存储 Shell 区块链
怎么把Python脚本打包成可执行程序?
该文档介绍了如何将Python脚本及其运行环境打包成EXE可执行文件,以便在不具备Python环境的计算机上运行。首先确保Python脚本能够正常运行,然后通过安装PyInstaller并使用`--onefile`参数将脚本打包成独立的EXE文件。此外,还提供了去除命令行窗口和指定可执行文件图标的详细方法。这些步骤帮助用户轻松地将Python程序分发给最终用户。
怎么把Python脚本打包成可执行程序?
|
3天前
|
存储 程序员 开发者
Python 编程入门:从零基础到编写实用脚本
【9月更文挑战第15天】本文是一篇面向初学者的Python编程入门指南,通过浅显易懂的语言和实际的代码示例,引导读者逐步掌握Python的基本概念、语法规则以及如何运用Python解决实际问题。文章不仅介绍了Python的基础知识点,还通过实例演示了如何将这些知识应用于日常编程任务中,帮助读者快速上手并能够独立编写简单的Python脚本。
|
3天前
|
监控 Ubuntu API
Python脚本监控Ubuntu系统进程内存的实现方式
通过这种方法,我们可以很容易地监控Ubuntu系统中进程的内存使用情况,对于性能分析和资源管理具有很大的帮助。这只是 `psutil`库功能的冰山一角,`psutil`还能够提供更多关于系统和进程的详细信息,强烈推荐进一步探索这个强大的库。
12 1
|
9天前
|
安全 JavaScript 前端开发
自动化测试的魔法:如何用Python编写你的第一个测试脚本
【8月更文挑战第41天】在软件的世界里,质量是王道。而自动化测试,就像是维护这个王国的骑士,确保我们的软件产品坚不可摧。本文将引导你进入自动化测试的奇妙世界,教你如何使用Python这把强大的魔法杖,编写出能够守护你代码安全的第一道防护咒语。让我们一起开启这场魔法之旅吧!
|
5天前
|
运维 监控 Linux
自动化运维的魔法:如何用Python脚本简化日常任务
【9月更文挑战第13天】在数字化时代的浪潮中,自动化运维如同一股清流,为IT团队带来了效率和灵活性的双重提升。本文将深入探讨如何通过Python脚本实现日常运维任务的自动化,从而释放双手,让重复性工作变得轻松愉快。从环境搭建到实际案例分析,我们将一步步揭开自动化运维的神秘面纱,让你的运维之路更加顺畅。
|
15天前
|
存储 Java 开发者
python脚本实现原理
【9月更文挑战第4天】python脚本实现原理
28 5
|
12天前
|
运维 监控 API
自动化运维:使用Python脚本进行日常管理
【9月更文挑战第6天】在现代的IT环境中,自动化运维已成为提升效率、减少人为错误的关键。本文将介绍如何通过Python脚本简化日常的运维任务,包括批量配置管理和日志分析。我们将从基础语法讲起,逐步深入到脚本的实际应用,旨在为读者提供一套完整的解决方案,以实现运维工作的自动化和优化。
14 1
|
16天前
|
运维 Linux 测试技术
自动化运维:使用Python脚本简化日常任务
【8月更文挑战第34天】在快节奏的IT环境中,自动化运维成为提升效率、降低错误率的关键。本文以Python脚本为例,展示如何通过编写简单的脚本来自动化日常运维任务,如批量更改文件权限、自动备份数据等。文章不仅提供代码示例,还探讨了自动化运维带来的益处和实施时应注意的问题。
|
16天前
|
运维 监控 网络安全
自动化运维:使用Python脚本简化日常任务
【8月更文挑战第33天】在本文中,我们将深入探讨如何通过Python脚本来自动化执行常见的运维任务。从基础的服务器健康检查到复杂的部署流程,Python因其简洁和功能强大的特性,成为自动化工具的首选。文章将展示编写Python脚本的基本方法,并通过实际示例演示如何应用于真实场景,旨在帮助读者提升效率,减少重复性工作。
|
5天前
|
敏捷开发 测试技术 持续交付
自动化测试之美:如何用Selenium和Python打造高效测试脚本
【9月更文挑战第13天】在软件开发的海洋中,自动化测试是那抹不可或缺的亮色。它不仅提升了测试效率,还保障了产品质量。本文将带你领略使用Selenium和Python构建自动化测试脚本的魅力所在,从环境的搭建到脚本的编写,再到问题的排查,每一步都是对软件质量把控的深刻理解和实践。让我们开始这段探索之旅,解锁自动化测试的秘密吧!
7 0