Python PIL 读取图片文件并打印所有像素点的像素值

from PIL import Image

f = open("o.txt", 'w+')  
im = Image.open('E:\\remote_sensing_image\\seg_train\\train\\label\\13801.png')
rgb_im = im.convert('RGB')
for i in range(256):
	for j in range(256):
		r, g, b = rgb_im.getpixel((i, j))
		print(r, g, b,file=f)

批量处理文件夹下的图片:

from PIL import Image
import os

img_folder = "D://"

f = open("o.txt", 'w+')

imlist = os.listdir(img_folder)
imlist.sort()

for imagename in imlist:
	imurl = os.path.join(img_folder,imagename)
	print(imurl)
	im = Image.open(imurl)
	rgb_im = im.convert('RGB')
	for i in range(256):
		for j in range(256):
			r, g, b = rgb_im.getpixel((i, j))
			print(r, g, b,file=f)

 

你可能感兴趣的:(python)