-
Notifications
You must be signed in to change notification settings - Fork 2
/
Untitled.py
70 lines (45 loc) · 1.73 KB
/
Untitled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import time
from skimage import io,filters,measure,morphology,color
import os
import comtypes.client
app = comtypes.client.CreateObject('Photoshop.Application')
# In[ ]:
def copy_file_contents_to_clipboard(path):
doc = app.Open(path)
doc.layers[0].Copy()
doc.Close(2) # 2 (psDoNotSaveChanges)
def create_layer_from_file(layer_name, path_from,path_to):
copy_file_contents_to_clipboard(path_from)
app.activeDocument = app.Open(path_to)
layer = app.activeDocument.artLayers.add() # Create Layer
layer.name = layer_name # Rename Layer
app.activeDocument.activeLayer = layer # Select Layer
app.activeDocument.Paste() # Paste into
app.activeDocument.Close(1)
# In[ ]:
def delsmall( img0 ):
img =color.rgb2gray(img0)
thresh = filters.threshold_otsu(img) #用otsu算法确定最佳分割阈值
bwimg =(img>=(thresh)) #用阈值进行分割,生成二值图像
labels = measure.label(bwimg) #标记连通域
bw =morphology.closing(img < thresh, morphology.square(2))
#img1 = morphology.remove_small_objects(labels, min_size=5, connectivity=1)
img2= morphology.remove_small_objects(bw, min_size=200, connectivity=1)
return img2
#io.imshow(img1)
# In[ ]:
import glob as gb
str='C:\\Users\\jjp\\Desktop\\test\\*.png'
out='C:\\Users\\jjp\\Desktop\\out\\'
psd='C:\\Users\\jjp\\Desktop\\PSD\\'
img_path = gb.glob(str)
for path in img_path:
theimg =delsmall(io.imread(path))
io.imsave(out+path.split("\\")[-1],(1-theimg)*255)
print(psd+path.split("\\")[-1].split(".")[-2]+'.psd')
create_layer_from_file("new_layer",out+path.split("\\")[-1],psd+path.split("\\")[-1].split(".")[-2]+'.psd')
time.sleep( 5 )
# In[ ]: