Skip to content

Commit

Permalink
fix: audio_uploader.upload_cover
Browse files Browse the repository at this point in the history
feat: Picture.resize
  • Loading branch information
Nemo2011 committed Feb 3, 2025
1 parent 2904293 commit 7e1b52f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions bilibili_api/audio_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,10 @@ async def upload_cover(cover: Picture, credential: Credential) -> str:
"""
api = _API["image"]
# 小于 3MB
raise_for_statement(os.path.getsize(cover) < 1024 * 1024 * 3, "3MB size limit")
raise_for_statement(len(cover.content) < 1024 * 1024 * 3, "3MB size limit")
# 宽高比 1:1
raise_for_statement(
cover.width == cover.height, "width == height, 600 * 600 recommanded"
cover.width == cover.height, "width == height, 600 * 600 recommended"
)
files = {"file": cover.content}
files = {"file": cover._to_biliapifile()}
return await Api(**api, credential=credential).update_files(**files).result
25 changes: 24 additions & 1 deletion bilibili_api/utils/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Picture:
imageType (str) : 格式,例如: png
size (Any) : 尺寸
size (Any) : 大小。单位 KB
url (str) : 图片链接
Expand Down Expand Up @@ -188,6 +188,29 @@ def convert_format(self, new_format: str) -> "Picture":
self.__set_picture_meta_from_bytes(new_format)
return self

def resize(self, width: int, height: int) -> "Picture":
"""
调整大小
Args:
width (int): 宽度
height (int): 高度
Returns:
Picture: `self`
"""
tmp_dir = tempfile.gettempdir()
img_path = os.path.join(tmp_dir, "test." + self.imageType)
open(img_path, "wb").write(self.content)
img = Image.open(img_path)
img = img.resize((width, height))
new_img_path = os.path.join(tmp_dir, "test." + self.imageType)
img.save(new_img_path)
with open(new_img_path, "rb") as file:
self.content = file.read()
self.__set_picture_meta_from_bytes(self.imageType)
return self

def to_file(self, path: str) -> "Picture":
"""
下载图片至本地。
Expand Down

0 comments on commit 7e1b52f

Please sign in to comment.