how to separate the images in folder using image name python? - python

I need to separate the images in folder with it's filename in python.
for example, i have a folder of images named as,
0_source.jpg, 0_main.jpg,0_cell.jpg, 1_net.jpg, 1_cells.jpg, 2_image.jpg,2_name.jpg
i want to separate these images and save in different folder like:
folder1:
0_source.jpg, 0_main.jpg, 0_cell.jpg
folder 2:
1_net.jpg, 1_cells.jpg
folder 3:
2_image.jpg, 2_name.jpg
I tried to look around, but seems none of them fits what i really needed.
hope someone here could help.
I'm open to any ideas, recommendation and suggestion, thank you.

The main idea is to use a regular expression to extract the folder name starting from the image name. Then build the folders and move the images
import shutil
import re
import os
images = ["0_source.jpg", "0_main.jpg", "0_cell.jpg", "1_net.jpg", "1_cells.jpg", "2_image.jpg", "2_name.jpg"]
def build_images_folders(image_names: [str]):
folder_name_regex = re.compile(r"^(\d)._*") # Regular expression which extract the folder starting from the image_name name
for image_name in image_names:
regex_match = folder_name_regex.match(image_name)
if regex_match:
folder_index = regex_match.group(1) # Extract the folder name
folder_name = "{folder_prefix}{folder_index}".format(folder_prefix="folder_", folder_index=folder_index) # Build the folder name
os.makedirs(folder_name, exist_ok=True) # Build the folder. If already exists, don't raise exception
src_image_path = image_name
dst_image_path = os.path.join(folder_name, image_name)
shutil.move(src_image_path, dst_image_path)
else:
error = "Invalid image name format \"{image_name}\". Expecting <number>_<string>".format(image_name=image_name)
raise ValueError(error)
if __name__ == '__main__':
build_images_folders(images

Related

Create folder by files year

I have a lot of pictures in a paste following a pattern for the file name, they only differ in the file type which may be .jpg or .jpeg
For instance:
IMG-20211127-WA0027.jpg
IMG-20211127-WA0028.jpeg
IMG-20211127-WA0029.jpg
I'm trying to find a way to create a folder for each year and send the pictures for the respective folder, given that the file name already has its year.
How can I create folders for each year, move the files to the right folder?
I tried to adapt a code from a tutorial, but I'm not getting what I need.
Please see my code below :
from distutils import extension
import os
import shutil
path = "D:\WhatsApp Images"
files = os.listdir(path)
year = os.path.getmtime(path)
for file in files:
filename, extension = os.path.splitext(file)
extension = extension[1:]
if os.path.exists(path+'/'+extension):
shutil.move(path+'/'+file, path+'/'+extension+'/'+file)
else:
os.makedirs(path+'/'+extension)
shutil.move(path+'/'+file,path+'/'+extension+'/'+file)
You can try something like this: See the inline comments for an explanation.
from pathlib import Path
import shutil
import os
path = Path("D:\\WhatsApp Images") # path to images
for item in path.iterdir(): # iterate through images
if not item.suffix.lower() in [".jpg", "jpeg"]: # ensure each file is a jpeg
continue
parts = item.name.split("-")
if len(parts) > 1 and len(parts[1]) > 5:
year = parts[1][:4] # extract year from filename
else:
continue
if not os.path.exists(path / year): # check if directory already exists
os.mkdir(path / year) # if not create the directory
shutil.move(item, path / year / item.name) # copy the file to directory.
I like #alexpdev 's answer, but you can do this all within pathlib alone:
from pathlib import Path
path_to_your_images = "D:\\WhatsApp Images"
img_types = [".jpg", ".jpeg"] # I'm assuming that all your images are jpegs. Extend this list if not.
for f in Path(path_to_your_images).iterdir():
if not f.suffix.lower() in img_types:
# only deal with image files
continue
year = f.stem.split("-")[1][:4]
yearpath = Path(path_to_your_images) / year # create intended path
yearpath.mkdir(exist_ok = True) # make sure the dir exists; create it if it doesn't
f.rename(yearpath / f.name) # move the file to the new location

How to move the pictures in all subfolders to another new same folder in Python?

I plan to move the pictures in all subfolders (as shown in the picture) under the train file train/LUAD to another new folder train_new/LUAD. There are .jpg images in each subfolder such as the first one in the picture TCGA-05-4249-01Z-00-DX1.9fce0297-cc19-4c04-872c-4466ee4024db.
import os
import shutil
count = 0
def moveFiles(path, disdir):
dirlist = os.listdir(path)
for i in dirlist:
child = os.path.join('%s/%s' % (path, i))
if os.path.isfile(child):
imagename, jpg = os.path.splitext(i)
shutil.copy(child, os.path.join(disdir, imagename + ".jpg"))
continue
moveFiles(child, disdir)
if __name__ == '__main__':
rootimage = '/content/drive/MyDrive/stat841_final_data/train/LUAD'
disdir = '/content/drive/MyDrive/stat841_final_data/train_new/LUAD'
moveFiles(rootimage, disdir)
But it does not work. I only got image from the last subfolder except for other subfolders in my new folder train_new/LUAD...
Just to clarify, you want to move (not copy) images from a nested file structure to a new folder, without nesting?
Be aware that this could overwrite images, if multiple images share the same name!
import pathlib
def move_files(source_folder:pathlib.Path, target_folder:pathlib.Path):
target_folder.mkdir(parents=True, exist_ok=True)
for image_file in source_folder.rglob("*.jpg"): # recursively find image paths
image_file.rename(target_folder.joinpath(image_file.name))
If you are unsure maybe use the copy function first, so you won't lose your original data:
import pathlib
import shutil
def move_files(source_folder:pathlib.Path, target_folder:pathlib.Path):
target_folder.mkdir(parents=True, exist_ok=True)
for image_file in source_folder.rglob("*.jpg"): # recursively find image paths
shutil.copy(image_file, target_folder.joinpath(image_file.name))

How can I change all the images in a folder and save the changed images to another folder?

I have a lot of images in a folder. I need to process each image the same way and save the processed images to a different folder. I imagine it something like this:
for i in range(nuber_of_file):
current_image = cv2.imread("path to file")
# transformation
cv2.imwrite("new path", new_image)
I am having difficulty getting the number of files in a folder and getting a new path each time. Can you please tell me how to do this?
You can use:
glob: to get all the files in your directory
rglob: (recursive glob) to get all the files in your directory and all sub directories
Then you can read them with cv2.imread.
here is an example:
from pathlib import Path
import cv2
def main():
destination_path = '/path/to/destination'
target_path = '/path/to/target'
format_of_your_images = 'jpg'
all_the_files = Path(destination_path).rglob(f'*.{format_of_your_images}')
for f in all_the_files:
p = cv2.imread(str(f))
# transformation
cv2.imwrite(f'{target_path}/{f.name}', p)
if __name__ == '__main__':
main()
Hope it was helpful
you can list the files in a folder with os.listdir(dirString). I gives you a list of files names, you can filter them like this :
dl = os.listdir(dirString)
imgList = list()
imgList.append([f for f in dl if ".JPEG" in f or ".jpg" in f or ".png" in f])
Then you get the full path and read the image like this:
img = cv2.imread(os.path.join(dirString, imgList[0]), cv2.IMREAD_COLOR)
Mathieu.

How to access file names from a folder in a jupyter notebook

I am working with jupyter notebook and python, and I have a folder called 'images', with the images inside titled, 'image0', 'image1', 'image2', etc. I would like to access this folder, and see the largest image number inside the folder. How do I access the folder to see the names of the images inside?
I tried:
imagesList = []
for image in images:
imagesList.append(image)
imageNum = []
for image in images:
imageNum.append(int(image[5:]))
max = 0
for item in imageNum:
if item>max:
max = item
print(max)
but am getting 'images is not defined'.
I also tried:
for image in home/jovyan/images:
but this gave me 'home' is not defined.
How do I access the image names within this folder?
Thanks!
import os
folder_files = os.listdir('images') #You can also use full path.
print("This Folder contains {len_folder} file(s).".format(len_folder=len(folder_files)))
for file in folder_files:
#Action with these files
print(file)

Storing the path to folders and inner folders

i'm having difficulties trying to read from sub-folders that are inside a folder. What im trying to do is: i have my path "C:\Dataset" which has 2 folders inside them and inside both folders, i have person names that have pictures for example: "C:\Dataset\Drunk\JohnDoe\Pic1", "C:\Dataset\Sober\JaneDoe\Pic1". I want to be able to read each picture and store them in a path variable.
At the moment, what i got so far, basically, i get the images as long as they are inside Drunk and Sober only, for instance: 'C:\Dataset\Drunk\Pic1', and the code that i am using to do is this:
DATADIR = "C:\Dataset"
CATEGORIES = ["Positive", "Negative"]
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for img in os.listdir(path):
img_path = os.path.join(path,img)
img_data = open(img_path, 'r').read()
break
break
Basically, what i am trying to do is that when i iterate inside Drunk folder it also iterates inside the inner folders, reading the path to the pictures that are in C:\Dataset\Drunk\JohnDoe\nthPic, C:\Dataset\Drunk\JoeDoe\nthPic, C:\Dataset\Drunk and Sober \nthJoe\nthPic C:\Dataset\Drunk\JamesDoe\nthPic. Therefore, when I do the reading, it grabs the whole folder map
This is basically what my goal is.
You need one nesting more:
It saves all images in the dictionary images, key is the full path.
DATADIR = "C:\Dataset"
CATEGORIES = ["Drunk", "Sober"]
images = {}
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for person in os.listdir(path):
personfolder = os.path.join(path, person):
for imgname in os.listdir(personfolder):
fullpath = os.path.join(personfolder, imgname)
images[fullpath] = open(fullpath, 'r').read()

Categories