这里的问题是,这仅用于一个图像,我需要对其进行优化,以便可以存储多个图像。 (它们的宽度,高度等)
我不太懂python。大约4年前,我已经在研究它,但是现在我几乎忘记了大部分语法。
def __init__(self, im):
self.image = im
self.height, self.width, self.nbchannels = im.shape
self.size = self.width * self.height
self.maskONEValues = [1,2,4,8,16,32,64,128]
#Mask used to put one ex:1->00000001, 2->00000010 .. associated with OR bitwise
self.maskONE = self.maskONEValues.pop(0) #Will be used to do bitwise operations
self.maskZEROValues = [254,253,251,247,239,223,191,127]
#Mak used to put zero ex:254->11111110, 253->11111101 .. associated with AND bitwise
self.maskZERO = self.maskZEROValues.pop(0)
self.curwidth = 0 # Current width position
self.curheight = 0 # Current height position
self.curchan = 0 # Current channel position
我想将文件路径(包含这些图像)中的多个图像(宽度,高度等)存储在数组中
答案 0 :(得分:0)
尝试:-
from PIL import Image
import os
# This variable will store the data of the images
Image_data = []
dir_path = r"C:\Users\vasudeos\Pictures"
for file in os.listdir(dir_path):
if file.lower().endswith(".png"):
# Creating the image file object
img = Image.open(os.path.join(dir_path, file))
# Getting Dimensions of the image
x, y = img.size
# Getting channels of the image
channel = img.mode
img.close()
# Adding the data of the image file to our list
Image_data.append(tuple([channel, (x, y)]))
print(Image_data)
只需使用图像文件的目录更改dir_path
变量。此代码在该文件唯一的独立元组中存储图像的颜色通道和尺寸。并将元组添加到列表中。
PS: 元组格式=(通道,尺寸)