python中的新功能我想对一个文件夹中的不同图像应用一种算法,然后将新图像保存到另一个文件夹中生物学研究图像

如何解决python中的新功能我想对一个文件夹中的不同图像应用一种算法,然后将新图像保存到另一个文件夹中生物学研究图像

我要反转黑白图像的颜色,然后使用以下代码将背景更改为透明:

imgg = Image.open('HSPl4_E5_LP8.png')
data = np.array(imgg)

converted = np.where(data == 255,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,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")

然后,我想在一个文件夹中的多个图像中进行迭代。然后,我想将带有更改的新图像保存到另一个文件夹中。但是我找不到使它起作用的方法

解决方法

不确定我是否正确理解了您的问题,但我认为您可以执行以下操作。 首先,您将这两种操作捆绑为一个功能:

from PIL import Image

def imageTransform(imgfile,destfolder):
    img = Image.open(imgfile)
    data = np.array(img)
    converted = np.where(data == 255,255)
    img = Image.fromarray(converted.astype('uint8'))
    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,0))
        else:
            newData.append(item)      
    img.putdata(newData)
    img.save(destfolder+"/"+imgfile,"PNG")

此功能将打开图像,应用您提到的更改,然后将其保存在指定的路径中。然后,您可以使用以下代码自动执行此过程:

import os

originalfolder = "folderpath"  #place your folder path as string
destfolder = "folderpath" #place your destination path as string
directory = os.fsencode(originalfolder)    
for file in os.listdir(directory):
    filename = os.fsdecode(file)
    imageTransform(file,destfolder)

“原始文件夹”是原始图像所在的文件夹。格式应类似于"C:/Users/yourfolder"

“解折叠器”是将要存储新图像的文件夹。格式应类似于"C:/Users/yournewfolder"

,

为此,您可以使用pathlib,假设apply_algo是一个函数,该函数接受输入图像的路径对象并返回转换后的PIL.Image对象,这应该可以工作。

from pathlib import Path


def process_files(source: str,dstn: str):
    dstn = Path(dstn)
    source = Path(source)
    # check if input strings are directories or not.
    if not (source.is_dir() and dstn.is_dir()):
        raise Exception("Source and Dstn must be directories")
    # use rglob if you want to pick files from subdirectories as well
    for path in source.glob("*"):
        if path.is_file():
            output_img = apply_algo(path)
            output_img.save(dstn / path.name(),"PNG")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?