社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python — 将非方形图像转换为方形图像

AI算法与图像处理 • 2 年前 • 280 次点击  

使用 CV2 在 Python 中以编程方式完成如下操作:将非方形图像转换为方形图像

因此,6 年来,我第一次将一些图片上传到 Instagram。我画了一些愚蠢的漫画,想上传它来娱乐一下。然而,问题:

  • 我有 10 张图片要上传
  • 每个图像都有不同的尺寸
  • Instagram 会自动将你的图像 (ew) 裁剪为
  1. 方形
  2. 4:5 纵横比
  3. 9:16 纵横比

所以我需要一种方法来为我的图像添加白色填充,使它们都是正方形。

以下是我如何使用 Python 实现的:

文件夹结构

- myimages
  - 1.png
  - 2.png
  - 3.png
  - 4.png
  - 5.png
  - 6.png
run.py

安装依赖

pip install opencv-python

完整代码

# run.py

import cv2
import os

# replace this with your own folder name
# your folder should contain all images you want to squarify
folder = 'yourfolder'

new_folder = f'{folder}_new'
if not os.path.exists(new_folder):
    os.mkdir(new_folder)

for filename in os.listdir(folder):
    if '.png' not in filename.lower():
        continue

    filepath = f'{folder}/{filename}'
    img = cv2.imread(filepath)

    h, w = img.shape[:2]
    px, py = 00

    if h > w:
        px = (h-w)//2
    elif h         py = (w-h)//2

    newimg = cv2.copyMakeBorder(
        img, py, py, px, px, borderType=cv2.BORDER_CONSTANT, value=[255,255,255]
    )

    cv2.imwrite(f'{new_folder}/{filename}', newimg)

代码中发生了什么

import cv2
import os

# replace this with your own folder name
# your folder should contain all images you want to squarify
folder = 'myimages'

# name of folder the NEW images will be in
new_folder = f'{folder}_new'

# if this folder doesn't exist, create it
if not os.path.exists(new_folder):
    os.mkdir(new_folder)

# looping through all images in our folder
for  filename in os.listdir(folder):

    # ignoring anything that isn't an image
    # rememeber to change this is you're using jpg or something else
    if '.png' not in filename.lower():
        continue

    filepath = f'{folder}/{filename}'

    # reading the image using cv2.imread
    # img is a numpy 3d array
    img = cv2.imread(filepath)

    # extracting height/width of image
    h, w = img.shape[:2]

    # px --> number of padding pixels to add on left/right
    # py --> number of padding pixels to add on top/bottom
    px, py = 00

    # checking if image height is more than image width
    if h > w:
        # calculating pixels to add to left/right
        # py will be 0
        px = (h-w)//2
    elif h         # calculating pixels to add to top/bottom
        # py will be 0
        py = (w-h)//2

    # creating new image with padding
    newimg = cv2.copyMakeBorder(
        img, py, py, px, px, 
        borderType=cv2.BORDER_CONSTANT, 
        value=[255,255,255]  # white (change this if you want other colors)
    )

    # saving our image
    cv2.imwrite(f'{new_folder}/{filename}', newimg)

运行后的结果文件夹结构

- myimages
  - 1.png
  - 2.png
  - 3.png
  - 4.png
  - 5.png
  - 6.png
- myimages_new
  - 1.png
  - 2.png
  - 3.png
  - 4.png
  - 5.png
  - 6.png
run.py

运行我们的 Python 脚本后,该myimages_new文件夹会自动生成。对于myimages中的每张图片,都会在myimages_new 中生成对应的同名正方形图片。


欢迎大家加入我的这个”AIGC与GPT“知识星球,价格便宜,目前已有近70人

作为一个大厂算法工程师和机器学习技术博主,我希望这个星球可以:

  • 【最全免费资源】免费chatgpt-API,最新AIGC和GPT相关pdf报告和手册。

  • 【最专业算法知识】Transformer、RLHF方法、多模态解读及其论文分享。

  • 【最新变现姿势】如何结合ChatGPT应用落地,各种可以作为副业的AIGC变现方式,打好这个信息差。

  • 【最有趣AICG】ChatGPT+midjourney拍电影,制作壁纸,漫画等等有趣的AICG内 容分享。

另外这里会保存我收集的各种关于AIGC的资源和资料,包括AI绘画-midjourney,ChatGPT, GPT-4,百度-文心一言的各种资料。会保持持续更新,欢迎大家自行拿取。( 网盘地址和密码在知识星球自取






Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/154244
 
280 次点击