50 lines
2.2 KiB
Python
50 lines
2.2 KiB
Python
|
|
from PyQt5.QtCore import QThread, pyqtSignal
|
|
import requests
|
|
from PIL import Image
|
|
from io import BytesIO
|
|
import logging
|
|
|
|
# 로거 인스턴스 가져오기
|
|
logger = logging.getLogger('default_logger')
|
|
|
|
class ImageSaveThread(QThread):
|
|
progress_updated = pyqtSignal(int, int) # 전체 수, 현재 진행 수
|
|
|
|
def __init__(self, conn, image_save_folder, parent=None):
|
|
super(ImageSaveThread, self).__init__(parent)
|
|
self.conn = conn
|
|
self.image_save_folder = image_save_folder
|
|
|
|
def run(self):
|
|
logger.debug("네이버 쇼핑 이미지 저장 쓰레드 시작")
|
|
cursor = self.conn.cursor()
|
|
cursor.execute("SELECT id, keyword, imageUrl FROM NaverShopping WHERE imageUrl IS NOT NULL")
|
|
shopping_rows = cursor.fetchall()
|
|
|
|
total_images = len(shopping_rows)
|
|
for idx, row in enumerate(shopping_rows):
|
|
id, keyword, imageUrl = row
|
|
try:
|
|
response = requests.get(imageUrl)
|
|
if response.status_code == 200:
|
|
# 이미지 데이터를 메모리에 로드
|
|
image = Image.open(BytesIO(response.content))
|
|
# 이미지 파일 이름 정의 (예: keyword_id.jpg)
|
|
image_file_name = f"{keyword}_{id}.png"
|
|
image_path = f"{self.image_save_folder}/{image_file_name}"
|
|
# 이미지 저장
|
|
# image.save(image_path)
|
|
image.save(image_path, format='PNG') # PNG 형식으로 명시적 저장
|
|
logger.debug(f"이미지 저장 완료 : {image_path}")
|
|
num_idx = idx + 1
|
|
logger.debug(f"이미지 저장 {num_idx}/{total_images}")
|
|
# DB에 로컬 이미지 경로 업데이트 (선택적)
|
|
cursor.execute("UPDATE NaverShopping SET localImagePath = ? WHERE imageUrl = ?", (image_path, imageUrl))
|
|
self.conn.commit()
|
|
except Exception as e:
|
|
logger.debug(f"Error saving image {imageUrl}: {e}")
|
|
|
|
current_progress = idx + 1
|
|
self.progress_updated.emit(total_images, current_progress)
|