Created
April 7, 2023 06:49
-
-
Save hu-qi/a1d67d77d30196bbe20f341db269edab to your computer and use it in GitHub Desktop.
Generate a photo gallery of your favorite stars.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import requests | |
import tqdm | |
import time | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', | |
"Cookie": "BDqhfp=%E5%88%98%E4%BA%A6%E8%8F%B2%26%26NaN-1undefined%26%268976%26%2612; BIDUPSID=35317AAD04D05FDEDB9BACFC678F5849; PSTM=1657272836; BAIDUID=35317AAD04D05FDEDB9BACFC678F5849:SL=0:NR=50:FG=1; H_WISE_SIDS=110085_189755_194530_204912_209568_210322_211435_211986_212295_213039_214798_215730_216212_216843_216942_218549_219248_219404_219473_219581_219623_219943_219946_220017_220071_220610_220663_220864_221120_221410_221479_221502_222298_222397_222522_222625_223064_223211_223374_223683_224055_224436_224457_224633_224877_225636_225859_226014_226026_226088_226102_226294_226547_226578_226598_226627_226722_226755_226936_226946_227062_227063_227066_227155_227187_227241_227264_227378_227489_227515_227580_227591_227613_227657_227732_227746_227864_227896_227920_227932_227971_228132_228139_228575_228644_228843_228933_229023_229027_229125_229155_229166_229268_8000063_8000120_8000141_8000150_8000157_8000163_8000178_8000180_8000183_8000186_8000188; H_WISE_SIDS_BFESS=110085_189755_194530_204912_209568_210322_211435_211986_212295_213039_214798_215730_216212_216843_216942_218549_219248_219404_219473_219581_219623_219943_219946_220017_220071_220610_220663_220864_221120_221410_221479_221502_222298_222397_222522_222625_223064_223211_223374_223683_224055_224436_224457_224633_224877_225636_225859_226014_226026_226088_226102_226294_226547_226578_226598_226627_226722_226755_226936_226946_227062_227063_227066_227155_227187_227241_227264_227378_227489_227515_227580_227591_227613_227657_227732_227746_227864_227896_227920_227932_227971_228132_228139_228575_228644_228843_228933_229023_229027_229125_229155_229166_229268_8000063_8000120_8000141_8000150_8000157_8000163_8000178_8000180_8000183_8000186_8000188; MCITY=-257%3A; newlogin=1; BDUSS_BFESS=9BNkxDNlhtYXJJTmZldzZBVkpMUG9wbXNHaVJvNGlBTVcwSWFucTdmb0lOVXRrSUFBQUFBJCQAAAAAAAAAAAEAAABMbsdyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAioI2QIqCNkVU; H_PS_PSSID=38185_36545_38470_38441_38397_38468_38289_38380_37934_26350_38420_38283_37881; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=35317AAD04D05FDEDB9BACFC678F5849:SL=0:NR=50:FG=1; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; userFrom=null; ab_sr=1.0.1_MzAwOWZiMjhlYzIzMjhkNjg2YTlhM2NhYjgwMmQxZTYzODRhODdkYTJmOGQwMjQ0ZWE0ZThjMjg3OTZlNjkxNjE0ODUzMDMyZDMzOWJmNjBmZTc4MzEwNmVlOGUwNjM2MTJlOWVmOWYyMTYyMmRlZWViMGQxNGYzZGFlYTMxMzhkM2RlZTg3ODlkOTgzZTUxNTUzOTNjNzM2Njc4OTQxNw==; firstShowTip=1" | |
} | |
def getImg(url,idx,path): | |
img=requests.get(url,headers=headers) | |
file=open(path+str(idx)+'.jpg','wb') | |
file.write(img.content) | |
file.close() | |
def remove_escape_sequences(string): | |
return string.encode('utf-8').decode('unicode_escape') | |
search=input("请输入搜索内容:") | |
if search == "": | |
search = "刘亦菲" | |
number=int(input("请输入需求数量:")) | |
if number == 0: | |
number=1000 | |
path='images/'+search+'/' | |
if not os.path.exists(path): | |
os.makedirs(path) | |
bar=tqdm.tqdm(total=number) | |
page=0 | |
while(True): | |
if number==0: | |
break | |
url = 'https://image.baidu.com/search/acjson' | |
params={ | |
"tn": "resultjson_com", | |
"logid": "11555092689241190059", | |
"ipn": "rj", | |
"ct": "201326592", | |
"is": "", | |
"fp": "result", | |
"queryWord": search, | |
"cl": "2", | |
"lm": "-1", | |
"ie": "utf-8", | |
"oe": "utf-8", | |
"adpicid": "", | |
"st": "-1", | |
"z": "", | |
"ic": "0", | |
"hd": "", | |
"latest": "", | |
"copyright": "", | |
"word": search, | |
"s": "", | |
"se": "", | |
"tab": "", | |
"width": "", | |
"height": "", | |
"face": "0", | |
"istype": "2", | |
"qc": "", | |
"nc": "1", | |
"fr": "", | |
"expermode": "", | |
"force": "", | |
"pn": str(30*page), | |
"rn": number, | |
"gsm": "1e", | |
"1617626956685": "" | |
} | |
try: | |
# print(requests.get(url, headers=headers,params=params).json()) | |
result = requests.get(url, headers=headers,params=params).json() | |
url_list=[] | |
for data in result['data'][:-1]: | |
url_list.append(data['thumbURL']) | |
for i in range(len(url_list)): | |
getImg(url_list[i],60*page+i,path) | |
bar.update(1) | |
number-=1 | |
if number==0: | |
break | |
page+=1 | |
except: | |
print('finally:') | |
page+=1 | |
time.sleep(10) | |
continue | |
print("\n get finish!") | |
from PIL import Image | |
import os | |
# 设置图像大小和行列数 | |
image_size = 256 # 每个小图片的大小 | |
rows, cols = 30, 30 # 行列数 | |
# 创建一个空白的大图 | |
big_image = Image.new('RGB', (cols * image_size, rows * image_size)) | |
# 遍历目录下的所有文件 | |
pic_dir = path | |
for i, filename in enumerate(sorted(os.listdir(pic_dir))): | |
# 读取每个小图像素数据 | |
small_image = Image.open(os.path.join(pic_dir, filename)) | |
# 计算小图像的缩放比例,使其能够适应大图中的单元格 | |
width, height = small_image.size | |
# scale = min(image_size/width, image_size/height) | |
scale = max(image_size/width, image_size/height) | |
new_width = int(width*scale) | |
new_height = int(height*scale) | |
# 调整小图像的大小,保持其比例不变 | |
small_image = small_image.resize((new_width, new_height), Image.ANTIALIAS) | |
# 计算小图像在大图中的位置 | |
row = i // cols | |
col = i % cols | |
x = col * image_size + (image_size - new_width) // 2 | |
y = row * image_size + (image_size - new_height) // 2 | |
# 将小图粘贴到大图上 | |
big_image.paste(small_image, (x, y)) | |
# 保存大图 | |
big_image.save('big_image_pic_' + search + '.jpg') | |
print("\n save finish!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment