import glob
import cv2
import os
import numpy as np
from PIL import Image
images=[]
images=np.array[images]
path='C:\Users\Quantum\Desktop\test'
count=0
images = [cv2.imread[file,0] for file in glob.glob["E:\homework\Computer vision\Faces\*.jpg"]]
for i in range[len[images]]:
# im = Image.fromarray[images[i]]
# cv2.imwrite[str[path] + '.jpg', images[count]]
cv2.imwrite[os.path.join[path, 'pic.jpg'], images[count]]
count+=1
Cố gắng chọn tất cả các hình ảnh từ một thư mục và hình ảnh được chọn và được chuyển đổi thành thang độ xám mặc dù tôi không biết cách viết những hình ảnh đó vào một thư mục cụ thể.
Đã hỏi ngày 3 tháng 11 năm 2017 lúc 2:22Nov 3, 2017 at 2:22
1
#Multiple Chuyển đổi hình ảnh
import cv2
import os,glob
from os import listdir,makedirs
from os.path import isfile,join
path = '/root/Desktop/Anil' # Source Folder
dstpath = '/root/Desktop/Anil2' # Destination Folder
try:
makedirs[dstpath]
except:
print ["Directory already exist, images will be written in same folder"]
# Folder won't used
files = list[filter[lambda f: isfile[join[path,f]], listdir[path]]]
for image in files:
try:
img = cv2.imread[os.path.join[path,image]]
gray = cv2.cvtColor[img,cv2.COLOR_BGR2GRAY]
dstPath = join[dstpath,image]
cv2.imwrite[dstPath,gray]
except:
print ["{} is not converted".format[image]]
for fil in glob.glob["*.jpg"]:
try:
image = cv2.imread[fil]
gray_image = cv2.cvtColor[os.path.join[path,image], cv2.COLOR_BGR2GRAY] # convert to greyscale
cv2.imwrite[os.path.join[dstpath,fil],gray_image]
except:
print['{} is not converted']
Đã trả lời ngày 14 tháng 8 năm 2018 lúc 5:49Aug 14, 2018 at 5:49
Anil Sahanil SahAnil Sah
1.1728 huy hiệu bạc11 huy hiệu đồng8 silver badges11 bronze badges
import cv2
import glob, os, errno
# Replace mydir with the directory you want
mydir = r'C:\Users\Quantum\Desktop\testoutput'
#check if directory exist, if not create it
try:
os.makedirs[mydir]
except OSError as e:
if e.errno == errno.EEXIST:
raise
for fil in glob.glob["*.jpg"]:
image = cv2.imread[fil]
gray_image = cv2.cvtColor[image, cv2.COLOR_BGR2GRAY] # convert to greyscale
cv2.imwrite[os.path.join[mydir,fil],gray_image] # write to location with same name
Đã trả lời ngày 3 tháng 11 năm 2017 lúc 3:36Nov 3, 2017 at 3:36
GumboygumboyGumboy
4574 Huy hiệu bạc8 Huy hiệu Đồng4 silver badges8 bronze badges
import cv2
from os import listdir,makedirs
from os.path import isfile,join
path = r'C:\Users\fakabbir.amin\Desktop\pdfop' # Source Folder
dstpath = r'C:\Users\fakabbir.amin\Desktop\testfolder' # Destination Folder
try:
makedirs[dstpath]
except:
print ["Directory already exist, images will be written in asme folder"]
# Folder won't used
files = [f for f in listdir[path] if isfile[join[path,f]]]
for image in files:
try:
img = cv2.imread[os.path.join[path,image]]
gray = cv2.cvtColor[img,cv2.COLOR_BGR2GRAY]
dstPath = join[dstpath,image]
cv2.imwrite[dstPath,gray]
except:
print ["{} is not converted".format[image]]
Đoạn mã này sẽ lấy tất cả các hình ảnh từ đường dẫn và viết vào một thư mục khác được đề cập trong Dstpath.path and write into another folder mentioned in dstpath.
Đã trả lời ngày 3 tháng 11 năm 2017 lúc 7:15Nov 3, 2017 at 7:15
import os,cv2
path = r'C:\Users\me\Desktop\folder' # Source Folder
dstpath = r'C:\Users\me\Desktop\desfolder' # Destination Folder
try:
makedirs[dstpath]
except:
print ["Directory already exist, images will be written in asme folder"]
# Folder won't used
files = os.listdir[path]
for image in files:
img = cv2.imread[os.path.join[path,image]]
gray = cv2.cvtColor[img,cv2.COLOR_BGR2GRAY]
cv2.imwrite[os.path.join[dstpath,image],gray]
Đã trả lời ngày 1 tháng 6 năm 2020 lúc 16:54Jun 1, 2020 at 16:54
Prajot Kuvalekarprajot KuvalekarPrajot Kuvalekar
3,8493 huy hiệu vàng14 Huy hiệu bạc26 Huy hiệu đồng3 gold badges14 silver badges26 bronze badges
1