Python Digital Image processing (6): Batch processing of images

Source: Internet
Author: User

Sometimes, not only do we have to deal with a picture, we may also process a batch of pictures. At this point, we can do the processing through loops, or we can call the collection of pictures that the program comes with.

The picture collection functions are:

skimage.io.ImageCollection(Load_pattern,load_func=none)

This function is placed in the IO module, with two parameters, the first parameter Load_pattern, representing the path of the picture group, can be a str string. The second parameter, Load_func, is a callback function, which we can do with the batch processing of the image. The callback function defaults to Imread (), which means that the default function is to bulk read the picture.

Let's look at an example:

Import Skimage.io as IO  from Import Data_dirstr ' /*.png '  = io. ImageCollection (str)print(Len (coll))

The result is 25, indicating that the system comes with 25 PNG sample images, which are read out and placed in the coll of the picture Collection. If we want to display one of the pictures, we can add a line of code to it:

Io.imshow (coll[10])

Shown as:

If a folder, we have both a number of images in JPG format, but also stored in some PNG format pictures, now want to read them all out, how to do?

Import Skimage.io as IO  from Import data_dirstr='d:/pic/*.jpg:d:/pic/*.png'= io. ImageCollection (str)print(Len (coll))

Note that this place ' d:/pic/*.jpg:d:/pic/*.png ', is two strings together, the first one is ' d:/pic/*.jpg ', the second is ' d:/pic/*.png ', together, the middle with a colon to separate, so you can put d:/ Both JPG and PNG format images are read in the pic/folder. If you still want to read pictures stored elsewhere, you can add them together, but the middle is also separated by a colon.

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

Then you need to define a function and then use the function as the second parameter, such as:

 from Import Data_dir,io,color def Convert_gray (f):    rgb=io.imread (f)    return  color.rgb2gray (RGB)    str =data_dir+'/*.png'= io. ImageCollection (str,load_func=convert_gray) io.imshow (coll[10])

This batch operation is extremely useful for video processing because the video is a series of picture combinations

 from Import Data_dir,io,color class Aviloader:     ' Myvideo.avi '    def __call__ (self, frame):         return  =#  0, ten, ... IC =io. ImageCollection (frames, load_func=avi_load)

The meaning of this code is to myvideo.avi this video every 10 frames of the picture read out, put in the picture collection.

After we get the picture collection, we can also connect the images to form a higher-dimensional array, and the function to connect the pictures is:

skimage.io.concatenate_images (IC)

Take one parameter, which is the picture collection above, such as:

 from Import  = io. ImageCollection ('d:/pic/*.jpg')Mat=io.concatenate_images (coll)

The concatenate_images(IC) function is used only if the dimensions of these images must be identical, otherwise an error will occur. Let's look at the dimension changes before and after the image is connected:

 from Import  = io. ImageCollection ('d:/pic/*.jpg')print(Len (coll))      # number of pictures connected Print (Coll[0].shape)   # picture size before connection, all the same mat=io.concatenate_images (coll)print(mat.shape)  #  the size of the array after the connection

Show Results:

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

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

If we make a batch operation on the picture, we want to save the result after the operation, it can be done.

Example: all PNG sample images from the system are converted to 256*256 jpg grayscale, saved in the d:/data/folder

To change the size of the image, we can use the resize () function of the Tranform module, which will be discussed later.

 fromSkimageImportData_dir,io,transform,colorImportNumPy as NPdefConvert_gray (f): RGB=io.imread (f)#read RGB pictures sequentiallyGray=color.rgb2gray (RGB)#convert an RGB image to a grayscale imageDst=transform.resize (Gray, (256,256))#convert grayscale picture size to 256*256     returnDST str=data_dir+'/*.png'Coll= Io. ImageCollection (str,load_func=Convert_gray) forIinchRange (len (coll)): Io.imsave ('d:/data/'+np.str (i) +'. jpg', Coll[i])#Loop Save Picture

Results:

Python Digital Image processing (6): Batch processing of images

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.