在python中将图像从黑色背景转换为白色并将白色转换为黑色

时间:2020-09-18 18:32:14

标签: python-3.x image matplotlib python-imaging-library

enter image description here

我需要使用python libaray将图像中的黑色背景转换为白色,将白色字符转换为黑色。我尝试了ImageOps.invert

im = Image.open('aa_comressed_32.png').convert('RGB')

im_invert = ImageOps.invert(im)

im_invert.save('aa_comressed_32opinvert.png')

或尝试在matlab上使用:

f = misc.face(gray=True)  # retrieve a grayscale image

import matplotlib.pyplot as plt

plt.imshow(f, cmap=plt.cm.gray

1 个答案:

答案 0 :(得分:0)

导入os,sys 从PIL导入图像

im = Image.open(“ image_path”)

newsize =(128,128) im1 = im.resize(newsize)

#im1_new = add_margin(im1,20,20,20,20,(255,255,255))

显示(im1) out = im1.convert(“ RGB”) out_rgb = out.convert(“ L”)

display(out_rgb)

相关问题