[Lazy sheep and sheep choose tutors] Reptile + image stitching

I should have read more papers and research directions of the tutors tonight, but when I checked the official website, I found that HKUST really provided us with a lot of teachers! I can’t choose any of them, and the interface can only view the photos of one teacher at a time. In order to have a panoramic view of the handsome and beautiful photos of the teachers at a glance, I wrote a code.

 

Table of contents

1. Climb the teacher's photo 

2. Stitching beautiful photos of teachers


1. Climb the teacher's photo 

List of available tutors for our majors:

Don't be envious, there are so many great teachers! !

 So start writing code:

# 导入必要的包
from bs4 import BeautifulSoup
import requests
import os
import traceback

#使用request取到网页的源码
html = requests.get(r"https://bs.ustc.edu.cn/chinese/teacher-8.html")
html.encoding = 'utf-8'
#使用beautifulsoup对网页源码进行解析
soup = BeautifulSoup(html.text,'lxml')
#https://bs.ustc.edu.cn/

path = r""#你要放照片的文件夹路径
for i in range(3,56):
    try:
        temp = soup.select("body > div.wrap.w.cle > div.fr.view > div.count > div.teach-list > ul:nth-child("+str(i)+") > li:nth-child(1) > a")
        temp_link = "https://bs.ustc.edu.cn/"+temp[0].attrs['href']
        #使用request取到网页的源码
        temp_html = requests.get(temp_link)
        temp_html.encoding = 'utf-8'
        #使用beautifulsoup对网页源码进行解析
        temp_soup = BeautifulSoup(temp_html.text,'lxml')
        pic_link = temp_soup.select("body > div.wrap.w.cle > div.fr.view > div.content > div.tearcher_crad > div.teahcher_img > img")
        pic_full_link = "https://bs.ustc.edu.cn"+pic_link[0].attrs["src"]
        temp_filename = "\\"
        name_xml = temp_soup.select("body > div.wrap.w.cle > div.fr.view > div.content > div.tearcher_crad > div.teacher_msg > ul.msg-text > li")
        for j in range(0,len(name_xml)):
            try:
                temp_filename = temp_filename+name_xml[j].string+"_"
            except Exception as e: #该方法捕获所有异常
                traceback.print_exc() #该处可报出异常的具体位置和原因
        r = requests.get(pic_full_link)
        r.raise_for_status()
        path_use = path + temp_filename+".jpg"
        with open(path_use,'wb') as f:
            f.write(r.content)
            f.close()
            print("保存成功",temp_filename)
    except Exception as e: #该方法捕获所有异常
        traceback.print_exc() #该处可报出异常的具体位置和原因

 Running result: (some information is missing, and the error path is printed)

