From ff66b292408f7680dc3d02a39ae0103f30d090c2 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Thu, 21 Sep 2023 15:42:14 +0800 Subject: [PATCH] :bug: Fix photo invalid dimensions --- .../apihelper/client/components/hyperion.py | 13 +++++----- modules/apihelper/models/genshin/hyperion.py | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/modules/apihelper/client/components/hyperion.py b/modules/apihelper/client/components/hyperion.py index fb75a667..6aaed677 100644 --- a/modules/apihelper/client/components/hyperion.py +++ b/modules/apihelper/client/components/hyperion.py @@ -108,10 +108,11 @@ class Hyperion: self.download_image(post_info.post_id, post_info.image_urls[page], page) for page in range(len(post_info.image_urls)) ] - result_list = await asyncio.gather(*task_list) - for result in result_list: - if isinstance(result, ArtworkImage): - art_list.append(result) + result_lists = await asyncio.gather(*task_list) + for result_list in result_lists: + for result in result_list: + if isinstance(result, ArtworkImage): + art_list.append(result) def take_page(elem: ArtworkImage): return elem.page @@ -119,7 +120,7 @@ class Hyperion: art_list.sort(key=take_page) return art_list - async def download_image(self, art_id: int, url: str, page: int = 0) -> ArtworkImage: + async def download_image(self, art_id: int, url: str, page: int = 0) -> List[ArtworkImage]: filename = os.path.basename(url) _, file_extension = os.path.splitext(filename) is_image = bool(file_extension in ".jpg" or file_extension in ".png") @@ -127,7 +128,7 @@ class Hyperion: response = await self.client.get( url, params=self.get_images_params(resize=2000) if is_image else None, de_json=False ) - return ArtworkImage( + return ArtworkImage.gen( art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content ) diff --git a/modules/apihelper/models/genshin/hyperion.py b/modules/apihelper/models/genshin/hyperion.py index 57936994..93296a2c 100644 --- a/modules/apihelper/models/genshin/hyperion.py +++ b/modules/apihelper/models/genshin/hyperion.py @@ -25,6 +25,32 @@ class ArtworkImage(BaseModel): pass return None + @staticmethod + def gen(*args, **kwargs) -> List["ArtworkImage"]: + data = [ArtworkImage(*args, **kwargs)] + if data[0].file_extension and data[0].file_extension in ["gif", "mp4"]: + return data + try: + with BytesIO(data[0].data) as stream, Image.open(stream) as image: + width, height = image.size + crop_height = height + crop_num = 1 + max_height = 10000 - width + while crop_height > max_height: + crop_num += 1 + crop_height = height / crop_num + new_data = [] + for i in range(crop_num): + slice_image = image.crop((0, crop_height * i, width, crop_height * (i + 1))) + bio = BytesIO() + slice_image.save(bio, "png") + kwargs["data"] = bio.getvalue() + kwargs["file_extension"] = "png" + new_data.append(ArtworkImage(*args, **kwargs)) + return new_data + except UnidentifiedImageError: + return data + class PostInfo(BaseModel): _data: dict = PrivateAttr()