簡體   English   中英

使用PIL在Python中進行圖像壓紋 - 增加深度,方位角等

[英]Image embossing in Python with PIL — adding depth, azimuth, etc

我正在嘗試使用PIL壓印圖像。

PIL提供了一種壓印圖像的基本方法(使用ImageFilter.EMBOSS )。

在像GIMP這樣的圖像編輯軟件包中,您可以更改此浮雕圖像中的方位角,深度和高程等參數。

如何用PIL做到這一點? 至少我想調整浮雕圖像的“深度”。

更新:我嘗試了Paul建議的東西(修改filterargs ,如scale, offset和矩陣),但我無法改變“深度”效果。 所以還在尋找答案。

這是使用PIL(左)和GIMP(右)的壓花效果的比較。 原始圖片位於http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node74.html

替代文字

如果您無法通過使用或組合操作來實現目標(例如旋轉,然后應用EMBOSS濾鏡,重新旋轉),(或增強對比度然后進行壓花),那么您可以采用更改(或創建自己的)濾鏡矩陣。

在ImageFilter.py中,你會發現這個類:

##
# Embossing filter.

class EMBOSS(BuiltinFilter):
    name = "Emboss"
    filterargs = (3, 3), 1, 128, (
        -1,  0,  0,
        0,  1,  0,
        0,  0,  0
        )

在矩陣的不同角落放置-1將改變方位角並使其為-2 可能具有您正在尋找的效果。

矩陣逐像素地應用。 矩陣中的每個元素對應於當前像素和周圍像素; 表示當前像素的中心值。 新的變換后的當前像素將被創建為所有9個像素的組合,由矩陣中的值加權。 例如,全部為零且中心為1的矩陣不會更改圖像。

其他參數是scaleoffset 對於內置EMBOSS,值為1(比例)和128(偏移)。 改變這些將改變結果的整體強度。

來自ImageFilter.py:

# @keyparam scale Scale factor.  If given, the result for each
#    pixel is divided by this value.  The default is the sum
#    of the kernel weights.
# @keyparam offset Offset.  If given, this value is added to the
#    result, after it has been divided by the scale factor.

由於我不熟悉GIMP的“深度”參數的影響,我不能說哪個最有可能做你想要的。

您還可以使矩陣具有不同的大小。 用(5,5)替換(3,3),然后創建25元素矩陣。

要在不重新保存源代碼的情況下對過濾器進行臨時更改,只需執行以下操作:

ImageFilter.EMBOSS.filterargs=((3, 3), 1, 128, (-1, 0, 0, 0, 1, 0, 0, 0, 0))

編輯:(采用NumPy方法)

from PIL import Image
import numpy

# defining azimuth, elevation, and depth
ele = numpy.pi/2.2 # radians
azi = numpy.pi/4.  # radians
dep = 10.          # (0-100)

# get a B&W version of the image
img = Image.open('daisy.jpg').convert('L') 
# get an array
a = numpy.asarray(img).astype('float')
# find the gradient
grad = numpy.gradient(a)
# (it is two arrays: grad_x and grad_y)
grad_x, grad_y = grad
# getting the unit incident ray
gd = numpy.cos(ele) # length of projection of ray on ground plane
dx = gd*numpy.cos(azi)
dy = gd*numpy.sin(azi)
dz = numpy.sin(ele)
# adjusting the gradient by the "depth" factor
# (I think this is how GIMP defines it)
grad_x = grad_x*dep/100.
grad_y = grad_y*dep/100.
# finding the unit normal vectors for the image
leng = numpy.sqrt(grad_x**2 + grad_y**2 + 1.)
uni_x = grad_x/leng
uni_y = grad_y/leng
uni_z = 1./leng
# take the dot product
a2 = 255*(dx*uni_x + dy*uni_y + dz*uni_z)
# avoid overflow
a2 = a2.clip(0,255)
# you must convert back to uint8 /before/ converting to an image
img2 = Image.fromarray(a2.astype('uint8')) 
img2.save('daisy2.png')

我希望這有幫助。 我現在可以看到為什么你對PIL的結果感到失望。 Wolfram Mathworld是矢量代數復習的一個很好的資源。

之前

替代文字

替代文字

要增加浮雕濾鏡的深度,請增加濾鏡蒙版的半徑。 深度較低:

h = [[1, 0, 0]
     [0, 0, 0]
     [0, 0, -1]]

與高深度:

h = [[1, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, 0]
     [0, 0, 0, 0, 0, 0, -1]]

要更改方位角,請將非零系數放在不同的角度:

h = [[0, 0, 1]
     [0, 0, 0]
     [-1, 0, 0]]

我不太確定海拔高度。 您可能需要更改非零系數值? 我只知道它需要是一個高通濾波器。

無論如何,要使用Scipy解決方案計算和顯示圖像:

import scipy.misc, scipy.signal
im = scipy.misc.imread(filename)
im_out = scipy.signal.convolve2d(im, h, 'same')
scipy.misc.imshow(im_out)

希望這可以幫助。

編輯:好的,正如Paul暗示PIL,你可以調整過濾器參數,甚至可以定義一個全新的內核。 比例和偏移參數與您要查找的內容無關。 過濾器的大小對於調整深度最重要。

經過進一步調查,PIL不允許您將過濾器大小更改為5x5以上。 似乎很奇怪。 因此,您不會像您預期的那樣獲得深度變化。

對於完全控制,您可能想要嘗試我前面提到過的Scipy解決方案。 將過濾器大小更改為荒謬的,如21x21,並查看它是否具有您想要的差異類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM