我在这里就不多说了,直接上代码:
https://my.oschina.net/neo600/blog/136393

How do I read image data from a URL in Python?

import os
import Image
fileName = 'c:/py/jb51.jpg'
fp = open(fileName,'rb')
im = Image.open(fp)
fp.close()
x,y = im.size
if x <300 or y < 300:
   os.remove(fileName)
from PIL import Image
import requests
import numpy as np
from StringIO import StringIOresponse = requests.get(url)
img = np.array(Image.open(StringIO(response.content)))
from PIL import Image
import urllib2im = Image.open(urllib2.urlopen(url))

or if you use requests:

from PIL import Image
import requestsim = Image.open(requests.get(url, stream=True).raw)


#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.sizewidthRatio = heightRatio = Noneratio = 1if (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_hif widthRatio and heightRatio:if widthRatio < heightRatio:ratio = widthRatioelse:ratio = heightRatioif widthRatio and not heightRatio:ratio = widthRatioif heightRatio and not widthRatio:ratio = heightRationewWidth = int(ori_w * ratio)newHeight = int(ori_h * ratio)else:newWidth = ori_wnewHeight = ori_him.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])'''image.ANTIALIAS还有如下值:NEAREST: use nearest neighbourBILINEAR: linear interpolation in a 2x2 environmentBICUBIC:cubic spline interpolation in a 4x4 environmentANTIALIAS: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.sizedst_scale = float(arg['dst_h']) / arg['dst_w'] #目标高宽比ori_scale = float(ori_h) / ori_w #原高宽比if ori_scale >= dst_scale:#过高width = ori_wheight = int(width*dst_scale)x = 0y = (ori_h - height) / 3else:#过宽height = ori_hwidth = int(height*dst_scale)x = (ori_w - width) / 2y = 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']) / widthnewWidth = 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.sizemark_im = image.open(arg['mark_img'])mark_w,mark_h = mark_im.sizeoption ={'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(pil)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图相关推荐

  1. python等比例压缩图片_python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图详解...

    #coding:utf-8 ''' python图片处理 @author:fc_lamp @blog:http://fc-lamp.blog.163.com/ ''' import Image as ...

  2. python PIL图像处理-框选

    框选图中位置 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random#---------------------- ...

  3. python PIL图像处理

    新建图像 # 三个参数分别代表图像的模式:常用的为RGB(3通道) .RGBA(4通道为透明通道,0为完全透明, 256为不透明) # 第二个参数为图像的长宽参数 # 第三个为默认的填充颜色,RGB时 ...

  4. Python:PIL图像处理库绘制国际象棋棋盘

    网页上搜索 "python绘制国际象棋棋盘",索引结果均为调用 turtle 库绘制棋盘结果:为了填充使用 python PIL 图像处理库绘制国际象棋棋盘的空白,今日分享此文. ...

  5. python图像等比例压缩_python使用pil进行图像处理(等比例压缩、裁剪)实例代码

    PIL中设计的几个基本概念 1.通道(bands):即使图像的波段数,RGB图像,灰度图像 以RGB图像为例: >>>from PIL import Image >>&g ...

  6. python压缩图片像素_python使用pil进行图像处理(等比例压缩、裁剪)实例代码

    PIL中设计的几个基本概念 1.通道(bands):即使图像的波段数,RGB图像,灰度图像 以RGB图像为例: 2.模式(mode):定义了图像的类型和像素的位宽.共计9种模式: 3.尺寸(size) ...

  7. Python PIL压缩二进制图像(PNG转JPG)

    文章目录 问题描述 解决方案 封装 参考文献 问题描述 压缩二进制图像(PNG转JPG) 1.png,69.7KB 解决方案 Image.save() 进 BytesIO 对象再读出来即可 from ...

  8. python自动裁剪图片_自动裁剪与Python / PIL的图像(Automatically cropping an ima

    谁能帮我弄清楚发生了什么事在我的图像自动裁剪脚本? 我有一个大的透明区域/空间PNG图像. 我想能够自动裁剪那个空间出来,剩下的要领. 原始图像具有正方形画布,最好这将是长方形的,只是封装分子. 这里 ...

  9. Python的图像处理库(OpenCV,PIL,matplotlib和scikit-image)

      目前接触过的python图像处理代码涉及到多种的图像库,其中最常用的当属opencv和PIL.惭愧的是,以前只是拿来用,却一直迷惑为什么不同的代码会选择不同的图像库.这些图像库的联系和区别又是什么 ...

最新文章

  1. 突发信号matlab,MATLAB信号处理仿真
  2. java jprofile安装与使用
  3. Python将被加入高考科目?你怎么看?
  4. 入住两年的CSDN,在今天2020年8月27日,成为CSDN博客专家
  5. 36个非常有趣的互动网站设计作品范例
  6. SDUTOJ2828_字典树
  7. The Trip On Abandoned Railway(线段树+树状数组)
  8. api网关 android,如何通过Android上的retrofit2使用Cognito Credentials调用API网关?
  9. 【项目管理】敏捷和计划驱动项目的特征对比
  10. 一线互联网公司的工程师们更应该增长技术以外的职场经验
  11. .net 4.0新特性-自旋锁(SpinLock)
  12. ssm心理咨询服务平台毕业设计源码324615
  13. 使用VGG16.npy文件载入权重
  14. 形式化验证工具——prism(1-安装)
  15. 12个免费在线Logo生成器
  16. AD软件系统属性配置
  17. 一款清爽的CSS表格样式
  18. 关于放大器失真的原因你了解多少呢?
  19. python空类型用什么表示_python空集合如何表示
  20. 基于模糊控制的Simulink仿真详解

热门文章

  1. 决策树算法python源代码_决的解释|决的意思|汉典“决”字的基本解释
  2. 一个简单的Java范例
  3. 写一个函数,要求输入一个字符串和一个字符长度,根据字符长度对该字符串进行分隔
  4. 【c语言】输入天数,求这天是全年的第几周的第几天
  5. 5/29 c的结构体变量
  6. 使用metasploit中Evasion模块
  7. 0、为什么推荐学习PowerShell?
  8. 静态方法调用注入对象(springMvc)
  9. phoenix 开发API系列(三)phoenix api 结合数据库
  10. 4 流程控制》4.5 比较for循环和while循环