python實現圖片操作

前段時間在知乎看到大神用Ruby做的下面這樣一張圖,覺得非常有意思,所以打算用python實現一下

python有圖像處理標準庫PIL(Python Imaging Library),功能強大,簡單易用,可惜只支持2.x,Pillow是在PIL基礎上創建的兼容版本,支持python 3.x,同時增加了一些新特性。

安裝Pillow

直接在命令行使用pip安裝,十分方便

pip install Pillow

操作圖像

首先要根據需要引入對應的包(Image,ImageColor,ImageFont,ImageFilter等)

from PIL import Image

打開圖片

image = Image.open(url)

獲取圖片大小(返回一個元祖,分別爲寬和高)

width, height = image.size

保存圖片

image.save(url)

顯示圖片

image.show()

新建圖像

newImage = Image.new(mode, (width, height), color)

裁剪圖像

cropedImage = image.crop((0, 0, 100, 100))

複製圖像(產生原圖像副本,對其操作不影響原圖像)

copyImage = image.copy()

粘貼(對調用的圖像對象直接修改)

copyImage.paste(image, (x, y))

調整大小

resizedIm = image.resize((width, height))

旋轉(逆時針,返回旋轉後圖像,原圖像不變)

rotateIm = image.rotate(90)

翻轉

image.transpose(Image.FLIP_TOP_LEFT_RIGTH) #水平翻轉
image.transpose(Image.FLIP_TOP_TOP_BOTTOM) #垂直翻轉

獲取像素值

image.getpixel((x,y))

寫像素值

image.putpixel((x,y),(r,g,b))

獲取alpha

image.getalpha((x,y))

寫alpha

image.putalpha((x,y), alpha)

圖像複合
1、將img2符合到img1上,返回Image圖像(要求size相同,切mode都必須爲RGBA)

img3 = Image.alpha_composite(img1, img2)

2、out = img1*(1-alpha)+img2*alpha

img3 = Image.blend(img1, img2, alpha)

3、

img3 = Image.composite(img1, img2, mask)

圖像過濾
需要引入ImageFilter包

from PIL import Image, ImageFilter

高斯模糊

im.filter(ImageFilter.GaussianBlur)

普通模糊

im.filter(ImageFilter.BLUR)

邊緣增強

im.filter(ImageFilter.EDGE_ENHANCE)

找到邊緣

im.filter(ImageFilter.FIND_EDGES)

浮雕

im.filter(ImageFilter.EMBOSS)

輪廓

im.filter(ImageFilter.CONTOUR)

銳化

im.filter(ImageFilter.SHARPEN)

平滑

im.filter(ImageFilter.SMOOTH)

細節

im.filter(ImageFilter.DETAIL)

Demo

# -*- coding: utf-8 -*-
"""
    將n*n張圖片拼接在一起,並覆蓋上一張降低透明度的圖片
    Copyright: 2018/01/27 by 邵明山
"""

import os
from PIL import Image


def makedir(path):
    if not os.path.exists(path):
        os.mkdir(path)
        print("The directory \"" + path + "\" creates success.")
    else:
        print("The directory \"" + path + "\" has already existed.")


base_path = "base"
mask_path = "mask"
makedir(base_path)
makedir(mask_path)


n = int(input("Please enter n, n^2 is the number of the pictures."))
print("Please put n^2 pictures to the forder \"base\".")
print("Please put one picture to the forder \"mask\".")
os.system("pause")
width = int(input("Please enter the width of the final picture."))
height = int(input("Please enter the height of the final picture."))
transparency = int(input("Please enter the transparency% of the mask."))

base_pictures = []
for root, dirs, files in os.walk(os.getcwd()+"\\"+base_path):
    for file in files:
        base_pictures.append(os.path.join(root, file))

num = 0
base = Image.new("RGBA", (width, height))
for i in range(0, n):

    for j in range(0, n):
        picture = Image.open(base_pictures[num])
        temp = picture.resize((int(width/n), int(height/n)))
        location = (int(i * width / n), int(j * height / n))
        base.paste(temp, location)
        num += 1

    if num >= len(base_pictures):
        break

    if num >= n*n:
        break

for root, files in os.walk(os.getcwd()+"\\"+mask_path):
    for file in files:
        mask_picture = Image.open(mask_path + "\\" + file)

mask = mask_picture.resize((width, height))
mask.putalpha(int(transparency/100 * 255))

result = Image.alpha_composite(base, mask)
result.save("result.png")

運行結果

這裏寫圖片描述

源代碼

https://github.com/Caesar233/PhotoMixer

發佈了56 篇原創文章 · 獲贊 118 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章