简体   繁体   中英

new in python. I want to apply an algorithm to different images from a folder and save the new ones in another folder. Biology research images

I want to inverse colors in a black and white image and then change the background in transparent, with the following codes:

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')

and

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")

Then I would like to iterate this in a several number of images that are in a folder. Then I would like to save the new images with the changes in another folder. But I do not find a way to make it work.

Not sure I understand your question properly but I think you can do the following. First you bundle both operations into one function:

from PIL import Image

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

This function will open an image, apply the changes you mentioned and then save it in the specified path. You can then make this process automatic by using the following code:

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)

"originalfolder" is the folder where your original images are. The format should be something like "C:/Users/yourfolder"

"desfolder" is the folder where the new images will be stored. The format should be something like "C:/Users/yournewfolder"

You can use pathlib for this, assuming apply_algo is a function that takes input a path object for input image and returns transformed PIL.Image object this should work.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM