1、Jpg转Png
示例代码:
# 图片格式转换, Jpg转Png
# 方法①
from PIL import Image
img = Image.open('demo.jpg')
img.save('demo_open.png')
# 方法②
from cv2 import imread, imwrite
image = imread("demo.jpg", 1)
imwrite("demo_imread.png", image)
2、获取电脑的配置信息
前置依赖: pip install wmi 使用Python的WMI模块,便可以轻松查看你的电脑信息。 示例代码:
# 获取计算机信息
import wmi
def System_spec():
Pc = wmi.WMI()
os_info = Pc.Win32_OperatingSystem()[0]
processor = Pc.Win32_Processor()[0]
Gpu = Pc.Win32_VideoController()[0]
os_name = os_info.Name.encode('utf-8').split(b'|')[0]
ram = float(os_info.TotalVisibleMemorySize) / 1048576
print(f'操作系统: {os_name}')
print(f'CPU: {processor.Name}')
print(f'内存: {ram} GB')
print(f'显卡: {Gpu.Name}')
print("\n计算机信息如上 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑")
System_spec()
效果:
3、PDF加密和解密
前置依赖: pip install pikepdf 如果多个PDF文件加密,使用Python的pikepdf模块,即可对文件进行加密,写一个循环就能进行批量加密文档。 示例代码:
# PDF加密
import pikepdf
pdf = pikepdf.open("demo.pdf")
pdf.save('encrypt.pdf', encryption=pikepdf.Encryption(owner="111111", user="111111", R=4))
pdf.close()
效果:
4、压缩、解压文件
示例代码:
import zipfile
# 压缩文件夹
with zipfile.ZipFile('archive.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.write('encrypt.pdf')
# 解压文件夹
unzip = zipfile.ZipFile("archive.zip", "r")
unzip.extractall("output Folder")
5、将图像转换为素描图
示例代码:
# 图像转换
import cv2
# 读取图片
img = cv2.imread("demo.jpg")
# 灰度
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
invert = cv2.bitwise_not(grey)
# 高斯滤波
blur_img = cv2.GaussianBlur(invert, (7, 7), 0)
inverse_blur = cv2.bitwise_not(blur_img)
sketch_img = cv2.divide(grey, inverse_blur, scale=256.0)
# 保存
cv2.imwrite('sketch.jpg', sketch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果:
6、获取CPU温度
有了这个Python脚本,从此告别鲁大师!
# 获取CPU温度
from time import sleep
from pyspectator.processor import Cpu
cpu = Cpu(monitoring_latency=1)
with cpu:
while True:
print(f'Temp: {cpu.temperature} °C')
sleep(2)
7、提取PDF表格
示例代码如下:
# 方法①
import camelot
tables = camelot.read_pdf("tables.pdf")
print(tables)
tables.export("extracted.csv", f="csv", compress=True)
# 方法②, 需要安装Java8
import tabula
tabula.read_pdf("tables.pdf", pages="all")
tabula.convert_into("table.pdf", "output.csv", output_format="csv", pages="all")
8、截图
示例代码如下:
# 方法①
from mss import mss
with mss() as screenshot:
screenshot.shot(output='scr1.png')
# 方法②
import PIL.ImageGrab
scr = PIL.ImageGrab.grab()
scr.save("scr2.png")
9、生成强密码
示例代码如下:
import random
import string
total = string.ascii_letters + string.digits + string.punctuation
length = 15
password = "".join(random.sample(total, length))
print(password)
10、Python词云
示例代码如下:
import jieba
import wordcloud
str = '我看过沙漠下暴雨,看过大海吻鲨鱼,' \
'看过黄昏追逐黎明,但是没有看过你'
w = wordcloud.WordCloud(
width=1000,
font_path='simhei.ttf',
height=700,
background_color='white'
)
w.generate(' '.join(jieba.lcut(str)))
w.to_file('奇妙能力.png')
题外话
感谢你能看到最后,给大家准备了一些福利!
感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。
👉优快云大礼包🎁:全网最全《Python学习资料》免费赠送🆓!(安全链接,放心点击)
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python兼职渠道推荐*
学的同时助你创收,每天花1-2小时兼职,轻松稿定生活费.
三、最新Python学习笔记
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
四、实战案例
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

若有侵权,请联系删除