-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
54 lines (50 loc) · 1.47 KB
/
search.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
import itchat
import math
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import os
import shutil
# 获取到当前文件的目录,并检查是否有saveImg文件夹,如果不存在则自动新建saveImg文件
File_Path = os.getcwd()
result = os.listdir(File_Path)
if "saveImg" in result:
print('Directory already exists')
shutil.rmtree('./saveImg')
print('delete success')
os.makedirs('./saveImg')
else:
print('Directory not exists')
os.makedirs('./saveImg')
itchat.auto_login()
friends = itchat.get_friends(update=True)
user = friends[0]["UserName"]
num = 0
for i in friends:
img = itchat.get_head_img(userName=i["UserName"])
fileImage = open('./saveImg' + "/" + str(num) + ".jpg", 'wb')
fileImage.write(img)
fileImage.close()
num += 1
ls = os.listdir('./saveImg')
each_size = int(math.sqrt(float(640 * 640) / len(ls)))
lines = int(640 / each_size)
image = Image.new('RGB', (640, 640))
x = 0
y = 0
for i in range(0, len(ls) + 1):
try:
img = Image.open('./saveImg' + "/" + str(i) + ".jpg")
if img.mode != "RGB":
img = img.convert("RGB")
except IOError:
print(i)
print("Error")
else:
img = img.resize((each_size, each_size), Image.ANTIALIAS)
image.paste(img, (x * each_size, y * each_size))
x += 1
if x == lines:
x = 0
y += 1
image.save('./saveImg/' + 'all.jpg')
itchat.send_image('./saveImg/' + 'all.jpg', 'filehelper')