python(PIL)圖像處理(等比例壓縮、裁剪壓縮) 縮略(水印)圖

pykde 9年前發布 | 3K 次閱讀 Python

#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)

</pre>

 本文由用戶 pykde 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!