-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_pic_step1.py
129 lines (113 loc) · 4.26 KB
/
edit_pic_step1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#-*- coding : utf-8 -*-
import os
from PIL import Image
import shutil
import re
import numpy as np
def extract_picture(folder_path=os.getcwd(),picked_pic_folder='picked_pic'):
if not os.path.exists(picked_pic_folder):
os.mkdir(picked_pic_folder)
all_file=os.listdir(folder_path)
pic_info_list=[]
sum_pic_list=[]
beauty_count=0
for parent,dirfile,picture_list in os.walk(folder_path):
if parent.split('\\')[-1]==picked_pic_folder or \
len(picture_list)==0 or picture_list[0].split('.')[-1]!='jpg':
continue
beauty_count+=1
os.chdir(parent)
parent_path=os.path.dirname(os.getcwd())
beauty_name=parent_path.split('\\')[-1]
beauty_name= beauty_name.replace(' ','_')
pic_num=len(os.listdir(parent))
picked_pic_list=[]
for picture in picture_list:
#confirm picture is .jpg, if not ,then del it
if picture.split('.')[-1]!='jpg':
os.remove(picture)
continue
img=Image.open(picture)
if img.size[0]<img.size[1] and len(picked_pic_list)<4:
picked_pic_list.append(picture)
rename_list=[]
for picked_pic in picked_pic_list:
newname=str(beauty_name)+'__'+picked_pic
shutil.copy(picked_pic, os.path.join(folder_path,picked_pic_folder,newname))
rename_list.append(newname)
sum_pic_list.append(rename_list)
pic_info_list.append(parent.split('\\')[-1]+'['+str(pic_num)+'P'+str(pic_num*4)+'M]')
print "while extracting: done %s pictures" %(beauty_count)
os.chdir(folder_path)
return pic_info_list,sum_pic_list
def resize_pic(pic_folder,width_new=1000,cut_height=50):
os.chdir(pic_folder)
pic_list=os.listdir(pic_folder)
for picture in pic_list:
img=Image.open(picture)
(width,heigh)=img.size
height_new=heigh*width_new/width
out=img.resize((width_new,height_new),Image.ANTIALIAS)
out.crop((0,0,width_new,height_new-cut_height)).save(picture)
def create_html(pic_info,sum_pic,upload_path,yun_link):
"""create a templete html, and each created hmtl just copy and replace
arg:
pic_info: str info of beauty's , pic size pic num
sum_pic: list each name of 4 picture
upload_path: website path to store pictures
yun_link: tumple the fisrt element is baiduyun link,
the second element is code
"""
content=""" %s
<p>
<img src="%s%s" style="" title="%s"/>
</p>
<p>
<img src="%s%s" style="" title="%s"/>
</p>
<p>
<img src="%s%s" style="" title="%s"/>
</p>
<p>
<img src="%s%s" style="" title="%s"/>
</p>
<p>
<span style="color: #FF0000; font-size: 24px;">link:
</span>
<a href="%s" target="_blank"
style="font-size: 24px; text-decoration: underline;">
<span style="font-size: 24px;">%s
</span>
</a>
<span style="font-size: 24px;">
<span style="color: #FF0000; font-size: 24px;">code:
</span>
%s
</span>
</p>\n\n\n\n\n\n\n\n\n
"""%(pic_info,
upload_path,sum_pic[0],sum_pic[0],upload_path,sum_pic[1],sum_pic[1],
upload_path,sum_pic[2],sum_pic[2],upload_path,sum_pic[3],sum_pic[3],
yun_link[0],yun_link[0],yun_link[1])
with open('content.txt', 'a') as f:
f.write(content)
f.close()
def yun_link(file_name='yun.txt'):
yun_list=[]
with open(file_name,'r') as f:
lines=f.readlines()
for line in lines:
regular=re.findall(':(.*?) code: (.*)',line)
yun_list.append(regular[0])
f.close()
return yun_list
if __name__=='__main__':
folder_path=os.getcwd()
picked_pic_folder='picked_pic'
pic_info_list,sum_pic_list=extract_picture(folder_path,picked_pic_folder)
#save pic info
np.save('pic_info_list.npy',pic_info_list)
np.save('sum_pic_list.npy',sum_pic_list)
print "finished picking and summarizing info"
resize_pic(os.path.join(folder_path,picked_pic_folder),width_new=1000)
print "finished resizing pictures"