how to import images by using Pil library in python? - python

Hope you all are good. I'm facing this problem while importing images from a directory inside my project directory. I don't what's the problem. I did this while putting the images in the main directory. It's worked perfectly fine but I want to now import images from my folder images . Here is the screenshot. I hope you all can understand what i'm trying to do The only problem is in that "if items.endswith("images.jpg").

Try this :
for items in os.listdir('images'):
if items.endswith('.jpg'):
image = Image.open(f'images/{items}')
Explanation:
os.listdir returns the files inside the directory which doesn't include the full path. ie. images/images.jpg.
So when using PIL you should add the master path at the beginning.

I can see there are two main problems, the first one is that you are checking the file extension with endswith('images/.jpg'), this should be endswith('.jpg'), the second one is that you are using os.listdir, which returns a list with the file names in the folder and not the full path, that means that you are trying to load the image from im_name.jpg and not images/im_name.jpg.
The solution to both of these problems would be to use the glob package, which lets you search for all files matching an extension, and returns the full path for each file. For example:
import glob
print(glob.glob('images/*.jpg'))
should print:
images/image1.jpg
images/image2.jpg
...
And these are enough to load them using PIL.

Related

Been trying to copy folders and files using shutil.tree() function in python but brings FileExistsError

I have tried to use shutil module in python. The shutil.copytree fails to copy folders and brings FileExistsError. I have tried it using different files but it still fails.Please check
import shutil
from pathlib import Path
p = Path(r"C:\Users\rapid\OneDrive\Desktop")
shutil.copytree(p/"folder_1", p/"folder_2")
Please check for the error image
I tried the code myself and works fine the first time.
After the destination folder (in your case folder_2) already created shutil can't create it again and it fails. So you can either delete the destination folder manually or using shutil.rmtree.
Good Luck!
All credit to Timus. He provided the solution on the comment section. Just used
shutil.copytree(p/"folder_1", p/"folder_2", dirs_exist_ok=True)
It works fine. Thanks Timux

Why using Image.save() (from Pillow in Python) with variables is not working?

On a Python course, I have to write a script to transform a bunch of images (with wrong format(.tiff) and size) to '.jpeg' and save them with the same name to another folder!
The problem is it won't save it to the directory I want (even on the same directory) using the path + file variable + the image format. I used the os.path.join() method too but it did not work either. I managed to do it a few times but not in the directory I want. Can you give me some advice? Thank you!
#!/usr/bin/env python3
from PIL import Image
import os
files = os.listdir('/home/dani/images')
if not os.path.exists('/home/dani/images/opt/icons'):
os.makedirs('/home/dani/images/opt/icons')
for file in files[1:]:
if not os.path.isdir('/home/dani/images/'+ file):
im = Image.open('/home/dani/images/'+file)
im.convert('RGB').rotate(-90).resize((128,\
128)).save('/home/dani/images/opt/icons/'+file +'.jpeg')
Sometimes saving has problems with permission. Try saving the file in the same folder you have your .py file.
If saving in the same folder works, then the problem is a permission. Try os.umask()

Python os.getcwd() is not working on subfolders in VSCODE

I have a python file, converted from a Jupiter Notebook, and there is a subfolder called 'datasets' inside this file folder. When I'm trying to open a file that is inside that 'datasets' folder, with this code:
import pandas as pd
# Load the CSV data into DataFrames
super_bowls = pd.read_csv('/datasets/super_bowls.csv')
It says that there is no such file or folder. Then I add this line
os.getcwd()
And the output is the top-level folder of the project, and not the subfolder when is this python file. And I think maybe that's the reason why it's not working.
So, how can I open that csv file with relative paths? I don't want to use absolute path because this code is going to be used in another computers.
Why os.getcwd() is not getting the actual folder path?
My observation, the dot (.) notation to move to the parent directory sometimes does not work depending on the operating system. What I generally do to make it os agnostic is this:
import pandas as pd
import os
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
super_bowls = pd.read_csv(__location__ + '/datasets/super_bowls.csv')
This works on my windows and ubantu machine equally well.
I am not sure if there are other and better ways to achieve this. Would like to hear back if there are.
(edited)
Per your comment below, the current working directory is
/Users/ivanparra/AprendizajePython/
while the file is in
/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv
For that reason, going to the datasets subfolder of the current working directory (CWD) takes you to /Users/ivanparra/AprendizajePython/datasets which either doesn't exist or doesn't contain the file you're looking for.
You can do one of two things:
(1) Use an absolute path to the file, as in
super_bowls = pd.read_csv("/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv")
(2) use the right relative path, as in
super_bowls = pd.read_csv("./Jupyter/datasets/super_bowls.csv")
There's also (3) - use os.path.join to contact the CWD to the relative path - it's basically the same as (2).
(you can also use
The answer really lies in the response by user2357112:
os.getcwd() is working fine. The problem is in your expectations. The current working directory is the directory where Python is running, not the directory of any particular source file. – user2357112 supports Monica May 22 at 6:03
The solution is:
data_dir = os.path.dirname(__file__)
Try this code
super_bowls = pd.read_csv( os.getcwd() + '/datasets/super_bowls.csv')
I noticed this problem a few years ago. I think it's a matter of design style. The problem is that: your workspace folder is just a folder, not a project folder. Most of the time, your relative reference is based on the current file.
VSCode actually supports the dynamic setting of cwd, but that's not the default. If your work folder is not a rigorous and professional project, I recommend you adding the following settings to launch.json. This is the simplest answer you need.
"cwd": "${fileDirname}"
Thanks to everyone that tried to help me. Thanks to the Roy2012 response, I got a code that works for me.
import pandas as pd
import os
currentPath = os.path.dirname(__file__)
# Load the CSV data into DataFrames
super_bowls = pd.read_csv(currentPath + '/datasets/super_bowls.csv')
The os.path.dirname gives me the path of the current file, and let me work with relative paths.
'/Users/ivanparra/AprendizajePython/Jupyter'
and with that it works like a charm!!
P.S.: As a side note, the behavior of os.getcwd() is quite different in a Jupyter Notebook than a python file. Inside the notebook, that function gives the current file path, but in a python file, gives the top folder path.

Hidden dot underscore ._ before name of image files in a directory while reading them using glob

I am trying to read images from a directory.
The directory has images named like-
person1_bacteria_1.jpeg
person1_bacteria_2.jpeg
person2_bacteria_3.jpeg
I am trying to grab these images using glob function.
images = images_directory.glob('*.jpeg')
The problem is, while I print the directory of the files I have just read, I found that some of the files have dot underscore (._) before them.
For example-
dir/._person1_bacteria_2.jpeg
I checked the image files in the directory again and again but there was no image name with ._ before it.
How do I avoid reading files whose name have dot underscore (._) before it?
I am running this in an windows machine.
I think this files are just thumbnail files. if you look with os.path.getsize(filename) you can probably confirm this hypothesis.
The reason you don't see this files is that they are probably marked as hidden files.
Try to configure your windows explorer such, that it shows all files. ( https://support.microsoft.com/en-us/help/14201/windows-show-hidden-files )
If glob.glob produces results with unexpected filenames, you can filter them with the following code using a comprehension list with a condition filter
import os
import glob
images = glob.glob(os.path.join(os.getcwd(), '*.jpeg'))
images = [image for image in images if not os.path.basename(image).startswith('._')]
If the directory contains files starting with dot glob won’t be matched by default. globe returns a list, you can write this code to add both normal files and hidden ones:
import glob
images = glob.glob('*.jpeg') + glob.glob('.*.jpeg')
UPDATE: If you want to exclude files that starts with ._ you can use this pattern:
images = glob.glob("[!._]*.jpeg")

Moving file based on first few words in name

I want to move a file that I downloaded.
I have tried this.
shutil.move('/Users/dgoodwin/Downloads/metrics-lifetime-20190513.csv', '/Users/dgoodwin/OneDrive/metrics-lifetime.csv')
This works as long as the name doesn't change. I found after another download that there is the date at the end which changes everyday.
Here is the image of the files I am working with. Files
I would like to just use the first part of the name "metrics-lifetime" to search for in my downloads but I can't find out how grab partial paths.
Any help would be great. Thank you!
You can use use the glob module
import glob
files = glob.glob('*.xlsx')
for file in files:
shutil.move(file, destination_path)
I will proceed to use folder hierarchy loading.
This is a Japanese homepage, but it is written here.

Categories