Running image conversion script gives me "no such file" - python

I have this image conversion script written in Python. I have some images in a folder that go like turbo1, turbo2, and so on...
I need to convert them from png to jpeg.
This is my code:
from PIL import Image
import os
directory = r'C:\Users\Filip\Desktop\turbos'
c=1
for filename in os.listdir(directory):
if filename.endswith(".PNG"):
im = Image.open(filename)
name='turbo'+str(c)+'.jpeg'
rgb_im = im.convert('RGB')
rgb_im.save(name)
c+=1
print(os.path.join(directory, filename))
continue
else:
continue
I get this error:
Traceback (most recent call last):
File "c:\Users\Filip\Desktop\convert.py", line 9, in <module>
im = Image.open(filename)
File "C:\Python\lib\site-packages\PIL\Image.py", line 2953, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'turbo1.PNG'
If I change filename.endswith(".PNG"): to filename.endswith(".png"):
it does not give me that error but the images don't convert.
What am I missing here?

.png and .PNG. are two different file extensions and your code should be
im = Image.open(os.path.join(directory, filename))

Related

Unable to open or read log files: FileNotFoundError [duplicate]

I'm trying to run the following script which simply reads and image and saves it again:
from PIL import Image
import os
rootdir = '/home/user/Desktop/sample'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
im = Image.open(file)
im.save(file)
I however get the following error:
Traceback (most recent call last):
File "test.py", line 10, in <module>
im = Image.open(file)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '1.jpg'
So, what I'm trying to do is simply read the file 1.jpg and save it again, provided that 1.jpg is located in the directory.
How can I fix this issue?
Thanks.
You're going to need to provide a fully qualified path, because file holds only the tail, not the entire path.
You can use os.path.join to join the root to the tail:
for root, dirs, files in os.walk(rootdir):
for file in files:
path = os.path.join(root, file)
im = Image.open(path)
im.save(path)

PIL "OSError: cannot identify image file " while opening an Image

I am trying to load a dataset of images using keras.load_img which returns a PIL Image instance, but am unable to do so due to some reason. The image was created by the following lines of code, after being generated by the imagaug module of python, which returns a numpy array:
img = augmenter(image=image)
img = Image.fromarray(img)
img.save(os.path.join(os.path.dirname(__file__), 'Roll 1_Augmented', 'PaperPrintImgAug' + str(i+1) + '_' + str(j) + '.jpg'))
The iamges seem to be saved successfully and I am able to open them with any image viewer. So I do not think they are corrupted. But I get the following error whenever I try to open the images using load_img:
Traceback (most recent call last):
File "ae.py", line 61, in readImages
img = load_img(folder_path[i], target_size=inputShape)
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7350df9e08>
I tried various solutions provided online and on stackoverflow, but the situations don't seem to match with mine. Upon downloading the latest version of Pillow, I got the following change in the traceback:
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/PIL/Image.py", line 2006, in open
raise IOError("cannot identify image file")
OSError: cannot identify image file
I suspect it might be some issue with the image encoding.
Can someone help me with this please?
EDIT:
The issue was solved by saving the images as png files.
For the issue with JPEG files, the following is the code I used:
from tensorflow.python.keras.preprocessing.image import load_img
def readImages(folder_path):
images = []
for i in range(len(folder_path)):
img = load_img(folder_path[i], target_size=inputShape)
img = np.array(img)
images.append(img)
images = np.array(images)
return images
augdata = glob('/home/ies/billa/Roll 1_Augmented/*')
augmentedImages = readImages(augdata)

"OSError: cannot identify image file" opening image with PIL/Image

I am trying to get some code working that has broken but was working before. I have a PNG file on my desktop and I simply want to open it using the Image module from PIL.
from PIL import Image
img_dir = r'C:\Users\DylanDB\Desktop\square.png'
img = Image.open(img_dir)
This is a remake of my more advanced code that it happens in as well. The error is:
Traceback (most recent call last):
File "C:/Users/DylanDB/Desktop/img_test.py", line 5, in <module>
img = Image.open(img_dir)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2317, in open
% (filename if filename else fp))
OSError: cannot identify image file 'C:\\Users\\DylanDB\\Desktop\\square.png'
I had the same error and it was due to the file was recently created and not closed properly before opening with the Image.open(). After closing the file f.close() it werked as expect
I found that the file was a corrupted image.

Python IOError: [Errno 2] No such file or directory:

I am just trying to open an image, I know there are several IOError questions on here, but I have not been able to understand the explanations.
The code i typed is here
from PIL import Image
image = Image.open("Lenna.png")
image.load()
The error obtained is as follows:
Traceback (most recent call last):
File "C:\Python27\hello world.py", line 4, in <module>
image = Image.open("Lenna.png")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'Lenna.png'
What do I do here?
Thank you.
You are trying to use relative filename. Python can't find such file in current work dir. Try to use full absolute path such as 'C:\Lenna.png'.
IOError: [Errno 2] No such file or directory: 'Lenna.png'
check path of the file
If you are trying to open image, either you have to specify the complete path of the image like,
from PIL import Image
im = Image.open('C:/Users/Anish/Desktop/2017PROJECTS/PYTHON_TWITTER/image/camel.png')
im.show()
Or you have to copy the image to the specified project directory and call the image name like,
im = Image.open('camel.png')

fp = builtins.open(filename, "rb") - Error

When I try running this script:
from PIL import Image
import os
files = os.listdir('mri')
for file in files:
img = Image.open(file)
I get the following error:
Traceback (most recent call last):
File "resize_image.py", line 6, in <module>
img = Image.open(file)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '6.jpg'
I made sure that 6.jpg is available. And, it seems that I get such error for any image in this location.
How can I fix the issue?
Thanks.
The file names from os.listdir are relative to the directory given. They must be made complete by joining the dirname to their basename.
files = os.listdir('my_folder')
for file in files:
img = Image.open(os.path.join('my_folder', file))
You are getting this error because you haven't add your image in project file folder.
Paste your image in project file and then run the program.
img = Image.open(os.path.join('mri', file))
this worked for me making sure you join the dir with the path
img = Image.open(os.path.join(r'C:\Users\Simin\Desktop\python proj\img.jpg'))
this direct path worked for me

Categories

Resources