我試圖將多個影像放在黑白中,但所有影像都變得全黑,我無法弄清楚錯誤在哪里:
def bw():
#images = [cv2.imread(file) for file in glob.glob("C:/PythonProjects/*frame.jpg")]
img_dir = "C:/PythonProjects" # Directory of all images
data_path = os.path.join(img_dir, '*frame.jpg') #Filter becouse I only want some type of images
files = glob.glob(data_path)
data = []
plus = Image.open("124frame.jpg") #getting the image just to get his size for the FOR cycle
for j in files:
img = cv2.imread(j)
data.append(img) #Save the images into a list
for i in range(0, 130): #128 are the numbers of images I want to work with
img_data = data[i] #Select image by image from the list
# Run the image
lst = []
for j in img_data:
lst.append(j[0] * 0.2125 j[1] * 0.7169 j[2] * 0.0689) #Black and White algorithm
#Using the pixels then saving them to a List
#New Image
new_image = Image.new("L", plus.size)
new_image.putdata(lst) #Put the data from the list to the new image
new_image = numpy.array(new_image)
#Save the image
cv2.imwrite("bwframe%d.jpg" % i, new_image)
uj5u.com熱心網友回復:
我相信你的問題j不是單個像素,而是一排像素。因此,添加到的每個專案lst將是前三個像素的加權平均值。
相反,您可以遍歷行。
lst = []
for x in img_data:
for y in x:
lst.append(y[0] * 0.2125 y[1] * 0.7169 y[2] * 0.0689)
或者您也可以將整個事情簡化為串列理解。
lst = [y[0] * 0.2125 y[1] * 0.7169 y[2] * 0.0689 for y in x for x in img_data]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/394349.html
