'How can I save images with different name on different directory using PIL .save?

I'm trynna get the train_img and ground truth img from directory './train_dataset/train_img_cropped' & './train_dataset/train_gt_cropped'. Next, I wanna save the both original image and flipped one with a '_0', '_1'tail on its name in directory './train_dataset/train_img_preprocessed' & './train_dataset/train_gt_preprocessed'. But there's an Error of changing names (file + "_0" or "_1") as an unknown file extension. Looks like somehow PIL recognizes _0, _1 as a extension. Is there anybody who can help me to save with changing the name?

import os
import os.path
import glob

from PIL import Image

def preprocess(img_path, save_path):
targetdir = img_path
files = os.listdir(targetdir)

format = [".png"]
for (path, dirs, files) in os.walk(targetdir):

    for file, i in files:
        if file.endswith(tuple(format)):
            image = Image.open(path + "/" + file)
            image.save(save_path + "/" + file)

            flippedImage = image.transpose(Image.FLIP_LEFT_RIGHT)
            flippedImage.save(save_path + "/" + file)

            print(file + " successfully flipped!")
        
        else:
            print(path)
            print("InValid", file)

if __name__ == "__main__":
train_img_cropped_path = './train_dataset/train_img_cropped'
train_img_preprocessed_path = './train_dataset/train_img_preprocessed'

train_gt_cropped_path = './train_dataset/train_gt_cropped'
train_gt_preprocessed_path = './train_dataset/train_gt_preprocessed'



preprocess(train_img_cropped_path, train_img_preprocessed_path)
preprocess(train_gt_cropped_path, train_gt_preprocessed_path)


Solution 1:[1]

Chances are your dao.findNode method results in a Future[Option[MyNode]], so the Await.result makes f1 an Option[MyNode]. It's not that clear what type you want, but you can get a Future[(Option[MyNode], LookupResult)] with this

dao.findNode("nodeA"), 5.seconds)
  .flatMap { nodeOpt: Option[MyNode] =>
    nodeOpt.filter(_.isUpAndRunning)
      .map { node =>
        // node exists and is up and running
        val finalResult = nodeOpt -> LookupResult(true, node.varA, node.varB)

        // it's much more efficient in this scenario to use
        // Future.successful than Future { }
        Future.successful(finalResult)
      }
      .getOrElse {
        // node didn't exist, or wasn't up and running
        val lookupFut = lk ? Lookup(sm.id, sm.("address"))
        lookupFut.map { lookupResult =>
          nodeOpt -> lookupResult
        }
      }
  }

The rough outline of what's happening is:

  • use dao to find the node
  • schedule a callback for when we get the (possibly nonexistent) node to check if it's up and running (in which case we short-circuit to our now-known final result), otherwise perform the lookup and build a final result if that lookup succeeds

Future.successful is used because Future {} will almost certainly incur more overhead scheduling a task on a thread and completing the future than constructing the final result.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Levi Ramsey