Ubuntu下使用python+opencv 批量修改图片为320 240 的并保存到image2的文件夹下。
#coding:utf-8
import os
import cv2
def ChangeFileName(path,Qianzui,Houzui):#, dist, pos_or_neg
filelist = os.listdir(path)
# print("filelist:",filelist)
count=0
for file in filelist:
print(file)
for file in filelist:
Olddir=os.path.join(path,file)
if os.path.isdir(Olddir):
continue
filename=os.path.splitext(file)[0]
filetype=os.path.splitext(file)[1]
Newdir=os.path.join(path,Qianzui+str(count).zfill(1)+Houzui+filetype)
os.rename(Olddir,Newdir)
count+=1
def cutPicture():
filelist = os.listdir(path)
"""简化版,读到文件名之后不保存,立即进行操作"""
count=7
for file in filelist:
print(file)
for file in filelist: # 文件遍历
line = path+ '/'+ file
print(line)
image = cv2.imread(line) # 读取文件名对应的图片
cropImg = image[0:288,0:360] # 裁剪 # 先y范围,后x范围,每个的第三个为采样步长
cv2.imwrite("image1/" + str(count).zfill(1) + ".jpg", cropImg) # 保存
count+=1
def resizePicture():
filelist = os.listdir(path)
"""简化版,读到文件名之后不保存,立即进行操作"""
count=7
for file in filelist:
print(file)
for file in filelist: # 文件遍历
line = path+ '/'+ file
print(line)
image = cv2.imread(line) # 读取文件名对应的图片
new_image = cv2.resize(image,(320,240))
cv2.imwrite("image2/" + str(count).zfill(1) + ".jpg", new_image) # 保存
count+=1
path = "/home/lid/share/cannypicture/image1"
resizePicture()
#ChangeFileName(path + "/image","jpg","") # 你的路径, 前缀,后缀
#ChangeFileName(path + "/com","jpg","")
#ChangeFileName(path + "/banzi","banzi","")
最新评论