edit : bing art 추가
This commit is contained in:
81
custom_apps/bingart/bingart.py
Normal file
81
custom_apps/bingart/bingart.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import urllib.request
|
||||
import os
|
||||
from pathlib import Path
|
||||
from bingart import BingArt
|
||||
|
||||
from custom_apps.utils import cookie_manager
|
||||
from rest.app.utils.parsing_utils import prompt_to_filenames
|
||||
from rest.app.utils.date_utils import D
|
||||
from const import OUTPUT_FOLDER
|
||||
|
||||
|
||||
class BingArtGenerator:
|
||||
|
||||
model = 'dalle3'
|
||||
detail = 'art'
|
||||
output_folder = os.path.join(OUTPUT_FOLDER,"dalle","art")
|
||||
|
||||
def __init__(self):
|
||||
self.bing_art = BingArt(auth_cookie_U=cookie_manager.get_cookie())
|
||||
self.prompt = ''
|
||||
self.datetime = D.date_file_name()
|
||||
Path(self.output_folder).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def get_image_links(self, prompt, image_len:int):
|
||||
"""
|
||||
이미지 url값 생성
|
||||
"""
|
||||
results = None
|
||||
try:
|
||||
self.prompt = prompt_to_filenames(prompt)
|
||||
results = self.bing_art.generate_images(prompt)
|
||||
|
||||
finally:
|
||||
self.bing_art.close_session()
|
||||
|
||||
if results == None:
|
||||
raise Exception("result is null")
|
||||
|
||||
images = results.get("images")
|
||||
|
||||
if images != None:
|
||||
_temp = []
|
||||
for i in images:
|
||||
_temp.append(i.get('url'))
|
||||
images = _temp
|
||||
|
||||
#중복제거
|
||||
images = list(set(_temp))
|
||||
|
||||
if len(images) < image_len:
|
||||
return images[0:len(images)]
|
||||
else:
|
||||
return images[0:image_len]
|
||||
else:
|
||||
return None
|
||||
|
||||
def link_to_img(self, img_links:list):
|
||||
"""
|
||||
이미지 링크로 이미지 파일 저장
|
||||
"""
|
||||
jpeg_index = 1
|
||||
|
||||
if img_links == None:
|
||||
raise Exception("result is null")
|
||||
|
||||
for i in img_links:
|
||||
path= f"{self.model}_{self.detail}_{self.prompt}_{jpeg_index}_{self.datetime}.png"
|
||||
|
||||
urllib.request.urlretrieve(i, os.path.join(self.output_folder,path))
|
||||
jpeg_index += 1
|
||||
|
||||
def get_images(self, prompt, image_len):
|
||||
image_links = self.get_image_links(prompt,image_len)
|
||||
|
||||
if image_links == None:
|
||||
return 0
|
||||
|
||||
else:
|
||||
self.link_to_img(img_links=image_links)
|
||||
return len(image_links)
|
||||
|
||||
Reference in New Issue
Block a user