[Python programming] Image cropping

import the necessary modules

import os
import cv2

Path variable defining the image to be cropped

DATADIR = r"E:\系统默认\桌面\pytorch-CycleGAN-and-pix2pix123\datasets\RGB2ToF\\"
data_k = "trainA"
path = os.path.join(DATADIR, data_k)

os.listdir() function: Returns a list of the names of the files or folders contained in the specified folder.

img_list = os.listdir(path)     

loop through files in folder

for i in img_list:
    img1 = cv2.imread(path + '/' + i )  # 读取图片, cv2.IMREAD_GRAYSCALE是以灰度属性读入 
    cv2.imshow("Original Image",img1 ) # 展示图片
    
    start_row,start_col=150,150     # start_row和start_col是开始坐标 
    end_row,end_col=1150,1150       # end_row和end_col是结束坐标 
    cropped=img1[start_row:end_row,start_col:end_col] # 裁切
    cv2.imshow("Cropped_Image",cropped) # 展示

    '''生成图片存储的目标路径'''
    img_name = str(i)          # 以原文件名命名保存
    save_path = path + '3/'
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    save_img = save_path + img_name
    cv2.imwrite(save_img, cropped)
    cv2.waitKey(1)
    cv2.destroyAllWindows()

Convert an image with RGB attributes to HSV attributes

data_k1 = "trainA3"
path1 = os.path.join(DATADIR, data_k1)
img_list1 = os.listdir(path1)     
for i in img_list1:
    img1 = cv2.imread(path1 + '/' + i)
    cv2.imshow('img0',img1)
    img2 = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV)
    cv2.imshow('img2_2',img2)
    img_name1 = str(i)
    save_path = path1 + '3_3/'
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    save_img1 = save_path + img_name1
    cv2.imwrite(save_img1, img2)
    cv2.waitKey(1)
    cv2.destroyAllWindows()

full code

import cv2
import random
import numpy as np
import os

DATADIR = r"E:\系统默认\桌面\pytorch-CycleGAN-and-pix2pix123\datasets\RGB2ToF\\"
data_k = "trainA"
path = os.path.join(DATADIR, data_k)

img_list = os.listdir(path)     

for i in img_list:
    img1 = cv2.imread(path + '/' + i )  # , cv2.IMREAD_GRAYSCALE
    cv2.imshow("Original Image",img1 )
    
    start_row,start_col=150,150     # start_row和start_col是开始坐标 
    end_row,end_col=1150,1150       # end_row和end_col是结束坐标 
    cropped=img1[start_row:end_row,start_col:end_col]
    cv2.imshow("Cropped_Image",cropped)

    '''生成图片存储的目标路径'''
    img_name = str(i)
    save_path = path + '3/'
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    save_img = save_path + img_name
    cv2.imwrite(save_img, cropped)
    cv2.waitKey(1)
    cv2.destroyAllWindows()

# 将rgb属性的图片转换为HSV
data_k1 = "trainA3"
path1 = os.path.join(DATADIR, data_k1)
img_list1 = os.listdir(path1)     
for i in img_list1:
    img1 = cv2.imread(path1 + '/' + i)
    cv2.imshow('img0',img1)
    img2 = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV)
    cv2.imshow('img2_2',img2)
    img_name1 = str(i)
    save_path = path1 + '3_3/'
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    save_img1 = save_path + img_name1
    cv2.imwrite(save_img1, img2)
    cv2.waitKey(1)
    cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/lingchen1906/article/details/131386944