Python batches modify image pixels, modify names, binarize, and divide data sets

Modify naming

It is modified directly under the source folder, so remember to copy the file in advance before processing.

#-----------------------------批量重命名图片------------------------------------
#-----------重命名《图片》《xml文件》《任何格式的文件》都可以自定义序号来命名------------
import os
import cv2 as cv
# 导入需要的模块
from glob import glob
from PIL import Image
# 存储(输出)路径
##path_save = "F://A-MLearn//archiveDack//Origtestall//all"
path = 'F://A-MLearn//archiveDack//sg//Pro//'
# 绝对路径
filelist = os.listdir(path)
'''
起始数字,重命名的第一个文件的名字会加1
'''
i = 0
for item in filelist:
        print(i);
        if item.endswith('.jpg'):
                print(i);
                i = i + 1
                # 第一张图片命名为1.png
                name = str(i)
                # 将数字转换为字符串才能命名
                src = os.path.join(os.path.abspath(path),item)
                # 原始图像的路径
                dst = os.path.join(os.path.abspath(path),name + '.jpg')
                # 目标图像路径
        try:
                os.rename(src,dst)
                print('rename from %s to %s'%(src,dst))
                # 将转换结果在终端打印出来以便检查
        except:
                continue

Batch modify the pixels of all pictures in a folder

Image resolution refers to the amount of information stored in the image, which is the number of pixels per inch of the image. The unit of resolution is PPI (Pixels Per Inch), which is usually called pixels per inch. Image resolution is generally used in PS to change the clarity of the image.

The size of the image resolution will affect the progress of our experiment. Modifying the appropriate resolution can make the experiment perfect.

进行下去。
from glob import glob
from PIL import Image
import os
###
img_path = glob("F:/A-MLearn/archiveDack/sg/all/*.jpg")#对目录内容进行匹配,*.jpg表示获取当前目录下所有的jpg格式图片
# path_save = "Cutimgs/1-outer/"#存放新文件的文件夹,需要自己提前创建
def produceImage(file_in, width, height, file_out):
    image = Image.open(file_in)
    resized_image = image.resize((width, height), Image.ANTIALIAS)
    resized_image.save(file_out) # #修改并保存图片
for file_in in img_path:  #批量处理
    width = 256  # 调整的分辨率大小
    height = 256
    # 分辨率
    produceImage(file_in, width, height, file_in)

Reference links:
Link one:
https://blog.csdn.net/qq_32394351/article/details/90212002
Link two:
https://blog.csdn.net/qq_45222550/article/details/125025338?spm=1001.2014.3001.5502

Batch binarization

import os
import cv2
from PIL import Image
def binarization():
    # 获取目录下所有图片名
    filename = os.listdir(r"F:\A-MLearn\archiveDack\sg\all")
    print(filename)
    # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。
    base_dir = r"F:\A-MLearn\archiveDack\sg\all"  # input
    new_dir = r"F:\A-MLearn\archiveDack\sg\all1"  # output
    for img in filename:
        name = img
        path1 = os.path.join(base_dir, img)
        img = cv2.imread(path1)
        # print(img)
        Grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(Grayimg, 255, 255, cv2.THRESH_TOZERO_INV) #这个函数十分重要
        cv2.imwrite('img.png', thresh)
        image = Image.open('img.png')
        # 有需要可对图像进行大小调整
        # image = image.resize((350, 350),Image.ANTIALIAS)
        path = os.path.join(new_dir, name)
        image.save(path)

binarization()

Reference link:
https://www.cnblogs.com/april0315/p/13576778.html

https://blog.csdn.net/m0_61899108/article/details/123006032

There is something wrong with this binarization.

Guess you like

Origin blog.csdn.net/qq_52626583/article/details/127730171