88from src .upload .bilitool .bilitool .model .model import Model
99from src .config import BAIDU_API_KEY
1010
11+
1112def cover_up (img : str ):
1213 """Upload the cover image
1314 Parameters
@@ -20,14 +21,15 @@ def cover_up(img: str):
2021 """
2122 from PIL import Image
2223 from io import BytesIO
24+
2325 request = requests .Session ()
2426 request .headers = {
25- ' user-agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108" ,
26- ' referer' : "https://www.bilibili.com/" ,
27- ' connection' : ' keep-alive'
27+ " user-agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108" ,
28+ " referer" : "https://www.bilibili.com/" ,
29+ " connection" : " keep-alive" ,
2830 }
2931 model = Model ().get_config ()
30- request .cookies .set (' SESSDATA' , model [' cookies' ][ ' SESSDATA' ])
32+ request .cookies .set (" SESSDATA" , model [" cookies" ][ " SESSDATA" ])
3133 with Image .open (img ) as im :
3234 # you should keep the image ratio 16:10
3335 xsize , ysize = im .size
@@ -44,7 +46,7 @@ def cover_up(img: str):
4446 data = {
4547 "cover" : b"data:image/jpeg;base64,"
4648 + (base64 .b64encode (buffered .getvalue ())),
47- "csrf" : model [' cookies' ][ ' bili_jct' ]
49+ "csrf" : model [" cookies" ][ " bili_jct" ],
4850 },
4951 timeout = 30 ,
5052 )
@@ -55,24 +57,33 @@ def cover_up(img: str):
5557 print (res ["data" ]["url" ], flush = True )
5658 return res ["data" ]["url" ]
5759
60+
5861def baidu_generate_cover (your_file_path ):
62+ """Generater cover image using baidu api
63+ Args:
64+ your_file_path: str, path to the image file
65+ Returns:
66+ str, local download path of the generated cover image file
67+ """
5968 try :
6069 cover_url = cover_up (your_file_path )
6170
6271 url = "https://qianfan.baidubce.com/v2/images/generations"
63- payload = json .dumps ({
64- "model" : "irag-1.0" ,
65- "prompt" : "这是视频截图,请根据该图生成对应的动漫类型的封面" ,
66- "refer_image" : cover_url
67- })
72+ payload = json .dumps (
73+ {
74+ "model" : "irag-1.0" ,
75+ "prompt" : "这是视频截图,请根据该图生成对应的动漫类型的封面" ,
76+ "refer_image" : cover_url ,
77+ }
78+ )
6879 headers = {
69- ' Content-Type' : ' application/json' ,
70- ' Authorization' : f' Bearer { BAIDU_API_KEY } '
80+ " Content-Type" : " application/json" ,
81+ " Authorization" : f" Bearer { BAIDU_API_KEY } " ,
7182 }
72-
83+
7384 response = requests .request ("POST" , url , headers = headers , data = payload )
7485 if response .status_code == 200 :
75- image_url = response .json ()[' data' ][0 ][' url' ]
86+ image_url = response .json ()[" data" ][0 ][" url" ]
7687 img_data = requests .get (image_url ).content
7788 cover_name = time .strftime ("%Y%m%d%H%M%S" ) + ".png"
7889 temp_cover_path = os .path .join (os .path .dirname (your_file_path ), cover_name )
@@ -87,5 +98,6 @@ def baidu_generate_cover(your_file_path):
8798 print (e , flush = True )
8899 return None
89100
90- if __name__ == '__main__' :
91- print (baidu_generate_cover ("" ))
101+
102+ if __name__ == "__main__" :
103+ print (baidu_generate_cover ("" ))
0 commit comments