圖像處理PILLOW的使用

1.安裝html

pip install Pillow

2.使用python

1)圖片縮放windows

from PIL import Image
im = Image.open('dog.jpg') w,h = im.size #獲取圖像的尺寸 im.thumbnail((w/2,h/2)) #將圖像縮放50% #im.show() #顯示圖片 im.save('dog_thumbnail.jpg') #保存圖片

 

效果圖:dom

 

 2.圖片模糊字體

im = Image.open('dog.jpg')
im2 = im.filter(ImageFilter.BLUR)
im2.save('dog_blur.jpg')

效果圖:spa

3.生成驗證碼3d

from PIL import ImageDraw,ImageFont,ImageFilter
import random

#返回一個A-Z的隨機字母
def randomChar():
    return chr(random.randint(65,90))

#隨機顏色
def randomColor1():
    return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

#隨機顏色2
def randomColor2():
    return (random.randint(32,127),random.randint(32,127),random.randint(32,127))

width = 60 * 4
height = 60
#建立白色圖片
image = Image.new('RGB',(width,height),(255,255,255))
#建立font對象
#OSError: cannot open resource 須要指定字體庫的系統路徑
#font = ImageFont.truetype("Arial.ttf",36)
font = ImageFont.truetype("C:/windows/fonts/Arial.ttf",36)
#建立Draw對象,用於向白色背景圖片上繪圖
draw = ImageDraw.Draw(image)
#填充每個像素
for x in range(width):
    for y in range(height):
        draw.point((x,y),fill=randomColor1())

#輸出文字
for i in range(4):
    draw.text((60*i+10,10),randomChar(),font=font,fill=randomColor2())

#模糊
image = image.filter(ImageFilter.BLUR)
image.save("captcha.jpg")

 效果:code

 4.裁圖,旋轉,粘貼htm

from PIL import Image
im = Image.open('dog.jpg')
box = (100,50,350,250)  #要裁剪的矩形區域,座標以左上角爲原點
region = im.crop(box)  #返回裁剪到的圖片
#region.show()
region = region.transpose(Image.ROTATE_180) #對圖像進行旋轉
im.paste(region,box)  #將旋轉後的圖像粘回原圖
im.show()

效果圖:對象

5.添加圖片水印:

from PIL import Image

image = Image.open('dog.jpg')
logo = Image.open('logo.png')
logoW,logoH= logo.size
imageW,imageH = image.size
image.paste(logo,(imageW - logoW, imageH - logoH))
image.show()

效果:

6.添加透明文字水印:

參考: http://pythoncentral.io/watermark-images-python-2x/

def add_watermark(in_file, text,font,out_file='watermark.jpg', angle=23, opacity=0.25):
    '''
    :param in_file: 要添加水印的圖片
    :param text:  文字水印內容
    :param out_file: 添加水印後的圖片
    :param font:水印字體
    :param angle: 水印旋轉角度
    :param opacity: 水印透明度
    '''
    img = Image.open(in_file).convert('RGB')
    watermark = Image.new('RGBA', img.size, (0, 0, 0, 0))
    size = 2
    n_font = ImageFont.truetype(font, size)
    #getsize返回水印文字對應字體大小的寬度和高度
    n_width, n_height = n_font.getsize(text)
    #找到使得水印文字寬度最接近圖片寬度的字體大小
    while n_width + n_height < watermark.size[0]:
        size += 2
        n_font = ImageFont.truetype(font, size)
        n_width, n_height = n_font.getsize(text)
    draw = ImageDraw.Draw(watermark, 'RGBA')
    draw.text(((watermark.size[0] - n_width) / 2,
               (watermark.size[1] - n_height) / 2),
              text, font=n_font)
    watermark = watermark.rotate(angle, Image.BICUBIC)
    alpha = watermark.split()[3]
    #經過下降亮度和對比度來下降水印的透明度
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    #設置透明度
    watermark.putalpha(alpha)
    #添加水印
    Image.composite(watermark, img, watermark).save(out_file, 'JPEG')
   

FONT = 'C:/Windows/fonts/Arial.ttf'
add_watermark('dog.jpg','python',font=FONT)

效果圖:

相關資料:

https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

http://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html

相關文章
相關標籤/搜索