In my file, I have a large number of images in jpg format and they are named [fruit type].[index].jpg.
Instead of manually making three new sub folders to copy and paste the images into each sub folder, is there some python code that can parse through the name of the images and choose where to redirect the image to based on the fruit type in the name, at the same time create a new sub folder when a new fruit type is parsed?
Before
TrainingSet (file)
apple.100.jpg
apple.101.jpg
apple.102.jpg
apple.103.jpg
peach.100.jpg
peach.101.jpg
peach.102.jpg
orange.100.jpg
orange.101.jpg
After
TrainingSet (file)
apple(file)
apple.100.jpg
apple.101.jpg
apple.102.jpg
apple.103.jpg
peach(file)
peach.100.jpg
peach.101.jpg
peach.102.jpg
orange(file)
orange.100.jpg
orange.101.jpg
Here’s the code to do just that, if you need help merging this into your codebase let me know:
import os, os.path, shutil
folder_path = "test"
images = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
for image in images:
folder_name = image.split('.')[0]
new_path = os.path.join(folder_path, folder_name)
if not os.path.exists(new_path):
os.makedirs(new_path)
old_image_path = os.path.join(folder_path, image)
new_image_path = os.path.join(new_path, image)
shutil.move(old_image_path, new_image_path)
If they are all formatted similarly to the three fruit example you gave, you can simply do a string.split(".")[0] on each filename you encounter:
import os
for image in images:
fruit = image.split(".")[0]
if not os.path.isdir(fruit):
os.mkdir(fruit)
os.rename(os.path.join(fruit, image))
As an idea, hope it helps
import os
from pathlib import Path
import shutil
folder_path = "images/"
nameList=[]
for image in os.listdir(folder_paths):
folder_name = image.split('.')[0]
nameList.append(folder_name)
for f in os.listdir(folder_paths):
Path(folder_name).mkdir(parents=True, exist_ok=True,mode=0o755)
des = folder_name +"/"+str(f)
old_path = folder_paths+str(f)
for path in nameList:
if f.endswith('.jpg'):
print(f)
if path == folder_name:
shutil.move(old_path, str(des))
Related
the file location is: /Users/D/Desktop/Files/1/no.h5 and its the same filename (no.h5) in the folders 1-400. I want all these files to collect in the same folder with their number as their new names.
Your help is greatly appreciated!
You could use a glob pattern to find the target files to do the move. The pathlib library provides a convenient way to manage the paths.
from pathlib import Path
base = Path(" /Users/D/Desktop/Files")
target_folder = Path("/some/where/else")
for target in base.glob("*/no.h5"):
name = target.parent.name
if name.isdigit():
target.rename(target_folder/(name + ".h5"))
You can use the shutil module (https://docs.python.org/3/library/shutil.html):
import shutil
...
shutil.copyfile(src_fname, os.path.join(target_dir, new_fname))
You can also walk through your base directory to create a list of files that need to be copied:
import os
dir_base = fr'\Users\D\Desktop\Files'
fname_base = 'no'
fname_suffix = 'h5'
list_files = []
for subdir, dirs, files in os.walk(dir_base):
for file in files:
if file.startswith(fname_base) and file.endswith(fname_suffix):
list_files.append(os.path.join(subdir, file))
The whole code for an example I created on my system looks like this:
import shutil
import os
#dir_base = fr'\Users\D\Desktop\Files' #<============ base directory for your case
dir_base = fr'./test' #<============ base dir for my example
fname_base = 'no'
#fname_suffix = 'h5' #<============ extension of your files
fname_suffix = 'txt' #<============ extension of files in my example
target_base_fname='abc' #<============ base filename for new files in my example
target_dir = fr'./test/tgt' #<============ target dir for my example
list_files = []
for subdir, dirs, files in os.walk(dir_base):
for file in files:
if file.startswith(fname_base) and file.endswith(fname_suffix):
list_files.append(os.path.join(subdir, file))
for elem in list_files:
print(f'processing: {elem}')
this_dir_path = os.path.dirname(elem)
this_dir_name = os.path.basename(this_dir_path)
new_fname = target_base_fname + '_' + this_dir_name + '.' + fname_suffix
shutil.copyfile(elem, os.path.join(target_dir, new_fname))
path_school="/content/drive/MyDrive"
test_path=path_school+"//"+"test"
processedex_path=path_school+"//"+"test_ex"
for (path, dir, files) in os.walk(train_path):
for filename in files:
ext = os.path.splitext(filename)[-1]
test_folder_list = [f for f in os.listdir(path_school+'//'+'test')]
for f in os.listdir(test_path):
fol=os.path.splitext(f)[-1]
'''
os.makedirs(processedex_path+"//"+f)
'''
if ext == '.jpg':
img=Image.open ("%s/%s" % (path, filename)).convert('L')
img=img.resize((256,256))
img.save(processedex_path+"//"+f+"//"+"pre"+"_"+filename)
in 'test_path' there are a lot of folders like 'A356_4_50_TS_167' and in this folder there are images named '0232-0024.jpg'.
I want to save images in right place folder 'A356_4_50_TS_167' in 'processedex_path' folder.
This code saves every changed images in every folder.
Please help me to save images in right folders.
enter image description here
enter image description here
these are my original path and I want to save images in same named folder in 'test_ex'(=processedex_path) folder
enter image description here
but every images from every folders were saved in each folders not 2 images per one folder but 70 images per on folder I want to save 2images per one folder
thank u for answering
I can't run your code but I think you have too many for-loops
I would do
import os
from PIL import Image
path_school = "/content/drive/MyDrive"
# source & destination folder
test_path = os.path.join(path_school, "test")
processedex_path = os.path.join(path_school, "test_ex")
os.makedirs(processedex_path, exist_ok=True)
for subfolder in os.listdir(test_path):
# source & destination subfolder
src_subfolder = os.path.join(test_path, subfolder)
dst_subfolder = os.path.join(processedex_path, subfolder)
if os.path.isdir(src_subfolder): # check if it is really subfolder
os.makedirs(dst_subfolder, exist_ok=True)
for filename in os.listdir(src_subfolder):
if filename.lower().endswith( ('.jpg', '.png', 'webp') ):
# source & destination file
src_file = os.path.join(src_subfolder, filename)
dst_file = os.path.join(dst_subfolder, "pre_"+filename)
img = Image.open(src_file).convert('L')
img = img.resize((256, 256))
img.save(dst_file)
print('converted:', filename)
print('src:', src_file)
print('dst:', dst_file)
I have a folder of images containing
['PAT_01_01_crypts.png',
'PAT_01_01_orig.png',
'PAT_01_02_crypts.png',
'PAT_01_02_orig.png',
'PAT_01_03_crypts.png',
'PAT_01_03_orig.png']
I want to split these images into two folders.
Folder 1:
PAT_01_01_crypts.png
PAT_01_02_crypts.png
PAT_01_03_crypts.png
Folder 2:
PAT_01_01_orig.png
PAT_01_02_orig.png
PAT_01_03_orig.png
Can you please help me out. Im also trying to augment these images.
You can use in keyword to check whether the certain string in the statement.
A = ['PAT_01_01_crypts.png', 'PAT_01_01_orig.png', 'PAT_01_02_crypts.png',
'PAT_01_02_orig.png', 'PAT_01_03_crypts.png', 'PAT_01_03_orig.png']
folder1 = []
folder2 = []
for name in A:
if "orig" in name:
folder1.append(name)
else:
folder2.append(name)
print(folder1)
print(folder2)
or you can use:
folder1 = [name for name in A if "orig" in name]
folder2 = [name for name in A if "crypt" in name]
Both Output:
['PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png']
['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png']
you can use the shutillibrary combined with glob library
import shutil
import os
import glob
path = os.path.join('src','data','images','*')
#use your own path
list_files = glob.glob(path)
for file in list_files:
if file.endswith('orig.png'):
shutil.move(file,os.path.join('src','data','target','')
else:
shutil.move(file,os.path.join('src','data','target2','')
glob.glob(path) will return a list containing all the files / folder that are in path. Note that to do so you need the * at the end of your path
shutil.move()
go to https://www.geeksforgeeks.org/python-shutil-move-method/ for details
I have not tested it, so tell me if it works
Here's a novice approach to the problem. The approach is simple but accounts for any number of image types (orig, crypt etc..)
Code
import os
import shutil
image_type = ['orig', 'crypt', 'hd', 'sd']
files_list = ['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png', 'PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png', 'myfile_hd.png', 'yourfile_sd.png']
# Check if directory exists. If not, create it
for folder in image_type:
if os.path.isdir(f"./{folder}"):
pass
else:
os.mkdir(f"./{folder}")
# Move files
for fname in files_list: # traverse the files list
for img in image_type: # compare which type it matches to
if img in fname:
shutil.move(f"./{fname}", f"./{img}/{fname}") # move it to the respective directory
break
# Print contents of each folder
for folder in image_type:
print(f"\n\nContents of {folder}::::")
print(os.listdir(f"./{folder}"))
Input
PAT_01_01_crypts.png
PAT_01_02_crypts.png
PAT_01_03_crypts.png
PAT_01_01_orig.png
PAT_01_02_orig.png
PAT_01_03_orig.png
myfile_hd.png
yourfile_sd.png
Output
Contents of orig::::
['PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png']
Contents of crypt::::
['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png']
Contents of hd::::
['myfile_hd.png']
Contents of sd::::
['yourfile_sd.png']
I'm in an introductory neural networking class so mind my ignorance. Also my first SO post.
I'm trying to resize some very highly resolved images within a dataset into 80x80p grayscale images in a new dataset. However, when I do this, I'd like to keep the filenames of each new image the same as the original image. The only way I know how to resave images into a new file is through a str(count) which isn't what I want. The filenames are important in creating a .csv file for my dataset later.
The only SO post I can find that is related is this:
Use original file name to save image
But the code suggested there didn't work - wasn't sure if I was going about it the wrong way.
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
img_resized.save(path2 + str(count) + '.png')
count +=1
pass
pass
Reuse the original filename that you get from the for loop i.e. file
and, split it into filename and extension using os.path.splitext() like below:
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = os.path.splitext(file)[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
Another option, we can use python str's built-in split method to split the original filename by . and discard the extension.
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = file.split(".")[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
So, if an image has a name such as some_image.jpeg then, the img_filename will have a value some_image as we splitted by . and discarded .jpeg part of it.
NOTE: This option assumes the original_filename will not contain any . other than the extension.
I assume that image name is on path1. If so you can grap image name from there in this way:
x=path1.rsplit('/',1)[1]
We are splitting path1 on last slash and taking image name string via indexing.
I'm trying to organize some data before processing it.
What I have is a folder of raw tiff files (they're raster bands from a drone sensor).
I want to move these files into new, individual folders. e.g., IMG_001_1, IMG_001_2, IMG_001_3, IMG_001_4 and IMG_001_5 are all moved into a new folder titled IMG_001. I am ok with changing the naming structure of the files in order to make the code simpler.
An additional issue is that there are a few images missing from the folder. The current files are IMG0016 - IMG0054 (no IMG0055), IMG0056 - IMG0086 (no IMG0087), and IMG0087 - IMG0161. This is why I think it would be simpler to just rename the new image folders from 1-143.
My main problem is actually moving the files into the new folders - creating the folders is fairly simple.
Played around a little and this script came out, which should do what you want:
import os
import shutil
import re
UNORG = "C:\\Users\joshuarb\Desktop\Unorganized_Images\\"
ORG = "C:\\Users\joshuarb\Desktop\Organized_Images\\"
def main():
file_names = [os.path.join(UNORG, i) for i in get_files_of(UNORG)]
for count in range(0, 143):
current_dir = "{}IMG_{:04d}".format(ORG, count)
os.makedirs(current_dir)
move_files = get_files_to_move(file_names, count)
print move_files
for i in move_files:
shutil.move(i, os.path.join(current_dir, os.path.basename(i)))
def get_files_to_move(file_names, count):
return [i for i in file_names if re.match('.*IMG{}_.*'.format(count), i)]
def get_files_of(mypath):
(dirpath, dirnames, filenames) = os.walk(mypath).next()
return filenames
if __name__ == '__main__':
main()
As you see, the code is not commented. But feel free to ask if something is unclear;)
Problem solved!
import os
import shutil
srcpath = "C:\Users\joshuarb\Desktop\Python_Test\UnorganizedImages"
srcfiles = os.listdir(srcpath)
destpath = "C:\Users\joshuarb\Desktop\Python_Test\OrganizedImages"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:8] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for dirname, full_path in targets:
for filename in srcfiles:
if dirname == filename[0:8]:
move(filename, full_path)