python等比例压缩图片_python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图详解...
#coding:utf-8
'''
python图片处理
@author:fc_lamp
@blog:http://fc-lamp.blog.163.com/
'''
import Image as image
#等比例压缩图片
def resizeImg(**args):
args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg = {}
for key in args_key:
if key in args:
arg[key] = args[key]
im = image.open(arg['ori_img'])
ori_w,ori_h = im.size
widthRatio = heightRatio = None
ratio = 1
if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']):
if arg['dst_w'] and ori_w > arg['dst_w']:
widthRatio = float(arg['dst_w']) / ori_w #正确获取小数的方式
if arg['dst_h'] and ori_h > arg['dst_h']:
heightRatio = float(arg['dst_h']) / ori_h
if widthRatio and heightRatio:
if widthRatio < heightRatio:
ratio = widthRatio
else:
ratio = heightRatio
if widthRatio and not heightRatio:
ratio = widthRatio
if heightRatio and not widthRatio:
ratio = heightRatio
newWidth = int(ori_w * ratio)
newHeight = int(ori_h * ratio)
else:
newWidth = ori_w
newHeight = ori_h
im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
'''
image.ANTIALIAS还有如下值:
NEAREST: use nearest neighbour
BILINEAR: linear interpolation in a 2x2 environment
BICUBIC:cubic spline interpolation in a 4x4 environment
ANTIALIAS:best down-sizing filter
'''
#裁剪压缩图片
def clipResizeImg(**args):
args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg = {}
for key in args_key:
if key in args:
arg[key] = args[key]
im = image.open(arg['ori_img'])
ori_w,ori_h = im.size
dst_scale = float(arg['dst_h']) / arg['dst_w'] #目标高宽比
ori_scale = float(ori_h) / ori_w #原高宽比
if ori_scale >= dst_scale:
#过高
width = ori_w
height = int(width*dst_scale)
x = 0
y = (ori_h - height) / 3
else:
#过宽
height = ori_h
width = int(height*dst_scale)
x = (ori_w - width) / 2
y = 0
#裁剪
box = (x,y,width+x,height+y)
#这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标
#所包围的图像,crop方法与php中的imagecopy方法大为不一样
newIm = im.crop(box)
im = None
#压缩
ratio = float(arg['dst_w']) / width
newWidth = int(width * ratio)
newHeight = int(height * ratio)
newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
#水印(这里仅为图片水印)
def waterMark(**args):
args_key = {'ori_img':'','dst_img':'','mark_img':'','water_opt':''}
arg = {}
for key in args_key:
if key in args:
arg[key] = args[key]
im = image.open(arg['ori_img'])
ori_w,ori_h = im.size
mark_im = image.open(arg['mark_img'])
mark_w,mark_h = mark_im.size
option ={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h),
'rightlow':(ori_w-mark_w,ori_h-mark_h)
}
im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA'))
im.save(arg['dst_img'])
#Demon
#源图片
ori_img = 'D:/tt.jpg'
#水印标
mark_img = 'D:/mark.png'
#水印位置(右下)
water_opt = 'rightlow'
#目标图片
dst_img = 'D:/python_2.jpg'
#目标图片大小
dst_w = 94
dst_h = 94
#保存的图片质量
save_q = 35
#裁剪压缩
clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q = save_q)
#等比例压缩
#resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)
#水印
#waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)
python等比例压缩图片_python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图详解...相关推荐
- python(pil)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图
我在这里就不多说了,直接上代码: https://my.oschina.net/neo600/blog/136393 How do I read image data from a URL in Py ...
- python微信自动发消息_Python使用微信itchat接口实现查看自己微信的信息功能详解...
Python使用微信itchat接口实现查看自己微信的信息功能详解 发布时间:2020-09-09 04:20:11 来源:脚本之家 阅读:134 本文实例讲述了Python使用微信itchat接口实 ...
- 爬虫技术python爬到女性语音_python爬虫看看虎牙女主播中谁最“顶”步骤详解
网页链接:https://www.huya.com/g/4079 这里的主要步骤其实还是和我们之前分析的一样,如下图所示: 这里再简单带大家看一下就行,重点是我们的第二部分. 既然网页结构我们已经分析 ...
- python的窗口处理模块_Python tkinter模块弹出窗口及传值回到主窗口操作详解
本文实例讲述了Python tkinter模块弹出窗口及传值回到主窗口操作.分享给大家供大家参考,具体如下: 有些时候,我们需要使用弹出窗口,对程序的运行参数进行设置.有两种选择 一.标准窗口 如果只 ...
- python diango 增删改查_python中关于django对数据库Mysql的增删改查操作详解
下面小编就为大家带来一篇python django 增删改查操作 数据库Mysql.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 下面介绍一下django增删改查操作: ...
- java压缩图片比例quality_Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】...
本文实例讲述了java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...
- [Python图像处理] 三十三.图像各种特效处理及原理万字详解(毛玻璃、浮雕、素描、怀旧、流年、滤镜等)...
此文转载自:https://blog.csdn.net/Eastmount/article/details/111568397#commentBox 该系列文章是讲解Python OpenCV图像处理 ...
- python变量命名规则思维导图_Python思维导图详解
Python思维导图 Python思维导图详解 第一阶段:学习Python基础语法,主要学习变量的使用以及类型.变量的计算和输入输出.变量的命名.运算符.if判断语句.while循环语句.字符串.常量 ...
- python二维图颜色函数_Python绘图之二维图与三维图详解
各位工程师累了吗? 推荐一篇可以让你技术能力达到出神入化的网站"持久男" 1.二维绘图 a. 一维数据集 用 Numpy ndarray 作为数据传入 ply 1. import ...
最新文章
- java中缀表达式转后缀表达式_数据结构Java实现06----中缀表达式转换为后缀表达式...
- 从大象安全套到超级表格
- java对于数组的定义_Java中方法的定义与使用,以及对数组的初步了解。
- Azure实践之automation自动整理资产信息
- 不同项目配置不同的 Git 账号
- redis 启动_Redis介绍amp;安装amp;启动
- python的垃圾处理机制_Python语法入门之垃圾回收机制
- ASA IPSEC ***配置
- 【刷题】BZOJ 2125 最短路
- Unity3D之Shader自定义编辑器功能拓展
- C++多态的基本语法与原理剖析
- 偶尔所得代码片(进程和锁相关)
- gdt描述_GDT(Global Descriptor Table)全局描述符表
- Java语言实现查找两个字符串的最大公共字串
- c#的is,as,强制类型问题
- How to create a DXL attribute using a DXL script
- Odin Inspector 系列教程 --- 初识Odin序列化
- 机器学习入门算法及其java实现-EM(Expectation Maxium)算法
- congestion解决方案
- php 中文 无法写入 mysql_php数据库不能存入中文