#1
from PIL import Image
import numpy as np
img=Image.open(‘png’)#图片路径
img_array=np.array(img)
print(len(img_array.nonzero()[0]))
#2
def RGB2GRAY(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
#3
from PIL import Image
import os, glob
def batch_image(in_dir, out_dir):#PGM读入路径,存储路径
if not os.path.exists(out_dir):
print(out_dir, ‘is not existed.’)
os.mkdir(out_dir)
if not os.path.exists(in_dir):
print(in_dir, 'is not existed.')
return -1
count = 0
for files in glob.glob(in_dir+'/*'):
filepath, filename = os.path.split(files)
out_file = filename[0:9] + '.jpg'
# print(filepath,',',filename, ',', out_file)
im = Image.open(files)
new_path = os.path.join(out_dir, out_file)
print(count, ',', new_path)
count = count + 1
im.save(os.path.join(out_dir, out_file))
if name==‘main’:
batch_image(‘D:/F/Desktop/romadata’, ‘D:/F/Desktop/romadate’)