栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用PIL在Python中进行图像浮雕-添加深度,方位角等

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用PIL在Python中进行图像浮雕-添加深度,方位角等

如果您无法通过使用或组合操作(例如旋转,然后应用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的矩阵不会更改图像。

附加参数为

scale
offset
。对于内置的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的“深度”参数的影响,因此无法确定哪种方法最有可能满足您的要求。

您还可以使矩阵具有不同的大小。将(3,3)替换为(5,5),然后创建25个元素的矩阵。

要在不重新保存源代码的情况下对过滤器进行临时更改,只需执行以下操作:

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

编辑:(采用NumPy方法)

from PIL import Imageimport numpy# defining azimuth, elevation, and depthele = numpy.pi/2.2 # radiansazi = numpy.pi/4.  # radiansdep = 10.          # (0-100)# get a B&W version of the imageimg = Image.open('daisy.jpg').convert('L') # get an arraya = numpy.asarray(img).astype('float')# find the gradientgrad = numpy.gradient(a)# (it is two arrays: grad_x and grad_y)grad_x, grad_y = grad# getting the unit incident raygd = numpy.cos(ele) # length of projection of ray on ground planedx = 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 imageleng = numpy.sqrt(grad_x**2 + grad_y**2 + 1.)uni_x = grad_x/lenguni_y = grad_y/lenguni_z = 1./leng# take the dot producta2 = 255*(dx*uni_x + dy*uni_y + dz*uni_z)# avoid overflowa2 = a2.clip(0,255)# you must convert back to uint8 /before/ converting to an imageimg2 = Image.fromarray(a2.astype('uint8')) img2.save('daisy2.png')

我希望这有帮助。现在,我明白为什么您对PIL的结果感到失望了。Wolfram Mathworld是向量代数复习的好资源。

之前



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号