python高精度等比压缩图片,压缩的图片名称与原图片相同

from PIL import Image
from PIL import ImageFilter
import cv2
import time
import os
import numpy as np
“”“代码中是将图片压缩到400X400,可以根据自己的需求修改”“”
im = Image.new("RGB", (400, 400), "white") # 生成400X400的白色图片,可以根据自己的需求改变
imndarray = np.array(im)

path = "C:/Users/Administrator/Desktop/123/123" # 原图所在文件夹路径
path1 = "C:/Users/Administrator/Desktop/123" # 处理完图片的保存路径
filenames = os.listdir(path)

time1 = time.time()
for i in filenames:
    filename = os.path.join(path, i)
    filename1 = os.path.join(path1, i)
    #image = cv2.imdecode(np.fromfile(filename, dtype=np.uint8), -1)
    img = Image.open(filename, "r")
    image = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    #双三次插值
    height, width = image.shape[:2]  #获取原图像的水平方向尺寸和垂直方向尺寸。
    temp = max(height, width)
    multemp = temp/400
    if height > width:
        res = cv2.resize(image, (int(width / multemp), 400), interpolation=cv2.INTER_AREA)
    elif height < width:
        res = cv2.resize(image, (400, int(height / multemp)), interpolation=cv2.INTER_AREA)
    else:
        res = cv2.resize(image, (400, 400), interpolation=cv2.INTER_AREA)

    # 创建滤波器,使用不同的卷积核
    imgE = Image.fromarray(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
    gary2 = imgE.filter(ImageFilter.DETAIL)
    # #图像点运算
    gary3 = gary2.point(lambda i: i*0.9)
    img_convert_ndarray = cv2.cvtColor(np.asarray(gary3), cv2.COLOR_RGB2BGR)
    height1, width1 = img_convert_ndarray.shape[:2]
    temph = int((400 - height1)/2)
    tempw = int((400 - width1)/2)
    a = cv2.copyMakeBorder(img_convert_ndarray, temph, 400-temph-height1,tempw, 400-tempw-width1, cv2.BORDER_CONSTANT, value=[255, 255, 255])
    cv2.imencode('.png', a)[1].tofile(filename1)  # 保存图片
time2 = time.time()
print(u'总共耗时:' + str(time2 - time1) + 's')
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值