下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
#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':'',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,quality=arg['save_q']) #水印(这里仅为图片水印)def waterMark(**args): args_key = {'ori_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,'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 = 94dst_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,save_q=save_q)#水印#waterMark(ori_img=ori_img,mark_img=mark_img,water_opt=water_opt)
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图全部内容,希望文章能够帮你解决python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)