一、将原图粘贴到一张正方形的背景上

def trans_square(image):r"""Open the image using PIL."""image = image.convert('RGB')w, h = image.sizebackground = Image.new('RGB', size=(max(w, h), max(w, h)), color=(127, 127, 127))  # 创建背景图,颜色值为127length = int(abs(w - h) // 2)  # 一侧需要填充的长度box = (length, 0) if w < h else (0, length)  # 粘贴的位置background.paste(image, box)return background

二、切片填充的方式

  1. 使用numpy创建背景,使用切片将原图的值填充到背景中。
    def trans_square(image):r"""Open the image using PIL."""img = image.convert('RGB')img = np.array(img, dtype=np.uint8)  # 图片转numpyimg_h, img_w, img_c = img.shapeif img_h != img_w:long_side = max(img_w, img_h)short_side = min(img_w, img_h)loc = abs(img_w - img_h) // 2img = img.transpose((1, 0, 2)) if img_w < img_h else img  # 如果高是长边则换轴,最后再换回来background = np.zeros((long_side, long_side, img_c), dtype=np.uint8)  # 创建正方形背景background[loc: loc + short_side] = img[...]  # 数据填充在中间位置img = background.transpose((1, 0, 2)) if img_w < img_h else backgroundreturn Image.fromarray(img, 'RGB')
def trans_square(img):"""图片转正方形,边缘使用0填充:param img: np.ndarray:return: np.ndarray"""img_h, img_w, img_c = img.shapeif img_h != img_w:long_side = max(img_w, img_h)short_side = min(img_w, img_h)loc = abs(img_w - img_h) // 2img = img.transpose((1, 0, 2)) if img_w < img_h else img  # 如果高是长边则换轴,最后再换回来background = np.zeros((long_side, long_side, img_c), dtype=np.uint8)  # 创建正方形背景background[loc: loc + short_side] = img[...]  # 数据填充在中间位置img = background.transpose((1, 0, 2)) if img_w < img_h else backgroundreturn img
  1. 使用 nn.ZeroPad2d() 或者 nn.ConstantPad2d() 进行填充
def trans_square(image):r"""transform square.:return PIL image"""img = transforms.ToTensor()(image)C, H, W = img.shapepad_1 = int(abs(H - W) // 2)  # 一侧填充长度pad_2 = int(abs(H - W) - pad_1)  # 另一侧填充长度img = img.unsqueeze(0)  # 加轴if H > W:img = nn.ZeroPad2d((pad_1, pad_2, 0, 0))(img)  # 左右填充,填充值是0# img = nn.ConstantPad2d((pad_1, pad_2, 0, 0), 127)(img)  # 左右填充,填充值是127elif H < W:img = nn.ZeroPad2d((0, 0, pad_1, pad_2))(img)  # 上下填充,填充值是0# img = nn.ConstantPad2d((0, 0, pad_1, pad_2), 127)(img)  # 上下填充,填充值是127img = img.squeeze(0)  # 减轴img = transforms.ToPILImage()(img)return img

使用Python将图片转正方形的两种方法相关推荐

  1. vue 图片转base64的两种方法(包括h5+plus调取手机图片)

    vue 图片转base64的两种方法(包括h5+plus调取手机图片) 1.获取图片文件对象进行转换(主要是对PC端的) 在main.js文件下添加全局方法 Vue.prototype.$base64 ...

  2. web自动化测试图片上传的两种方法

    web自动化测试图片上传的两种方法: 通过input标签的,如: <!DOCTYPE html> <html lang="en"> <head> ...

  3. html+轮播图下标跳转代码,最简单的JavaScript图片轮播代码(两种方法)

    通过改变每个图片的opacity属性: 素材图片: 代码一: 最简单的轮播广告 body, div, ul, li { margin: ; padding: ; } ul { list-style-t ...

  4. jupyter读取图片并展示的两种方法

    1.通用方法:python打开 from PIL import Image display(Image.open("data/20a6a2.png")) #data/20a6a2. ...

  5. Python数据分析:异常值检验的两种方法 -- Z 分数 上下分位点(放入自写库,一行代码快速实现)

    本文已在公众号 " 数据分析与商业实践 " 首发.关注一下~,更多商业数据分析案例源码等你来撩.后台回复 "异常值" ,即可获取本文的案例示范与包含详细注释的源 ...

  6. c语言mfc怎么插入背景图片,MFC 对话框添加背景图片详细过程(两种方法)

    给对话框添加背景图片方法很多,在此贴出两种很常见的方法.一种是通过读取位图资源显示位图(BitMap) step: 1.创建内存设备上下文: 2.选择位图,将其装入内存设备上下文: 3.使用BitBl ...

  7. python hist直方图拟合曲线_详解用Python为直方图绘制拟合曲线的两种方法

    直方图是用于展示数据的分组分布状态的一种图形,用矩形的宽度和高度表示频数分布,通过直方图,用户可以很直观的看出数据分布的形状.中心位置以及数据的离散程度等. 在python中一般采用matplotli ...

  8. 在Vue-cli脚手架中引入图片最常用的两种方法

    CommonJS API定义了很多普通应用程序(主要指非浏览器的应用)使用的API,require就说其中之一,我们通常需要在组件中引入图片时,可以在Data里使用这个方法require(" ...

  9. MFC 对话框添加背景图片详细过程(两种方法)

    给对话框添加背景图片方法很多,在此贴出两种很常见的方法.一种是通过读取位图资源显示位图(BitMap) step: 1.创建内存设备上下文: 2.选择位图,将其装入内存设备上下文: 3.使用BitBl ...

最新文章

  1. Winform 控件自适应 JSP 入门登录案例
  2. some requirement checks failed
  3. UIView上的控件使用push方法跳转
  4. ABI 与 API 的区别(应用程序二进制接口、应用程序编程接口)
  5. webflow_Webflow是否适合开发人员? 我的经验
  6. 代理proxy网络代理自动发现wpad代理自动配置pac三个概念解析
  7. msi笔记本u盘装linux,微星笔记本bios设置u盘启动的步骤方法详细教程 - 系统家园...
  8. 用友超客:社交化业务就是要化繁为简
  9. 网页设计的常用字体规范
  10. 雷赛控制卡可以用java写吗_运动控制卡应用编程技巧几招(2)
  11. gtest中死亡测试
  12. 【云原生kubernetes】coredns解析集群外部域名
  13. 解密通往元宇宙的三大入口,VR先上AR紧跟,但脑机接口才是未来
  14. Linux ls 命令学习和简单使用
  15. [经典收藏]1200个Photoshop经典实例打造ps高手!
  16. java 中的poi_Java中使用POI操作ExceL的读与
  17. PAT乙级题库踩坑实录
  18. 设计模式——备忘录模式
  19. 《环球企业家》:国产手机操纵者联发科的秘密
  20. 【MATLAB绘图】3sigma即剔除小概率事件功能的使用

热门文章

  1. 图像处理深度学习经典基础算法
  2. supesite 标签语法
  3. 宏与namespace
  4. 如何搭建入侵检测系统
  5. 为什么你看了很多书 ,却依然没有洞见 (深度好文)
  6. 1t硬盘怎么分区最好_详解500G/1TB移动硬盘怎么合理分区
  7. ADO.net技术内幕(奋斗的小鸟)_PDF 电子书
  8. PMP证书备考心得分享
  9. 十九、客户端多线程分组模拟高频并发数据
  10. 企业标志设计的创意方法