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')
Related
I'm trying to rename a set of files using the following code. The File is within the folder but it gives the following error. My code is attached here with:
import os
path='absolute_path'
arr = os.listdir(path)
for i in arr:
old_name=i
old_name_part=old_name.split(".")
new_name=old_name_part[0]+".png"
print(i,'\t',old_name,'\t',new_name)
os.rename(i,new_name)
Error :
drone_0002_01320.jpg drone_0002_01320.jpg drone_0002_01320.png
Traceback (most recent call last):
File "/absolute_path/rename.py", line 23, in <module>
os.rename(i,new_name)
FileNotFoundError: [Errno 2] No such file or directory: 'drone_0002_01320.jpg' -> 'drone_0002_01320.png'
os.rename(i,new_name) takes the full path of your file
you can change it into this:
os.rename(os.path.join(path, i),os.path.join(path, new_name))
Consider read about pathlib module, it has some good function for those kind of problems
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.
So I'm using Wand to try to convert the pdf to an image
from wand.image import Image
with Image(filename="test.pdf") as img:
img.save(filename="/temp.jpg")
with Image(filename="test.jpg") as img:
img.resize(200, 150)
img.save(filename="t.jpg")
but for some reason i get:
Traceback (most recent call last):
File "C:\Users\Rafael\Desktop\k\pdf to image.py", line 3, in <module>
with Image(filename="test.pdf") as img:
File "C:\Python27\lib\site-packages\wand\image.py", line 2534, in __init__
self.read(filename=filename, resolution=resolution)
File "C:\Python27\lib\site-packages\wand\image.py", line 2601, in read
self.raise_exception()
File "C:\Python27\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
DelegateError: PDFDelegateFailed `The system cannot find the file specified.
' # error/pdf.c/ReadPDFImage/798
Can i do something or is there another way to convert a pdf to an image?
Installing Ghostscript and adding
C:\Program Files (x86)\gs\gs9.06\bin
to your systempath should help to resolve this issue as it helped me to overcome this error....
The reason may be a missing ghostscript installation. This is a similar question on SO
There is another way. Looks like you are using windows so you need download "https://github.com/oschwartz10612/poppler-windows/releases/"
Because you are using Windows,so you need to state the path to poppler/bin folder:
code:
from pdf2image import convert_from_path
poppler_path = "C:\path\to\poppler\bin"
images = convert_from_path('example.pdf')
for i in range(len(images)):
images[i].save('page'+ str(i) +'.jpg', 'JPEG')
reference : https://www.geeksforgeeks.org/convert-pdf-to-image-using-python/
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
I'm looking for a way to open and crop several tiff images and then save the new croped images created in the same folder (related to my script folder).
My current code looks like this:
from PIL import Image
import os,platform
filespath = os.path.join(os.environ['USERPROFILE'],"Desktop\Python\originalImagesfolder")
for file in os.listdir(filespath):
if file.endswith(".tif"):
im = Image.open(file)
im.crop((3000, 6600, 3700, 6750)).save(file+"_crop.tif")
This script is returning me the error:
Traceback (most recent call last):
File "C:\Users...\Desktop\Python\script.py", line 22, in
im = Image.open(file)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2219, in open
fp = builtins.open(fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Image1Name.tif'
'Image1Name.tif' is the first tif image I'm trying to process in the folder. I don't get how the script can give the file's name without being able to find it. Any Help?
PS: I have 2 days experience in python and codes generaly speaking. Sorry if the answer is obvious
[EDIT/Update]
After modifying my initial code thanks to vttran and ChrisGuest answers, turning then into this:
from PIL import Image
import os,platform
filespath = os.path.join(os.environ['USERPROFILE'],"Desktop\Python\originalImagesfolder")
for file in os.listdir(filespath):
if file.endswith(".tif"):
filepath = os.path.join(filespath, file)
im = Image.open(filepath)
im.crop((3000, 6600, 3700, 6750)).save("crop"+file)
the script is returning me a new error message:
Traceback (most recent call last):
File "C:/Users/.../Desktop/Python/script.py", line 11, in
im.crop((3000, 6600, 3700, 6750)).save("crop"+file)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 986, in crop
self.load()
File "C:\Python34\lib\site-packages\PIL\ImageFile.py", line 166, in load
self.load_prepare()
File "C:\Python34\lib\site-packages\PIL\ImageFile.py", line 250, in
load_prepare
self.im = Image.core.new(self.mode, self.size) ValueError: unrecognized mode
A maybe-useful information, it's a Landsat8 image in GeoTiff format. The TIFF file therefore include geoposition, projection... informations. The script works perfectly fine if I first open and re-save them with a software like Photoshop (16int tiff format).
When you are search for the file names you use filespath to specify the directory.
But then when you open the file, you are only using the base filename.
So you could replace
im = Image.open(file)
with
filepath = os.path.join(filespath, file)
im = Image.open(filepath)
Also consider using the glob module, as you can do glob.glob(r'path\*.tif) .
It is also good practice to avoid using builtin functions like file as variable names.