python collection (display images, black and white, binary, process excel, draw directed graphs, undirected graphs, weighted graphs, network graphs NetworkX library)

EXCEL data reading code

https://blog.csdn.net/weixin_43820813/article/details/124467183

Network graph, directed graph and undirected graph (only suitable for a small amount of data)

https://blog.csdn.net/tan45du_yuan/article/details/109464240
https://blog.csdn.net/qq_55851911/article/details/124774716

Turn images into black and white in batches

Process pictures in batches and turn color pictures into black and white.
Here you only need to change the address of Path.
The picture is modified directly on the picture, so you can back up the original picture before modifying it.

code show as below:

import cv2
import os
def re_name (path) :
    files = os.listdir (path)
    for i, file in enumerate (files) :
        try:
            new_file_name = os.path.join (path, str(i) + ' . jpg')
            old_file_name = os.path. join (path, file)
            os. rename (old_file_name,new_file_name)
        except:
            continue
def gray_pic(path) :
    files =os.listdir(path)
    for file in enumerate (files) :
        try:
            pic = path +"/"+str(file[1])
            original_img = cv2. imread (pic)
            gray = cv2. cvtColor (original_img, cv2 . COLOR_BGR2GRAY)
            cv2. imwrite (path + "/" + str (file[1]),gray)
        except:
            continue
path = "F:/DATA/archive (kaggle)/Segmented/Pro"
#re_ name (path)
gray_pic (path)

Import and display images

After loading the image, you need to use these two functions to display the image.
Insert image description hereIn fact, the image exists in the computer and is a pixel one by one.
For example, this picture of mine is a two-dimensional array, and the pixel values ​​stored in the array are

Batch binarization

The following is my code for batch processing of images - binary black and white conversion:

You can try to refer to this article: Color image binarization processing

And this article: Binarization of python images

My code:

import cv2
import matplotlib.image as img
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
import os
file_dir = "F:/DATA/PM_400/tain/masks" # 图像文件路径
def imgBinaryThreshold(img, threshold=128):
    rows = img.shape[0]
    cols = img.shape[1]
    for i in range(rows):
        for j in range(cols):
            gray = img[i, j]
            if (gray.all() ==0):
                img[i, j] = 255
            else :
                img[i, j] = 0
    return img.astype(np.uint8)
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        if filename[-4:] in ['.JPG']:  # 判断文件是否为图片格式
            print(filename)  # 仅仅是为了测试
            img = cv2.imread(directory_name + "/" + filename)
            img = imgBinaryThreshold(img)
            #####保存图片#########
            cv2.imwrite("F:/DATA/Masks" + "/" + filename, img)
        else:
            continue
        cv2.imwrite("F:/DATA/PM_400/tain/test" + "/" + filename, img)
if __name__ == "__main__":
    read_directory(file_dir)

It’s just that this code traverses pixel by pixel really! So slow

Guess you like

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