安装opencv库
pip install opencv
读取与保存图片必须为英文名称,不然会报错
import numpy as np
import cv2
def amplify(pict: str, multiple: int = 2):
"""
:param pict: 需要放大的图片
:param multiple: 放大的倍数
"""
imread = cv2.imread(pict) # 读取图片
x, y = len(imread[0, ...]), len(imread[..., 0]) # 获取图片长、宽的大小
nplist = np.zeros((y * multiple, x * multiple, 3), dtype=np.uint8) # 建立一个 原像素*倍数 的三维数组
for i in range(y):
for j in range(x):
pixel = imread[i, j] # 获取每个像素
nplist[i * multiple:(i + 1) * multiple, j * multiple:(j + 1) * multiple] = pixel # 根据切片替换当前遍历的像素
cv2.imwrite('pict.png', nplist) # 保存
amplify('mypict.png') # 调用函数