# ImageBed **Repository Path**: billhuhongbin/ImageBed ## Basic Information - **Project Name**: ImageBed - **Description**: No description available - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-22 - **Last Updated**: 2022-05-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Use Gitee Repo as a Personal Image Bed ### 使用Gitee + python脚本 作为Typora图床 #### §1. [upload.py](./upload.py) 图片上传脚本 ``` import os import shutil import sys import urllib.request import uuid from datetime import datetime import git # 本地图床目录 image_dir = 'images' # 本地git仓 local_git_repo = r'D:\Workspace\PyCharm\ImageBed' # 图片对应远程链接(注意复制github/gitee内图片地址时,需要将blob改为raw) url_base = 'https://gitee.com/billhuhongbin/ImageBed/raw/master/%s/%s/%s' git = git.Repo(local_git_repo).git if __name__ == '__main__': is_upload_net_img = False image_files = sys.argv[1:] date_str = datetime.strftime(datetime.now(), '%Y-%m-%d') temp_dir = os.path.join(local_git_repo, image_dir, date_str) if not (os.path.exists(temp_dir) and os.path.isdir(temp_dir)): os.makedirs(temp_dir) print('Upload Success:') for image_file in image_files: # 切割路径获取文件名和后缀 (image_path, image_name) = os.path.split(image_file) (file_name, suffix) = os.path.splitext(image_name) # 重组文件名 image_name = 'image-' + str(uuid.uuid4()) + suffix new_image_file = os.path.join(temp_dir, image_name) if image_file.startswith('http') or image_file.startswith('https'): if is_upload_net_img: image_file = urllib.request.urlretrieve(image_file, new_image_file)[0] else: print(image_file) continue shutil.copy(image_file, new_image_file) git.add(new_image_file) git.commit('-m', 'Add image: ' + image_name) git.push() print(url_base % (image_dir, date_str, image_name)) ``` > ***注意修改远程图片地址,否则外部无法链接到图片**: > > 图片地址:https://gitee.com/billhuhongbin/ImageBed/blob/master/images/2021-09-22/image-d56180c9-2130-4bd5-addc-4849d1f63ee3.png > > 修改地址:https://gitee.com/billhuhongbin/ImageBed/raw/master/images/2021-09-22/image-d56180c9-2130-4bd5-addc-4849d1f63ee3.png #### §2. Change Typora setting `Preferrence >> Image` ![image-20210922233558275](https://gitee.com/billhuhongbin/ImageBed/raw/master/images/2021-09-22/image-d56180c9-2130-4bd5-addc-4849d1f63ee3.png) #### §3. Test Uploader ![image-20210922234437628](https://gitee.com/billhuhongbin/ImageBed/raw/master/images/2021-09-22/image-fd0b13f3-b306-406b-8a1f-8a7d396510df.png)