python 修改图片尺寸 留白_python等比压缩图片到固定大小,不足的留白
代码中为了切换图片的读取格式将图片保存后又读取了一遍,太过繁琐,需要改进的代码请前往
https://blog.csdn.net/lidongxx/article/details/92769009
from PIL import Image
from PIL import ImageFilter
import cv2
import time
import os
import numpy as np
im = Image.new("RGB", (400, 400), "white")
imndarray = np.array(im)
path = "C:/Users/Administrator/Desktop/E9-211-JPG/E9-JPG"
path1 = "C:/Users/Administrator/Desktop/newE9-JPG"
filenames = os.listdir(path)
time1 = time.time()
#读入图像
#resp = urllib.request.urlopen(url)
#image = np.asarray(bytearray(resp.read()), dtype="uint8")
#image = cv2.imdecode(image, cv2.IMREAD_COLOR)
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)
#双三次插值
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)
cv2.imwrite("C:/Users/Administrator/Desktop/temp/101.jpg", res)
imgE = Image.open("C:/Users/Administrator/Desktop/temp/101.jpg")
gary2 = imgE.filter(ImageFilter.DETAIL)
#图像点运算
gary3 = gary2.point(lambda i: i*0.9)
#savePath = (filename1)
img_convert_ndarray = np.array(gary3)
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('.jpg', a)[1].tofile(filename1) # 保存图片
time2 = time.time()
print (u'总共耗时:' + str(time2 - time1) + 's')
python 修改图片尺寸 留白_python等比压缩图片到固定大小,不足的留白相关推荐
- 在线修改图片尺寸缩放网站(完美解决图片过大无法上传问题)
在线修改图片尺寸缩放网站(完美解决图片过大无法上传问题) http://pic.sdodo.com/tool/picadjust/ http://www.zhengzong.cn/bbsxp/thre ...
- python炫酷动画源代码_Python tkinter实现的图片移动碰撞动画效果【附源码下载】...
本文实例讲述了Python tkinter实现的图片移动碰撞动画效果.分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: #!/usr/bin/python # -*- coding ...
- 如何使用python批量压缩图片_Python实现批量压缩图片
# -*- coding: utf-8 -*- """ __author__= 'Du' __creation_time__= '2018/1/5 10:06' &quo ...
- python修改y轴刻度_Python | Y轴刻度限制
python修改y轴刻度 In some cases, we need to visualize our data within some defined range rather than the ...
- python 识别登陆验证码图片(完整代码)_python 识别登录验证码图片功能的实现代码(完整代码)...
在编写自动化测试用例的时候,每次登录都需要输入验证码,后来想把让python自己识别图片里的验证码,不需要自己手动登陆,所以查了一下识别功能怎么实现,做一下笔记. 首选导入一些用到的库,re.Imag ...
- python修改文件的方法_python中文件操作实现全文或单行替换方法
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开.替换文本中的taste 为 tasting Yesterday when ...
- python修改自己的代码_python修改微信和支付宝步数的示例代码
项目意义 如果你想在支付宝蚂蚁森林收集很多能量种树,为环境绿化出一份力量,又或者是想每天称霸微信运动排行榜装逼,却不想出门走路,那么该python脚本可以帮你实现. 实现方法 手机安装第三方软件乐心健 ...
- python随机出现外星人飞船_python外星人入侵游戏如何改变飞船的大小
Python从入门到实践里的<外星人入侵> Python外星人入侵问题分享助世界上从来不缺少风景,缺少的只是欣赏的心情. Python项目外星人入侵添加爆照效果有没有原码<Pytho ...
- canva画图 图片居中裁剪_CSS3 object-fit:cover 剪裁图片尺寸适配div - 居中并裁剪图片...
今天来分享另一种最佳最完美的居中自动剪裁图片的功能,虽然IE8无法做到,但现在IE8还需要考虑吗? 放弃吧,直接使用 object-fit: cover; ! 你还记得通过设置图片的 backgro ...
最新文章
- mysql求每个订单的平均价_MySQL – 选择所有客户和每个客户的总订单和总价值
- 刻意练习:LeetCode实战 -- Task12. 合并K个排序链表
- linux命令学习(转)
- 阿里云系列——3.企业网站备案步骤---2018-1-4
- 第二个Spring冲刺周期团队进展报告
- 【深度学习】Win10安装TensorFlow_gpu(避坑必看)
- Thinkphp宅音乐html5在线音乐播放器
- 解决Git无法同步空文件夹的问题
- @Async异步功能
- union和union all区别
- Swagger入参为List
- java 内部邮件_java企业内部邮件email收发管理系统s2sh
- php office 在线预览,在线预览pdf和Office文档
- 听羊哥聊如何学习一门新技术
- php openssl 生成der,openssl asn.1 生成DER文件,把DER文件转换成内部数据结构
- 如何修改 / 校正Linux系统时间
- display 常用属性
- 浅析携程智能客服机器人实现
- 【What if 系列】拖住那架飞机!
- C\C++关键字---学习