import cv2
import numpy as np
from PIL import Image as ImagePIL, ImageFont, ImageDraw
from PIL import Image
import os
def change_dpi(img_name, min_width=1280, min_height=1280):
"""
单个图片不足1280*1280
:param img_name:
:return:
"""
print(img_name)
try:
im = cv2.imdecode(np.fromfile(img_name, dtype=np.uint8), -1)
size = im.size
except Exception as e:
os.remove(img_name)
print("删除", img_name)
print(img_name)
return
image = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) # 格式转换,bgr转rgb
width, height = image.size
min_fenbianlv_chengji = min_width * min_height
org_fenbianlv_chengji = width * height
if min_fenbianlv_chengji > org_fenbianlv_chengji:
multiple = (min_fenbianlv_chengji / org_fenbianlv_chengji)**0.5
new_img = image.resize((int(width*multiple), int(multiple*height)), Image.ANTIALIAS)
new_img.save(img_name)
print(img_name, "调节成功")
# new_dst_path=os.path.join(r"screen已调分辨率",os.path.basename(img_name))
# shutil.move(img_name,new_dst_path)
if __name__ == '__main__':
path=r"D:\electronic"
for root,dirs,files in os.walk(path):
for file in files:
file_path=os.path.join(root,file)
print(file_path)
change_dpi(file_path)
图像处理-批量调整图片的分辨率
最新推荐文章于 2023-12-01 18:57:58 发布