# （1）彩照转黑白

```# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 03:45:16 2019

@author: czh
"""
%clear
%reset -f
# In[*]
from PIL import Image
import numpy as np
import os
os.chdir('D:\\train\\cv')
# In[*]

from PIL import Image
import numpy as np
a = np.asarray(Image.open("AWM.jpg").convert('L'))#.convert是变成黑白的

# In[*]
b=255-a#在对应的颜色通道减去他自己变成黑白底片的效果
im=Image.fromarray(b.astype('uint8'))
im
# In[*]

c=(100/255)*a+150#区间变换,颜色比较淡的灰度的图片
im=Image.fromarray(c.astype('uint8'))

im
# In[*]
d=255*(a/255)**2#像素平方,颜色比较深的图
im=Image.fromarray(d.astype('uint8'))
im```

image.png

image.png

image.png

# （2）图片转素描画

```# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 03:45:16 2019

@author: czh
"""
%clear
%reset -f
# In[*]
from PIL import Image
import numpy as np
import os
os.chdir('D:\\train\\cv')
# In[*]

from PIL import Image
import numpy as np
a = np.asarray(Image.open("StarryNight.jpg").convert('L'))#.convert是变成黑白的
Out[37]:
[array([[225. , 233. , 252. , ...,  18. ,  19. ,   2. ],
[  0.5, 127. ,   3.5, ...,   5. ,  18. ,  17. ],
[ 19.5,  10. , 126.5, ..., 103.5,   5.5,  29. ],
...,
[  0. ,   0.5,   1. , ..., 126.5, 126.5, 126.5],
[  4. ,   4.5,   5. , ...,   7. ,   7. ,   7. ],
[  9. ,  10. ,   9. , ...,  13. ,  13. ,  13. ]]),
array([[239. ,   0. ,  21. , ..., 113.5, 115.5, 243. ],
[247. ,  13.5,  29. , ...,  25.5, 107.5, 226. ],
[236. ,   3. ,  17. , ...,  62.5, 127.5, 241. ],
...,
[242. , 116.5, 126. , ...,   4. ,   5. ,   5. ],
[242. , 117.5, 127. , ...,   3. ,   5. ,   5. ],
[243. , 117.5, 127. , ...,   5. ,   5. ,   5. ]])]```

`grad_x, grad_y = grad`

```grad_x = grad_x*depth/100.

image.png

### 制造光源效果

```depth = 10                  # (0-100)
uni_z = 1./A

vec_el = np.pi/2.2                   # 光源的俯视角度，弧度值
vec_az = np.pi/4.                    # 光源的方位角度，弧度值
dx = np.cos(vec_el)*np.cos(vec_az)   #光源对x 轴的影响
dy = np.cos(vec_el)*np.sin(vec_az)   #光源对y 轴的影响
dz = np.sin(vec_el)              #光源对z 轴的影响

b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)     #光源归一化```

### 重构图像

`b = b.clip(0,255)`

`im = Image.fromarray(gd.astype('uint8'))`

```# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 03:45:16 2019

@author: czh
"""
%clear
%reset -f
# In[*]
from PIL import Image
import numpy as np
import os
os.chdir('D:\\train\\cv')
# In[*]

from PIL import Image
import numpy as np
a = np.asarray(Image.open("AWM.jpg").convert('L'))#.convert是变成黑白的

# In[*]
b=255-a#在对应的颜色通道减去他自己变成黑白底片的效果
im=Image.fromarray(b.astype('uint8'))
im
# In[*]

c=(100/255)*a+150#区间变换,颜色比较淡的灰度的图片
im=Image.fromarray(c.astype('uint8'))

im
# In[*]
d=255*(a/255)**2#像素平方,颜色比较深的图
im=Image.fromarray(d.astype('uint8'))
im

# In[*]
a = np.asarray(Image.open("AWM.jpg").convert('L')).astype('float')
# In[*]
depth = 10                  # (0-100)
uni_z = 1./A

vec_el = np.pi/2.2                   # 光源的俯视角度，弧度值
vec_az = np.pi/4.                    # 光源的方位角度，弧度值
dx = np.cos(vec_el)*np.cos(vec_az)   #光源对x 轴的影响
dy = np.cos(vec_el)*np.sin(vec_az)   #光源对y 轴的影响
dz = np.sin(vec_el)              #光源对z 轴的影响

b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)     #光源归一化
b = b.clip(0,255)

im = Image.fromarray(b.astype('uint8'))  #重构图像
im
# In[*]
im.save("手绘.jpg")```

205 篇文章39 人订阅

0 条评论