SoFunction
Updated on 2024-11-14

Python digital image processing batch processing of images

main body (of a book)

There are times when we have to process not only a single image, but possibly a batch of images. At this point, we can perform processing through a loop, or we can call the program's own collection of images to process.

Image Collection Functions

(load_pattern,load_func=None)

This function is placed in the io module, with two parameters, the first parameter load_pattern, represents the path of the picture group, can be a str string. The second parameter load_func is a callback function, we can batch process the images through this callback function. The default callback function is imread(), which means the default function is to read images in bulk.

Let's look at an example first:

import  as io
from skimage import data_dir
str=data_dir + '/*.png'
coll = (str)
print(len(coll))

The result is 25, which means that the system comes with 25 png example images, which are read out and placed in the image collection coll. If we want to display one of the pictures, we can add a line of code after it:

(coll[10])

Show as:

batch read

If in a folder we have stored both some images in jpg format and some images in png format, and now we want to read them all out, what should we do?

import  as io
from skimage import data_dir
str='d:/pic/*.jpg:d:/pic/*.png'
coll = (str)
print(len(coll))

Note the place 'd:/pic/*.jpg:d:/pic/*.png', which is two strings put together.

The first is 'd:/pic/*.jpg', the

The second one is 'd:/pic/*.png'.

When combined, they are separated by a colon so that you can read out both the jpg and png format images in the d:/pic/ folder.

If you also want to read images stored elsewhere, you can add them all together, only again separated by colons.

() This function omits the second parameter, which is batch read. If we do not want to batch read, but other batch operations, such as batch conversion to grayscale, then what to do?

Batch conversion to grayscale

Then you need to define a function first and then take that function as the second parameter, e.g.:

from skimage import data_dir,io,color
def convert_gray(f):
    rgb=(f)
    return color.rgb2gray(rgb)
str=data_dir+'/*.png'
coll = (str,load_func=convert_gray)
(coll[10])

This kind of batch operation is extremely useful for video processing, since video is a series of image combinations

from skimage import data_dir,io,color
class AVILoader:
    video_file = ''
    def __call__(self, frame):
        return video_read(self.video_file, frame)
avi_load = AVILoader()
frames = range(0, 1000, 10) # 0, 10, 20, ...
ic =(frames, load_func=avi_load)

What this code means, is to read out every 10th frame of this video and put it in the image collection.

After getting the collection of images, we can also connect these images to form a higher dimensional array, the function to connect the images is:

.concatenate_images(ic)

With a parameter that is the above collection of images like:

from skimage import data_dir,io,color
coll = ('d:/pic/*.jpg')
mat=io.concatenate_images(coll)

The prerequisite for using the concatenate_images(ic) function is that the dimensions of these images read must be the same, otherwise an error will occur. Let's look at the change in dimension before and after the images are concatenated:

from skimage import data_dir,io,color
coll = ('d:/pic/*.jpg')
print(len(coll))      # of images connected
print(coll[0].shape)   #Picture size before connecting, all the same
mat=io.concatenate_images(coll)
print()  #Array size after concatenation

Show results:

2
(870, 580, 3)
(2, 870, 580, 3)

As you can see, two 3-dimensional arrays are concatenated into a 4-dimensional array

If we want to save the results of a batch operation on images, we can do that too.

batch save

Example: Take all the png sample images that come with the system, convert them all to 256*256 jpg grayscale images, and save them in the d:/data/ folder.

To change the size of an image, we can use the resize() function of the tranform module, which will be talked about later.

from skimage import data_dir,io,transform,color
import numpy as np
def convert_gray(f):
     rgb=(f)    # Read rgb images in order
     gray=color.rgb2gray(rgb)   #Convert rgb images to grayscale
     dst=(gray,(256,256))  # Convert grayscale image size to 256*256
     return dst
str=data_dir+'/*.png'
coll = (str,load_func=convert_gray)
for i in range(len(coll)):
    ('d:/data/'+(i)+'.jpg',coll[i])  #Cyclic saving of images

Results:

Above is the python digital image processing of the image of the batch processing of the details, more information about python digital image batch processing please pay attention to my other related articles!