【问题标题】:convert images black background to white and white to black in python在python中将图像黑色背景转换为白色,将白色转换为黑色
【发布时间】:2020-09-18 18:32:14
【问题描述】:

我需要使用 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

【问题讨论】:

  • 请在您的问题下单击edit,并通过将代码行缩进4个空格来正确格式化。谢谢。
  • 请将您删除的所有import 语句放回原处,以便其他人可以运行您的代码。另外,请解释什么不起作用,或者你得到了什么结果。谢谢。

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


【解决方案1】:

导入操作系统,系统 从 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")

显示(out_rgb)

【讨论】: