Python importing a batch of jpg images in files? - python-3.x

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))

Related

Converting multiple files in a directory into .txt format. But file names become Binary

So I am creating plagiarism software, for that, I need to convert .pdf, .docx,[enter image description here][1] etc files into a .txt format. I successfully found a way to convert all the files in one directory to another. BUT the problem is, this method is changing the file names
into binary values. I need to get the original file name which I am gonna need in the next phase.
**Code:**
import os
import uuid
import textract
source_directory = os.path.join(os.getcwd(), "C:/Users/syedm/Desktop/Study/FOUNDplag/Plagiarism-checker-Python/mainfolder")
for filename in os.listdir(source_directory):
file, extension = os.path.splitext(filename)
unique_filename = str(uuid.uuid4()) + extension
os.rename(os.path.join(source_directory, filename), os.path.join(source_directory, unique_filename))
training_directory = os.path.join(os.getcwd(), "C:/Users/syedm/Desktop/Study/FOUNDplag/Plagiarism-checker-Python/trainingdata")
for process_file in os.listdir(source_directory):
file, extension = os.path.splitext(process_file)
# We create a new text file name by concatenating the .txt extension to file UUID
dest_file_path = file + '.txt'
# extract text from the file
content = textract.process(os.path.join(source_directory, process_file))
# We create and open the new and we prepare to write the Binary Data which is represented by the wb - Write Binary
write_text_file = open(os.path.join(training_directory, dest_file_path), "wb")
# write the content and close the newly created file
write_text_file.write(content)
write_text_file.close()
remove this line where you rename the files:
os.rename(os.path.join(source_directory, filename), os.path.join(source_directory, unique_filename))
that's also not binary, but a uuid instead.
Cheers

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')

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.

Python3 clip.duration of all mp4 files in a folder

I am new to Python (using Python 3.6) and would like to extract the duration (in seconds) of all mp4 files I have in one folder. Code I have is:
path2 = path_directory
from moviepy.video.io.VideoFileClip import VideoFileClip
for root, dirs, files in os.walk(path2):
for filename in files:
clip = VideoFileClip(files)
print(clip.duration)
If I define clip = VideoFileClip("name_of_one_specific_file.mp4") it correctly prints the length (i.e. 590seconds) of that specific file, so I guess the mistake is in how I walk through all the files. I would need a list of the duration for each of the 245 mp4 files I have in path2.
Thank you very much in advance.
You just have a small error:
path2 = path_directory
from moviepy.video.io.VideoFileClip import VideoFileClip
for root, dirs, files in os.walk(path2):
for filename in files:
clip = VideoFileClip(filename) # <= !!!
print(clip.duration)
You want to open filename, not files. filename is the name of one specific file, files is the list of all files in a directory.
There's a newer library, available from Python 3.4, that's generally easier to use than walk. It's pathlib.
This is how you can use it in your situation.
from pathlib import Path
from moviepy.video.io.VideoFileClip import VideoFileClip
path2 = r' -------- '
for filename in Path(path2).glob('*.mp4'):
clip = VideoFileClip(filename.as_posix())
print(clip.duration)

Python 3 How to delete images in a folder

How do I delete all png format pics in a folder using Python 3?
This single line statement will take each file in a specified path and remove it if the filename ends in .png:
import os
os.remove(file) for file in os.listdir('path/to/directory') if file.endswith('.png')
import glob
removing files = glob.glob('file path/*.jpg')
for i in removing files:
os.remove(i)
replace file path with the directory to the image folder
this function will help you to delete a single image file all you need to do is put it in for loop to delete the multiple images or file..just double check that you are providing valid path to your file.
'
def remove_img(self, path, img_name):
os.remove(path + '/' + img_name)
# check if file exists or not
if os.path.exists(path + '/' + img_name) is false:
# file did not exists
return True
'

Resources