Save Success\Hu [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Jin [email protected]_Statistics Department of Finance_Probability and Statistics_Saving 
Success\Wang [email protected]_Department of Statistics and Finance_Finance_Saving 
Success\Wang Xueqin_Professor_+86-551-63606292_wangxq20@ ustc.edu.cn_Department of Statistics and Finance_Probability and Statistics_Saving 
Success\Wu [email protected]_Statistics and Finance Department_Probability and Statistics_Saving 
Success\Professor Yang Yaning [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Zhang [email protected]_Department of Statistics and Finance_Probability and Statistics_Saving 
success\Shuguang [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zhang [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zhang [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Han Xiao_Special [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zheng Zemin_Special [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Yang Qing_Special [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Chen Yu_Associate [email protected]_Statistics and Department of Finance_Probability and Statistics_Saving 
Success\Cui Wenquan_Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_Saving 
Success\Feng Qunqiang_Associate Professor_+86-551-63600846_fengqq@ ustc.edu.cn_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Gou Xiaoju_Associate [email protected]_Statistics and Finance Department_Finance_Save 
Success\Guo Xiao_Associate Professor [email protected]_Department of Statistics and Finance_Probability and Statistics_Saving 
Success\He Jun_Associate [email protected]_Department of Statistics and Finance_Finance _Save 
Success\Hu Zhishui_Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Liu Jie_Associate [email protected]. cn_Department of Statistics and Finance_Probability and Statistics_Saving 
Success\Mao Tiantian_Associate [email protected]_Statistics and Finance Department_Probability and Statistics_Saving 
Success\Pan Wanbin_Associate Professor_+ [email protected]_Department of Statistics and Finance_Finance_
Save Success\Wan Hongyan_Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Wang Zhanfeng_Associate [email protected] _Department of Statistics and Finance_Probability and Statistics_Save 
Success\Wen Canhong_Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Ye Wuyi_Associate Professor_+86 [email protected]_Department of Statistics and Finance_Finance_Saving 
Success\Zhuang Weiwei_Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Chen Liujun_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\He Baihua_special associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Liu Xing_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Lu Kai_Associate [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Sun Yulong_special associate [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success \ Xia Chongwu_Special Associate [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zeng Jing_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zhang Bo_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zhang Jingnan_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Preserving Success\Tension_Special Associate [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Zhao Zhenling_Special Associate [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Ding Hao_Special Associate [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\Guo [email protected]_Statistics and Finance Department_Finance_Saving 
Success\Wei [email protected]_Department of Statistics and Finance_Finance_Saving 
Success\Wu [email protected] _Department of Statistics and Finance_Finance_Saving 
Success\Wu [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Chen [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Fang [email protected]_Department of Statistics and Finance_General Management_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Guo [email protected]_Department of Statistics and Finance_Finance_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Li [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Li [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Li [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Lu [email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str
Save Success\Wei [email protected]_Department of Statistics and Finance_Probability and Statistics_Save 
Success\[email protected]_Department of Statistics and Finance_Probability and Statistics_
Traceback (most recent call last):
  File "C:\Users\53497\AppData\Local\Temp\ipykernel_11848\605563424.py", line 17, in <cell line: 2>
    temp_filename = temp_filename+name_xml[j].string+"_"
TypeError: can only concatenate str (not "NoneType") to str

2. Stitching beautiful photos of teachers

code:

from PIL import Image
import os

IMAGES_PATH = ''  # 图片集地址
IMAGES_FORMAT = ['.jpg', '.JPG']  # 图片格式
IMAGE_SIZE = 256  # 每张小图片的大小
IMAGE_ROW = 6 # 图片间隔,也就是合并成一张图后,一共有几行
IMAGE_COLUMN =9  # 图片间隔,也就是合并成一张图后,一共有几列
IMAGE_SAVE_PATH = '\\final.jpg'  # 图片转换后的地址(要带最终的图片名)

# 获取图片集地址下的所有图片名称
image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if
               os.path.splitext(name)[1] == item]
print("一共有"+str(len(image_names))+"张照片")
# 简单的对于参数的设定和实际图片集的大小进行数量判断
if len(image_names) != IMAGE_ROW * IMAGE_COLUMN:
    raise ValueError("合成图片的参数和要求的数量不能匹配!")

# 定义图像拼接函数
def image_compose():
    to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE)) #创建一个新图
    # 循环遍历,把每张图片按顺序粘贴到对应位置上
    for y in range(1, IMAGE_ROW + 1):
        for x in range(1, IMAGE_COLUMN + 1):
            try:
                from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
                    (IMAGE_SIZE, IMAGE_SIZE),Image.ANTIALIAS)
                to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
            except:
                continue
    return to_image.save(IMAGE_SAVE_PATH) # 保存新图
image_compose() #调用函数

operation result:

 

 Feast your eyes~ (Weakly mentioned, I hope you don’t mind using photos of teachers~ If you mind, please accept me as your student, and then order me to delete it, okay~)

Important! *************: This article is just for a little entertainment. You should choose a tutor based on your own situation to choose the teacher that best matches you~ I hope I will be taken in as soon as possible~

Guess you like

Origin blog.csdn.net/standingflower/article/details/126736071