我想反转黑白图像中的颜色,然后用以下代码更改透明背景:
imgg = Image.open('HSPl4_E5_LP8.png')
data = np.array(imgg)
converted = np.where(data == 255, 0, 255)
imgg = Image.fromarray(converted.astype('uint8'))
imgg.save('new HSPl4_E5_LP8.png')
及
from PIL import Image
img = Image.open('new HSPl4_E5_LP8.png')
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))#0 és la alfa de rgba i significa 0 opacity.
else:
newData.append(item)
img.putdata(newData)
img.save("HSPl4_E5_LP8 transparent.png", "PNG")
然后我想在一个文件夹中的几个图像中重复这个过程。然后,我想将新图像和更改保存在另一个文件夹中。但我没有找到一个办法让它发挥作用
我不确定我是否正确理解你的问题,但我认为你可以做到以下几点。 首先,将两个操作捆绑到一个函数中:
此函数将打开一个图像,应用您提到的更改,然后将其保存在指定的路径中。然后,您可以使用以下代码自动执行此过程:
“originalfolder”是原始图像所在的文件夹。格式应该类似于
"C:/Users/yourfolder"
“desfolder”是存储新图像的文件夹。格式应该类似于
"C:/Users/yournewfolder"
假设apply_algo是一个函数,它为输入图像获取一个路径对象,并返回转换后的PIL.image对象,则可以使用pathlib实现此目的
相关问题 更多 >
编程相关推荐