Skip to content

Commit

Permalink
实现了缩略图的本地缓存, 实现了Mock缩略图数据的获取,形成本地缓存,展示.
Browse files Browse the repository at this point in the history
Signed-off-by: Sadam·Sadik <[email protected]>
  • Loading branch information
Haoke98 committed Feb 24, 2024
1 parent 4f842b0 commit 3d0cb05
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
9 changes: 9 additions & 0 deletions frames/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# _*_ codign:utf8 _*_
"""====================================
@Author:Sadam·Sadik
@Email:[email protected]
@Date:2024/2/22
@Software: PyCharm
@disc:
======================================="""
from .image_grid import ScrollableImageGrid
64 changes: 52 additions & 12 deletions frames/image_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import datetime
import json
import logging
import os.path
import threading
import time
import tkinter as tk
from io import BytesIO

import requests
from PIL import Image, ImageTk
from PIL import Image, ImageTk, UnidentifiedImageError

from lib.mock import mock_images
from lib import export_frame

MOCK_ACTIVE = True


class ScrollableImageGrid(tk.Frame):
Expand Down Expand Up @@ -70,6 +75,8 @@ def set_data(self, assets: list[slice]):

def cache_assets_thumb(self):
createdAt = datetime.datetime.now()
if MOCK_ACTIVE:
_mock_images = mock_images("Soft", 1, 200, )
while True:
dlt = datetime.datetime.now() - createdAt
if len(self.assets) == 0:
Expand All @@ -78,19 +85,52 @@ def cache_assets_thumb(self):
else:
for i, asset_info in enumerate(self.assets):
recordName, size, file_type, created, modified, master_record_str, asset_record_str = asset_info
logging.info("{}. {}".format(i, recordName))
if not self.assets_caches.__contains__(recordName):
master_record = json.loads(master_record_str)
asset_record = json.loads(asset_record_str)
thumbURL = master_record["fields"]["resJPEGThumbRes"]["value"]["downloadURL"]

logging.info("{}. {} --> {}".format(i, recordName, thumbURL))
response = requests.get(thumbURL)
img = Image.open(BytesIO(response.content))
img.thumbnail((100, 100)) # Resize image while preserving aspect ratio
img = ImageTk.PhotoImage(img)

self.assets_caches[recordName] = img
cache_dir = os.path.join(".", "caches")
fn = recordName.replace("/", "_") + file_type
if MOCK_ACTIVE:
if i == 200:
_mock_images + mock_images("Soft", (i // 200) + 1, 200)
target = _mock_images[i]
thumbURL = target["url"]
# 对mock数据单独进行缓存π
cache_dir = os.path.join(".", "caches", "mock")
fn = str(target["id"]) + ".jpeg"
logging.info("{}. {} --> {}".format(i, recordName, fn, thumbURL))
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
fp = os.path.join(cache_dir, fn)
try:
if not os.path.exists(fp):
# 如果没有缓存,则先下载下来先产生缓存
response = requests.get(thumbURL)
contentType = response.headers.get("Content-Type")

if contentType in ["video/mp4"]:
video_fp = fp + ".mp4"
with open(video_fp, "wb") as f:
f.write(response.content)
export_frame(video_fp, 1, fp)
else:
# if contentType in ["image/jpeg", "image/png"]:
with open(fp, "wb") as f:
f.write(response.content)

# 打开
img = Image.open(fp)
img.thumbnail((100, 100)) # Resize image while preserving aspect ratio
img = ImageTk.PhotoImage(img)

self.assets_caches[recordName] = img
except FileNotFoundError as e:
logging.error("Could not find file: {}".format(fp), exc_info=True)
except UnidentifiedImageError as e:
logging.error("FileReadException: {},{}".format(e, response), exc_info=True)
except requests.exceptions.SSLError as e:
logging.error("NetworkException: {}".format(e), exc_info=True)

def update_labels(self):
epoch_num = 1
Expand All @@ -111,7 +151,7 @@ def update_labels(self):
a += 1
else:
b += 1
self._add_label(i, self.default_img)
self._add_label(i, None)
epoch_num += 1
logging.info("Epoch: {} thumb:{}".format(epoch_num, a))

Expand Down
28 changes: 28 additions & 0 deletions lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,31 @@
@Software: PyCharm
@disc:
======================================="""
import cv2


def export_frame(video_path, frame_number, output_path):
# 打开视频文件
cap = cv2.VideoCapture(video_path)

# 检查视频是否成功打开
if not cap.isOpened():
print("Error: Could not open video file.")
return

# 设置视频帧位置
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)

# 读取帧
ret, frame = cap.read()

# 检查帧是否成功读取
if not ret:
print("Error: Could not read frame.")
return

# 保存帧为图像文件
cv2.imwrite(output_path, frame)

# 关闭视频文件
cap.release()
23 changes: 23 additions & 0 deletions lib/mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# _*_ codign:utf8 _*_
"""====================================
@Author:Sadam·Sadik
@Email:[email protected]
@Date:2024/2/23
@Software: PyCharm
@disc:
======================================="""
import requests


def mock_images(nsfw: str, page: int = 1, limit: int = 100):
"""
:param page:
:param nsfw: boolean | enum (None, Soft, Mature, X) Filter to images that contain mature content flags or not (undefined returns all)
:param limit: The number of results to be returned per page. This can be a number between 0 and 200. By default, each page will return 100 results.
:return:
"""
resp = requests.get(
"https://civitai.com/api/v1/images?&limit=" + str(limit) + "&page=" + str(page) + "&nsfw=" + nsfw)
items = resp.json()["items"]
return items
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pyicloud~=1.0.0
colorlog~=6.6.0
piexif~=1.1.3
pillow~=10.2.0
pillow~=10.2.0
opencv-python~=4.9.0.80

0 comments on commit 3d0cb05

Please sign in to comment.