溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python怎么實現(xiàn)PS圖像抽象畫風效果

發(fā)布時間:2021-03-23 10:55:25 來源:億速云 閱讀:229 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹了Python怎么實現(xiàn)PS圖像抽象畫風效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

介紹一種基于圖像分割和color map 隨機采樣生成一種抽象畫風的圖像特效,簡單來說,就是先生成一張 color map 圖,顏色是漸變的,然后針對要處理的圖像,進行分割,這里用的是 SLIC 分割算法,然后從 color map 中隨機采樣,將采樣得到的像素值賦予分割后的圖像區(qū)域。

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 20 08:31:04 2017
@author: shiyi
"""
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.segmentation import slic
import numpy.matlib
import random
file_name='D:/Visual Effects/PS Algorithm/9.jpg';
img=io.imread(file_name)
row, col, channel = img.shape
# define the colormap
color_map = img.copy()
rNW = 0.5
rNE = 1.0
rSW = 0.0
rSE = 0.5
gNW = 0.0
gNE = 0.5
gSW = 0.0
gSE = 1.0
bNW = 1.0
bNE = 0.0
bSW = 0.5
bSE = 0.0
xx = np.arange (col)
yy = np.arange (row)
x_mask = numpy.matlib.repmat (xx, row, 1)
y_mask = numpy.matlib.repmat (yy, col, 1)
y_mask = np.transpose(y_mask)
fx = x_mask * 1.0 / col
fy = y_mask * 1.0 / row
p = rNW + (rNE - rNW) * fx
q = rSW + (rSE - rSW) * fx
r = ( p + (q - p) * fy )
r[r<0] = 0
r[r>1] =1
p = gNW + (gNE - gNW) * fx
q = gSW + (gSE - gSW) * fx
g = ( p + (q - p) * fy )
g[g<0] = 0
g[g>1] =1
p = bNW + (bNE - bNW) * fx
q = bSW + (bSE - bSW) * fx
b = ( p + (q - p) * fy )
b[b<0] = 0.0
b[b>1] = 1.0
color_map[:, :, 0] = r * 255
color_map[:, :, 1] = g * 255
color_map[:, :, 2] = b * 255
# segment the image
N_block = 100
segments = slic(img, n_segments=N_block, compactness=10)
# plt.imshow(segments, plt.cm.gray)
seg_img = img.copy()
T_mask = img.copy()
for i in range(N_block):
 mask = (segments == i)
 T_mask[:, :, 0] = mask
 T_mask[:, :, 1] = mask
 T_mask[:, :, 2] = mask
 x_ind = int(random.random() * (col-1))
 y_ind = int(random.random() * (row-1))
 color = color_map[y_ind, x_ind, :]
 T_img = seg_img * T_mask
 T_img = color
 seg_img = seg_img * (1-T_mask) + T_img * T_mask
plt.figure(2)
plt.imshow(seg_img)
plt.show()

原圖:

Python怎么實現(xiàn)PS圖像抽象畫風效果

效果圖:

Python怎么實現(xiàn)PS圖像抽象畫風效果

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python怎么實現(xiàn)PS圖像抽象畫風效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI