栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python批量补全图片为指定尺寸

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python批量补全图片为指定尺寸

# 将图片补全为1280*720
import cv2
from PIL import Image
import numpy as np
# 如果将图片补全为1280*720,输入的图片不能大于这个尺寸,高和宽可以自己修改
for i in range(1,1099):
    #输入的图片文件夹
    inputImage = cv2.imread('rotate2/img_' + str(i) + '.jpg', 1)

    file_path = 'rotate2/img_' + str(i) + '.jpg'
    img = Image.open(file_path)
    imgSize = img.size  #大小/尺寸
    w = img.width       #图片的宽
    h = img.height

    left = int((1280-w)/2)
    right = left
    if w+left+right != 1280:
        left = 1280 - w - right

    top = int((720-h)/2)
    bottom = top
    if h + top + bottom != 720:
        top = 720 - h - bottom


    outputImage = cv2.copyMakeBorder(inputImage,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[255,255,255])
    #输出的图片文件夹
    cv2.imwrite('rotate3/img_' + str(i) + '.jpg', outputImage)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号