I have folders (river reach, eg W&S_River) which contain sub folders (one per gravel bar eg GravelBar_18) which contain images (50 - 300 per gravel bar). I'm trying to convert the images from jpg to tiff. I've got some code that does the conversion, but it takes some time and doesn't loop through the directory folder (river reach). I'm hoping to define the reach folder and have some code that opens each sub folder and converts each sub folder.
I've been trying to use os.walk based on what I've read here. I'm not getting any error messages, but it's not actually doing anything. Below is what I'm currently using to update the image in each sub folder.
import os
import os.path
from PIL import Image
import glob
os.chdir('E:/W&S_River/GravelBar_18')
for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.save(file+".tiff", 'TIFF')
print("done")
for infile in glob.glob("/*/*.jpg"): # "/*" is important
...
#https://stackoverflow.com/a/36426997/11343720
#https://docs.python.org/3/library/glob.html#glob.glob
# ../../Tools/*/*.gif
import os
import os.path
from PIL import Image
import glob
def jpgToTIFF(folder):
os.chdir(folder)
for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.save(file+".tiff", 'TIFF')
subfolders = [f.path for f in os.scandir('f:/work_rpi') if f.is_dir() ]
for foler in subfolders:
print(foler)
jpgToTIFF(folder)
https://stackoverflow.com/a/40347279/11343720
Related
I'm working on a python script that checks the .pdf files in a directory, creates a new directory for each file, converts the .pdf into images, and writes the images as jpg into the new directory. I'm using pdf2image and have the following code:
import os
#import main
import glob
#import cv2
import matplotlib.pyplot as plt
from pdf2image import convert_from_path
from PIL import Image
path = "C:/Users/d/Desktop/Reis/"
for file in glob.iglob(path + "*.pdf"):
print(file)
name = os.path.basename(file)
filename = name.split(".")[0]
print(filename)
images = os.mkdir(path + filename)
pages = convert_from_path("C:/Users/d/Desktop/Reis/Reis_Wasser_Verhaeltnis.pdf",
350,
poppler_path=r'C:/Program Files/poppler-22.04.0/Library/bin',
output_folder=images)
for i in range(len(pages)):
pages[i].save('page' + str(i) + '.jpg', 'JPEG')
When I run my code I don't get an error message but no images either. Does anyone have an idea what I'm overseeing?
os.mkdir creates the Folder but it is of type boolean. Thus:
images = os.mkdir(path + filename)
returns only True and cannot be used as the output folder. My script writes the images into the default project directory.
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))
I am trying to save images after converting them into grayscale from one folder to another. when I run my code, it keeps saving the file in the same folder and making duplicates of all the images. Here is my code, Please guide where my problem lies...
import glob
import cv2
import os
spath=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png"
dpath=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png"
files = os.listdir(spath)
for filename in glob.glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png'):
print(filename)
img=cv2.imread(filename)
rl=cv2.resize(img, (40,50))
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(dpath,filename), gray_image)
If you pass a full pathname to glob.glob(), then it will return the full path of the result files, not just the filenames.
That means in this loop in your code:
for filename in glob.glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png'):
filename is a full path such as C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\myfile1.png.
Then, later in the loop when you call cv2.imwrite(os.path.join(dpath,filename), gray_image), you're trying to join together C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png and C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\myfile1.png, which is the cause of your error.
glob() is convenient to get the full path of just the files you want, but then you have to separate the filename from the directory.
Try using listdir() instead of glob():
import glob
import cv2
import os
sdir=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza"
ddir=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images"
for filename in os.listdir(sdir):
if not filename.lower().endswith(".png"):
continue
print(filename)
img=cv2.imread(os.path.join(sdir, filename))
rl=cv2.resize(img, (40,50))
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(ddir,filename), gray_image)
I want to implement a python code which would select a 1 image after 3 images and so on till the last image in an sequential manner in the specified folder and copy those images to another folder.
Example : As shown in the screenshot
link : https://i.stack.imgur.com/DPdOd.png
The solution is same but I think it is more clear for all
import os
import shutil
path_to_your_files = 'your pics path'
copy_to_path = 'destination for your copy'
files_list = sorted(os.listdir(path_to_your_files))
orders = range(1, len(files_list) , 4)
for order in orders:
files = files_list[order] # getting 1 image after 3 images
shutil.copyfile(os.path.join(path_to_your_files, files), os.path.join(copy_to_path, files)) # copying images to destination folder
You can:
import os
files = os.listdir('YOUR PICS DIRECTORY HERE')
every_4th_files=[f for idx,f in zip(range(len(files)), files) if not idx%4]
Is it what you need?
Edit
To copy images I recommend to use shutil.copyfile.
If you encounter a problem - inform about it.
import os
from shutil import copyfile
files = sorted(os.listdir('Source Folder'))
4thFile = [fileName for index, file in zip(range(len(files)),files) if not index%4]
for file in 4thFile:
copyfile(os.path.join(src_path, f), os.path.join(dest_path, file))
That should get the job done.
I want to be able to open/move/rename the images form a list of filenames from a directory with hundreds of images. Unfortunately there are no wildcards, they are all .jpg and the images names are not sequential.
e.g
list = ['media\\1520298987567.jpg',
'media\\1520298997109.jpg',
'media\\1520299004063.jpg',
'media\\1520299010082.jpg',
'media\\1520299015452.jpg',
'media\\1520299020690.jpg',
'media\\1520299026092.jpg']
Does anyone know how to do this?
Thank you in advance.
This may help you, (open/ move/ rename) the image files.
from PIL import Image
import glob, os
path = '/path/to/move/'
list_of_files = ['testfile1.jpg', 'testfile2.jpg']
for infile in glob.glob("*.jpg"):
filename, ext = os.path.splitext(infile)
if filename + '.' + ext in list_of_files:
# Open the image
im = Image.open(image_path)
# Rename the Image
renamed_filename = filename + '_renamed.' + ext
# Move the image
im.save(path + renamed_filename, 'jpg')
You can use glob package in python to get list of jpg files and os package to perform open/move/rename operations
import glob
list_of_files = glob.glob("media/*.jpg"):
for file in list_of_files:
# do required operation with the help of os package