python 图片正方形化

本小妞迷上赌 提交于 2019-12-08 02:52:36

代码如下:

from PIL import Image
from os import listdir
import math
import numpy as np
import cv2

def textureSquare( imgPath):
    img = Image.open(imgPath)
    width, height = img.size
    delta = width - height
    if(delta > 0):
        repeat = delta / height
        result = Image.new(img.mode, (width, height + delta))
        region1 = img.crop((0, height - (delta - repeat * height), width, height))
        result.paste(region1, box = (0, 0))
        for i in range(0, repeat + 1):
            result.paste(img, box = (0, delta - repeat * height + i * height))
        return result
    elif(delta < 0):
        delta = abs(delta)
        repeat = delta / width
        result = Image.new(img.mode, (width + delta, height))
        region1 = img.crop((0, 0, delta - repeat * width, height))
        result.paste(region1, box = (0, 0))
        for i in range(0, repeat + 1):
            result.paste(img, box = (delta - repeat * width + i * width, 0))
        return result
    else:
        return img


def textureTransform( imgPath, offsetX, offsetY, uvScale):
    imgOriginal = Image.open(imgPath)


img = textureSquare("E:\\PythonProjects\\pro_obj_converter\\a.jpg")
# print img.size
img.save("E:\\PythonProjects\\pro_obj_converter\\a.jpg")

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!