This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavatar.py
81 lines (62 loc) · 1.96 KB
/
avatar.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
import time
import json
import os
import requests
import urllib
import imutils
from vk_api import VkApi
import numpy as np
import cv2
# Загружает аватары друзей из вк
def load_avatars():
login, password = json.load(open('secret.json'))
vk = VkApi(login, password)
vk.auth()
target = 'annndruha'
members = []
try:
id = int(target.replace('id',''))
except:
id = vk.method('users.get', {'user_ids':target})[0]['id']
members = vk.method('friends.get', {'user_id':id})["items"]
print(len(members))
#print(members)
members_str = ','.join(map(str, members))
members_data = vk.method('users.get', {'user_ids':members_str, 'fields':'photo_400_orig'})
for data in members_data:
try:
url = data['photo_400_orig']
id = data['id']
filename = f'avatars/{id}.jpg'
response=requests.get(url)
if response.status_code==200:
with open(filename,'wb') as imgfile:
imgfile.write(response.content)
except:
pass
# Обрезает их до разрешения первого аватара
def crop(img):
n, m = img.shape[0], img.shape[1]
if n<m:
img = img[:, ((m-n)//2):n+((m-n)//2), : ]
else:
img = img[((n-m)//2):m+((n-m)//2), :, : ]
return img
# усредняет аватары
PATH = 'anime'
imagelist = os.listdir(PATH)
images = []
for i, img_name in enumerate(imagelist):
img_name = PATH +'/' + img_name
print(f'Open: {img_name}')
image = cv2.imread(img_name)
#image = crop(image)
#cv2.imwrite(img_name,image)
image = imutils.resize(image.copy(), width=256, height= 256) #height= 256
#if image.shape[1]==258: image=image[:,:256]
cv2.imwrite(img_name, image)
images.append(image/len(imagelist))
result = images[0]
for i in range(1,len(images)):
result += images[i]
cv2.imwrite(PATH+'/_result.png', result)