程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to realize batch processing of Python images

編輯:Python

python How to realize batch processing of images

This article mainly introduces python How to realize the batch processing of images , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article python How to realize the batch processing of images, the article will gain something , Let's have a look .

Picture collection function

skimage.io.ImageCollection(load_pattern,load_func=None)

This function is placed in io In module , With two parameters , The first parameter load_pattern, Indicates the path of the picture group , It could be a str character string . The second parameter load_func Is a callback function , We can batch process images through this callback function . The callback function defaults to imread(), That is, the default function is to read pictures in batches .

Let's look at an example :

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

The result is 25, It means that the system comes with 25 Zhang png A sample picture of , These pictures have been read out , Put it in the picture collection coll in . If we want to show one of the pictures , You can add a line of code after it :

io.imshow(coll[10])

Is shown as :

Bulk read

If a folder , We have some jpg Format picture , Some more png Format picture , Now I want to read them all , How do you do that ?

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

Notice this place 'd:/pic/*.jpg:d:/pic/*.png' , Is a combination of two strings ,

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

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

Together , The middle is separated by a colon , So we can take d:/pic/ Under folder jpg and png Format pictures are read out .

If you want to read pictures stored in other places , It can also be added together , But the middle is also separated by colons .

io.ImageCollection() This function omits the second argument , That is, batch reading . If we don't want to read in bulk , But other batch operations , Such as batch conversion to grayscale image , So how to do it ?

Batch conversion to grayscale image

You need to define a function first , Then take this function as the second argument , Such as :

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

This batch operation is extremely useful for video processing , Because video is a series of pictures

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

What this code means , Will be myvideo.avi Every... In this video 10 Frame picture read out , Put in the picture collection .

After getting the picture collection , We can also connect these pictures , Form a higher dimensional array , The function to connect pictures is :

skimage.io.concatenate_images(ic)

With one parameter , Is the above picture collection , Such as :

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

Use concatenate_images(ic) The precondition of the function is that the size of the read pictures must be the same , Otherwise it will go wrong . Let's look at the dimensional changes before and after the picture connection :

from skimage import data_dir,io,colorcoll = io.ImageCollection('d:/pic/*.jpg')print(len(coll))      # Number of connected pictures print(coll[0].shape)   # Picture size before connection , All the same mat=io.concatenate_images(coll)print(mat.shape)  # Array size after connection 

Show results :

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

You can see , take 2 individual 3 Dimension group , Connected into a 4 Dimension group

If we do batch operation on the pictures , You want to save the results after the operation , It can also be done .

Save in bulk

example : Take all of the system's own png The sample picture , All converted to 256*256 Of jpg Format grayscale , Save in d:/data/ Under the folder

Change the size of the picture , We can use tranform Modular resize() function , This module will be discussed later .

from skimage import data_dir,io,transform,colorimport numpy as npdef convert_gray(f):     rgb=io.imread(f)    # Read... In turn rgb picture      gray=color.rgb2gray(rgb)   # take rgb Image to grayscale      dst=transform.resize(gray,(256,256))  # Convert gray image size to 256*256     return dststr=data_dir+'/*.png'coll = io.ImageCollection(str,load_func=convert_gray)for i in range(len(coll)):    io.imsave('d:/data/'+np.str(i)+'.jpg',coll[i])  # Cycle through saving pictures 

  result :

About “python How to realize batch processing of images ” That's all for this article , Thank you for reading ! I'm sure you're right “python How to realize batch processing of images ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved