1.安装python
2.安装git
运行环境:
//确认python安装成功
#python
#pip.exe
//安装python-opencv
#pip install opencv-python
//安装git
#git
#pip install git+https://github.com/fizzday/imageCropSmart
python脚本:
"""
功能:智能裁剪图片
运行环境:python3
安装依赖:pip3 install smartcrop pillow6.2.1
运行样例:python3 clip2.py "D:\\home\\data\\code\\autoclip\\results_caijing\\cat.jpg" "D:\\home\\data\\code\\autoclip\\results_caijing\\cat2.jpg"
"""
import json
import sys
import smartcrop
from PIL import Image
def main(path,result_path):
size=9999
print(path)
src = Image.open(path)
hight,width=src.size[0],src.size[1]
if hight < size:
size = hight
if width < size:
size = width
print(src.size,size)
corn_info = smartcrop.SmartCrop().crop(src, size, size)
box = (
corn_info['top_crop']['x'],
corn_info['top_crop']['y'],
corn_info['top_crop']['width'] + corn_info['top_crop']['x'],
corn_info['top_crop']['height'] + corn_info['top_crop']['y']
)
cropped_image = src.crop(box)
cropped_image.save(result_path, 'JPEG', quality=90)
if __name__ == '__main__':
main(sys.argv[1],sys.argv[2])
运行结果如下:
安装Pillow
//安装 Pillow
#pip install Pillow
再次运行脚本:
升级smartCrop(先卸载之前的smartcrop,再安装smartcrop)
#pip uninstall smartcrop
#pip install smartcrop
再次运行脚本:
成功