Retrieve Images from different folders - python-3.x

I have a big folder and inside it has sub folders and i want to retrieve images from all those folders. How do i do it?
Second if i want to get dimensions of all those images how would i do that?
I have tried it with one folder and it is working fine. But i want to do it for multiple folders. I reading image dimensions of one folder from this code.
folder_images = "D:/DeepFashion/Category and Attribute Prediction/img/img"
size_images = dict()
for dirpath, _, filenames in os.walk(folder_images):
for path_image in filenames:
image = os.path.abspath(os.path.join(dirpath, path_image))
with Image.open(image) as img:
width, heigth = img.size
size_images[path_image] = {'width': width, 'heigth': heigth}

This code seems to be fine - the only problem with it being that if any of the files in the folder structure is not a proper image file, you will get an unhandled exception and the script will stop.
Since you are not reading the images in a lazy way, the is no reason to use the with command to get to the images - just change your code to:
folder_images = "D:/DeepFashion/Category and Attribute Prediction/img/img"
size_images = dict()
for dirpath, _, filenames in os.walk(folder_images):
for path_image in filenames:
image = os.path.abspath(os.path.join(dirpath, path_image))
try:
img = Image.open(image)
except OSError:
print("Not an image file: ", image)
width, heigth = img.size
size_images[path_image] = {'width': width, 'heigth': heigth}

Related

Upload every photo present in specific folder, without duplicates

I need help. After obtaining a list of photo names (.jpg) in a specific folder (chosen randomly from various folders), is there a way to upload every photo in that folder to a site, until they are finished? I would also like to avoid uploading duplicates.
So far, I have only managed to get a list of photos in a folder:
current_dir = os.getcwd()
folder = random.randint(1, 27)
img_path = current_dir + '\\' + 'Photo' + '\\' + str(folder)
img_dir = os.listdir(img_path)
img_list = []
for N in img_dir:
img_list.append(N)
At this point, I wanted to upload every single photo in the randomly selected folder using the send_keys() command, for example: driver.find_element(By.XPATH, "//input[#type='file']").send_keys(). But how can I do that? Thank you in advance for your help.

Automate cropping with Pillow and python?

So I have a folder with 500+ images that need to be cropped. And I have searched and have manage to create this cut-and-paste script. But, for some reason it doesn't save the new image!? The terminal is just still, no errors no nothing.
from PIL import Image # import the Python Image processing Library
import os # To read the folder
directory_in_str = "/Users/hora/Downloads/Etik"
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".png"):
image = os.path.join(directory_in_str, filename)
imageObject = Image.open(image) # Create an Image object from an Image
cropped = imageObject.crop((1025,85,2340,2040)) # Crop the iceberg portion (top left x, top left y, bottom right x, bottom right y)
cropped.save("{}".format(filename+"_cropped"), 'png') # Save the cropped portion
continue
else:
continue
Im searching in a specific folder, and the cropped image should be saved with a filename_cropped.png. But not necessary, I have backups if something should go side-ways.
The expected result:
Loop through a folder
Crop all images ending with .png
And save the crop image with the previous filename but with extension
FILNAME_cropped.png
Done
Two issues regarding this line:
cropped.save("{}".format(filename+"_cropped"), 'png')
Your filename still contains the file extension.
You don't add a (new) file extension yourself.
Both issues result in some string xxx.png_cropped for your new file.
My suggestion to modify your code:
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".png"):
image = os.path.join(directory_in_str, filename)
filename, file_extension = os.path.splitext(filename) # <-- After reading, filename can be overwritten
imageObject = Image.open(image)
cropped = imageObject.crop((1025,85,2340,2040))
cropped.save("{}".format(filename+"_cropped.png"), 'png') # <-- Explicitly add .png to your filename
continue
else:
continue
Hope that helps!
add the directory path in saving the file.
directory_in_str = "/Users/hora/Downloads/Etik/"
cropped.save("{}".format(directory_in_str+filename+"_cropped"),'png')

os.listdir reads images randomly making bounding box training difficult

os.listdir(path) command reads images randomly from a folder. I have saved a csv file with bouding box information for the images in folder sequentially. I assumed os.listdir would read the images sequentially so that my csv file also can be read sequentiallu during the training.
I have tried sorted(os.listdir) but no use. I could not find any other functions or code to read the images sequentially from a folder. I named the images as frame1.jpg, frame2.jpg etc.
PATH = os.getcwd()
# Define data path
data_path = PATH + '/frames'
data_dir_list = sorted(os.listdir(data_path))
print(data_dir_list)
img_data_list=[]
for dataset in (data_dir_list):
img_list=sorted(os.listdir(data_path+'/'+ dataset))
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in sorted(img_list):
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
input_img1=input_img
#input_img_resize=cv2.resize(input_img,(512,512))
img_data_list.append(input_img1)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
As per Python docs, os.listdir() returns filenames in an arbitrary order. It just maps to an underlying operating system call that will return filenames in whatever order is presumably most efficient based on filesystem design.
It's just a standard list of strings, so sorted() would work in the way you're using it. Are the sequential numbers in your filenames correctly padded for this to work with the more than 10 images you're presumably using? What's the random order you're seeing from sorted(os.listdir(...))?

How to change where the output directory where the new images go

I have a small query I'm hoping someone can help me out within Python 3. I am resizing a dataset of 10000 images to all be 1000x1000 in dimension before I do any pytorch analysis with it. I just wanted to ask how I change my code to save the outgoing images to a new folder I have created ''train_resized'' instead of the same folder as the original files as it is doing now when I run. Thanks
# Testing dataset
from PIL import Image
import os, sys
path = (r'G:\My Drive\CATSVDOGS2.0\test1\\')
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((1000,1000), Image.ANTIALIAS)
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
resize()
In your line
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
you're setting the path when using the variable f, as f uses the path variable you defined. A quick way to set the path is to do something like:
imResize.save('G:\\My Drive\\Path\\To\\Folder\\' + item + ' resized.jpg', 'JPEG', quality=90)
of course specify the path to be whatever you want. Untested as I don't have Python installed on my work machine, but that is the general gist.

Python importing a batch of jpg images in files?

I'm trying to import a batch of images from a file to a new separate folder according to the images name for example; 1000_70.jpg --> folder 70 and 1200_71.jpg --> folder 71. However, when I ran the script it does nothing.
from PIL import Image
import glob
import os
folder='Desktop/n' # All jpegs are in this folder
imList=glob.glob(folder+'*.jpg') # Reading all images with .jpg
newfold = 'Desktop/n/l' # New folder path
for img in imList: # Loop
im = Image.open(img) # Opening image
fileName, fileExt = os.path.splitext(img) # Extract the filename and
# Extension from path
im.save(newfold+fileName+'*.jpg') #save the image to new folder from
#old folder
First of you want the filename of the image not the path, use split instead of splitext to remove the parent folder and than use splitext to remove the extension:
os.path.splitext("afdsasdf/fasdfa/fff.jpg")
=> ('afdsasdf/fasdfa/fff', '.jpg')
os.path.split("afdsasdf/fasdfa/fff.jpg")
=> ('afdsasdf/fasdfa', 'fff.jpg')
Second you remove the wildcare in *.jpg when saving the image. You need that wildcare with glob since you are selectioning multiple files.
Third you need to extract the second number in the filename (1000_70.jpg --> 70).
All toghether you should have something that look like this:
for img in imList:
im = Image.open(img)
filepath, filenameExt = os.path.split(img)
filename, fileExt = os.path.splitext(filenameExt)
folderNumber = filename.split("_")[1]
im.save("{}/{}/{}".format(newfold, folderNumber, filenameExt))

Resources