-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.py
284 lines (258 loc) · 11.9 KB
/
prepare.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
from setting import *
import cv2
from PIL import Image
from extend import *
import shutil
from retrying import retry
import subprocess
def getmainimg():
girls = os.listdir(girl_path)
for girl in girls:
girlpath = os.path.join(girl_path,girl)
print(girlpath)
mainimgpath = os.path.join(girlpath,"mainimg")
if not os.path.isdir(mainimgpath):
os.mkdir(mainimgpath)
mainimgs = os.listdir(mainimgpath)
for onemainimg in mainimgs:
onemainimgpathold = os.path.join(mainimgpath,onemainimg)
# print(onemainimgpath)
onemainimgpathnew = onemainimgpathold.replace(' ','_')
onemainimgpathnew = onemainimgpathnew.replace('(','')
onemainimgpathnew = onemainimgpathnew.replace(')','')
os.rename(onemainimgpathold,onemainimgpathnew)
return_code = subprocess.run(['heif-enc', onemainimgpathnew])
print("return code:", return_code)
onemainimgpathnew = onemainimgpathnew.split('.')[0] + '.heic'
return girls
@retry
def getalbumncover(girl = None):
if girl == None:
sql = "select address, photocount,id from albumn where is_exist = 1"
else:
sql = "select address, photocount,id from albumn where girl = '%s' and is_exist = 1" % girl
# sql = "select address, photocount,id from albumn"
mycursor.execute(sql)
result = mycursor.fetchall()
print(result)
for albumn in result:
print(albumn)
allpath = os.path.join(girl_path,albumn[0])
if not os.path.exists(allpath):
continue
# print(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg'))
coverpath = os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg')
if os.path.isfile(coverpath):
print(True,albumn)
image = Image.open(coverpath)
if image.mode == "RGBA":
image = image.convert("RGB")
# 缩小图片尺寸
width, height = image.size
if width > 1066:
while width > 1066:
width = int(width * 0.5)
height = int(height * 0.5)
resized_image = image.resize((width, height))
# 保存缩小后的图片文件
resized_image.save(coverpath)
elif os.path.exists(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '1.jpg')):
if os.path.isfile(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '%s.jpg'%str(int(albumn[1])+1))):
shutil.copy(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '%s.jpg'%str(int(albumn[1])+1)),os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg'))
elif os.path.exists(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '%s.jpg'% albumn[1])) and os.path.getsize(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '%s.jpg'% albumn[1])) < os.path.getsize(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '1.jpg')):
shutil.copy(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '%s.jpg'% albumn[1]),os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg'))
else:
shutil.copy(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + '1.jpg'),os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg'))
def createthumbnail(girl = None):
if girl == None:
sql = "select address, photocount,id from albumn where is_exist = 1"
else:
sql = "select address, photocount,id from albumn where girl = '%s' and is_exist = 1" % girl
mycursor.execute(sql)
result = mycursor.fetchall()
print(result)
for albumn in result:
print(albumn)
albumn_path = os.path.join(girl_path,albumn[0])
thumbnail_albumn_path = os.path.join(girl_thumbnail_path,albumn[0])
if not os.path.exists(thumbnail_albumn_path):
os.makedirs(thumbnail_albumn_path)
if not os.path.exists(albumn_path):
continue
# print(os.path.join(allpath,albumn[0].split('/')[-1] + ' ' + 'cover.jpg'))
photos = os.listdir(albumn_path)
print(photos)
for photo in photos:
print(photo)
photopath = os.path.join(albumn_path,photo)
print(photopath)
if os.path.isdir(photopath) or 'mov' in photo.lower() or 'mp4' in photo.lower():
continue
thumbnail_photo_path = os.path.join(thumbnail_albumn_path,photo)
if not os.path.exists(thumbnail_photo_path):
image = Image.open(photopath)
if image.mode == "RGBA":
image = image.convert("RGB")
# 缩小图片尺寸
width, height = image.size
if width > 200:
while width > 200:
width = int(width * 0.5)
height = int(height * 0.5)
resized_image = image.resize((width, height))
# 保存缩小后的图片文件
# maybe a gif or animate jpeg
try:
resized_image.save(thumbnail_photo_path)
except:
pass
def createalbum(girl = None,albumn = None):
if girl == None:
girls = os.listdir(girl_path)
createthumbnail()
pass
else:
sql = "select * from girl where name = '%s' " % girl
# print(sql)
# exit()
mycursor.execute(sql)
result = mycursor.fetchall()
if not result:
sql = "insert into girl (name) values ('%s')" % (girl)
print(sql)
mycursor.execute(sql)
mydb.commit()
albumns = []
if albumn == None:
one_albumns = os.listdir(os.path.join(girl_path,girl))
one_albumns.sort()
print(one_albumns)
for albumn in one_albumns:
temppath = os.path.join(girl_path,girl,albumn)
if albumn[0] == '.' and not os.path.isdir(temppath):
os.remove(temppath)
# print(photo,'zzzzz')
elif albumn == '_gsdata_':
shutil.rmtree(temppath)
elif albumn == '.mainimg':
continue
else:
albumns.append(albumn)
else:
albumns.append(albumn)
print(albumns)
# exit()
for albumn in albumns:
albumpath = os.path.join(girl_path,girl,albumn)
print(albumpath)
address = albumpath.lstrip(girl_path)
print(address)
institution,date,serial_number,name, photocount = parsealbumn(albumn,girl)
print(institution,date,serial_number,name,photocount)
# exit()
sql = "select id from albumn where institution = '%s' and serial_number = '%s' and girl = '%s'" % (institution,serial_number,girl)
print(sql)
# exit()
mycursor.execute(sql)
result = mycursor.fetchall()
# print(result)
# exit()
if not result:
sql = "insert into albumn (institution,name,address,serial_number,girl,date,photocount,keywords,description)\
values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % \
(institution,name,address,serial_number,girl, date,photocount,"","")
print(sql)
# exit()
# print(date)
mycursor.execute(sql)
mydb.commit()
sql = "select id from albumn where institution = '%s' and serial_number = '%s' and girl = '%s'" % (institution,serial_number,girl)
# print(sql)
mycursor.execute(sql)
result = mycursor.fetchall()
albumn_id = result[0][0]
# print(albumn_id)
print("1111")
else:
# print(result)
albumn_id = result[0][0]
sql = "update albumn set photocount = %s where institution = '%s' and serial_number = '%s'"% (photocount,institution,int(serial_number))
print(sql)
mycursor.execute(sql)
mydb.commit()
# pass
# continue
# sql = "update albumn set name = '%s',address = '%s',serial_number ='%s',girl = '%s',date = '%s',photocount ='%s',\
# keywords ='%s',description ='%s'" %(name,address,serial_number,girl,date,photocount,"","")
# mycursor.execute(sql)
photos = os.listdir(albumpath)
print(photos)
# exit()
for photo in photos:
photopath = os.path.join(albumpath,photo)
print(photopath)
# exit()
if os.path.isdir(photopath):
print(photopath,'album, not a photo,have passed')
continue
if photo[0] == '.':
os.remove(photopath)
print(photo,'hiddenfile, have delete ')
continue
if photopath[-4:] == '.txt' or photopath[-4:] == '.url':
print(photopath,'other file, have delete ')
os.remove(photopath)
continue
if 'mov' in photo.lower() or 'mp4' in photo.lower():
continue
# if photopath[-4:] == '.jpg' or photopath[-4:] == '.png':
# print(photopath,'not heic, wait ')
# # os.remove(photopath)
# continue
serial_number,imagename = parsephoto(photo,albumn,girl)
if 'jpg' in photopath:
print(os.path.join(albumpath,imagename))
print('jpgjpg')
# exit()
os.rename(photopath,os.path.join(albumpath,imagename))
realimagepahth = os.path.join(albumpath,imagename)
return_code = subprocess.run(['heif-enc', realimagepahth])
print("return code:", return_code)
os.remove(realimagepahth)
address = realimagepahth.replace('jpg','heic').lstrip(girl_path)
# exit()
else:
address = photopath.lstrip(girl_path)
sql = "select id from image where name = '%s' and serial_number = %s and girl = '%s'" % (name,serial_number, girl)
print(sql)
# exit()
mycursor.execute(sql)
result = mycursor.fetchall()
print(result)
print(address)
# exit()
if not result:
sql = "insert into image (name,address,albumn_id,serial_number,girl,date,keywords,description)\
values ('%s','%s',%s,%s,'%s','%s','%s','%s')" % \
(name,address,albumn_id,serial_number,girl, date,"","")
print(sql)
print(date)
mycursor.execute(sql)
else:
print(result)
sql = "update image set address = '%s' where name = '%s' and serial_number = %s and girl = '%s' and albumn_id = %s" % (address,name,serial_number, girl,albumn_id)
# sql = "update image set albumn_id = %s where address = '%s' "% (albumn_id,address)
print(sql)
# exit()
mycursor.execute(sql)
mydb.commit()
# createthumbnail(girl)
if __name__ == "__main__":
mycursor = mydb.cursor()
# createalbum("发条少女")
createalbum('蠢沫沫')
# createalbum('蠢沫沫', '蠢沫沫 NO.004 艾米利亚 [46P-591MB]')
# getalbumncover()
# createthumbnail('日奈娇')
# createthumbnail('蠢沫沫')