Error Message When Converting to Grayscale in Python Using PIL? - python

So, I've got PIL installed (at least I think properly) to convert color images to grayscale, and when I use this code
from PIL import Image
image_file = Image.open("James.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('Gray.png')
IDLE shows me this...
Traceback (most recent call last):
File "C:/Users/Kane/Desktop/huh.py", line 2, in <module>
image_file = Image.open("James.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: 'James.png'
What do I need to do to fix this? I have Python 2.7, does this make a difference?

Your problem could be that the image file "James.png" is not in the same directory as your script, in your example on your Desktop. Is that the case?
Cheers

Related

How to give proper paths of folders in D drive in python

This is my code. Basically i want to load all images in a folder inside my D drive in a loop.
from PIL import Image
def loadimages(path):
list=listdir(path)
loadedimages=[]
for image in list:
img=Image.open(path+image)
loadedimages.append(img)
return loadedimages
path= r"D:\ACADEMICS\8SEM\PatternClassification\CBT-1\TrainCharacters"
imgs=loadimages(path)
I am getting error like this
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
imgs=loadimages(path)
File "<pyshell#11>", line 5, in loadimages
img=Image.open(path+image)
File "C:\Users\anjana ouseph\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2809, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\ACADEMICS\\8SEM\\PatternClassification\\CBT-1\\TrainCharacters1'
If the images are inside the TrainCharacters folder, you are missing a "\"
currently its looking for
'D:\\ACADEMICS\\8SEM\\PatternClassification\\CBT-1\\TrainCharacters1'
It's likely you need
'D:\\ACADEMICS\\8SEM\\PatternClassification\\CBT-1\\TrainCharacters\\1'
It appears the problem is the line img=Image.open(path+image).
I would recommend using the built-in Python path manipulation method os.path.join:
from PIL import Image
import os
def loadimages(path):
list=listdir(path)
loadedimages=[]
for image in list:
img=Image.open(os.path.join(path, image))
loadedimages.append(img)
return loadedimages
path= r"D:\ACADEMICS\8SEM\PatternClassification\CBT-1\TrainCharacters"
imgs=loadimages(path)

resizing images in a folder using python PIL

#!/usr/bin/python
from PIL import Image
import os, sys
path = "C:/Users/nonono/Desktop/hypergan/data/trainingData/"
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((256,256), Image.ANTIALIAS)
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
resize()
i got this code from another answer here and fitted it to my own parameters, yet when i run it in the command line i get an error:
Traceback (most recent call last):
File "resize.py", line 16, in <module>
resize()
File "resize.py", line 11, in resize
im = Image.open(path+item)
File "C:\Users\nonono\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2822, in open
raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file 'C:/Users/nonono/Desktop/hypergan/data/trainingData/bx71zze7egy18wel231 resized.jpg'
the folder trainingData is filled with a large amount of images, that i want to resize to 256x256 regardless of aspect ratio, i will not need the images afterwards so if there is a way to replace the images instead of just copying, resizing, and renaming, that would be great (unless i am reading this incorrectly, i have about 2 weeks experience in python)
any help would be appreciated

IOError: codec configuration error when reading image file

I am trying to get a thumbnail of a gif image using Pillow 4.3.0, Python 2.7.13, Debian 9.1
from PIL import Image
im = Image.open("Pictures/image.gif")
im.thumbnail((240,160))
im.save("Thumbnails/thumbnail.gif")
But I cannot do it. This is the log:
Traceback (most recent call last):
File "testpy.py", line 4, in <module>
im.thumbnail((240,180))
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1843, in
thumbnail
im = self.resize(size, resample)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1541, in
resize
self.load()
File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 244, in
load
raise_ioerror(err_code)
File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 59, in
raise_ioerror
raise IOError(message + " when reading image file")
IOError: codec configuration error when reading image file
This issue doesn't occur with other formats (jpg, png, bmp).
Any help is very appreciated.
Edit:
This happens specifically on pictures made with raspistill camera with gif encoding mode selected. If don't select encoding mode at all, it works well. But if not selecting encoding, then all the images are treated as jpg, which is not what I want.
The raspistill camera gif images contain a bit depth of 11,
>>> im.tile
[('gif', (0, 0, 100, 100), 792, (11, False))]
while Pillow supports gif images only up to a bit depth of 8.
I posted this question on Pillow github page and they are supposed to fix it soon.
https://github.com/python-pillow/Pillow/issues/2811

Cannot get Image written with cv2 and PIL Python 2.7

I am using python 2.7 with OpenCV and PIL Running on windows 10
I am trying to get an image written with cv2.imwrite() like follow Code:
x={{"Some Process On an Image "}}
#now saving the effects on real image
cv2.imwrite("temp.png", x)
now I am trying to get the text from the same image with pytesseract
and need to open the image
with the PIL library within another calss becuase the pytesseract
doesnt deal with cv2 images
from PIL import Image
import pytesseract
import cv2
if __name__ == '__main__':
url= Image.open("temp.png")
cv2.waitKey(200)
text=pytesseract.image_to_string(url)
print text
cv2.waitKey(0)
and Gave Me this Error:
Traceback (most recent call last):
File "C:\Users\Elie MA\Documents\NetBeansProjects\tesser\src\new_module.py", line 9, in <module>
text=pytesseract.image_to_string(url)
File "build\bdist.win32\egg\pytesseract\pytesseract.py", line 161, in image_to_string
File "build\bdist.win32\egg\pytesseract\pytesseract.py", line 94, in run_tesseract
File "c:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "c:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
why I cant get the image ?
is there any access rights or something ?
It can be showed with this Code:
url="temp.png"
cv2.imshow("temp_photo",url)
I want to know why PIL cannot get the image ?
With the knowledge that this image can be opened with photo viewer also
So Any one can Help Me?

pytesser doesn't work in python

I am using only PIL then it's work properly when I use pytesser then it doesn't work properly .What can i do for it?
from PIL import Image
from pytesser import *
image_file = Image.open("vote.jpg")
im = Image.open(image_file)
text = image_to_string(im)
print text
Traceback (most recent call last):
File "C:/Users/Tanvir/Desktop/py thon hand/hala.py", line 4, in <module>
image_file = Image.open("vote.jpg")
File "C:\Pythons27\lib\site-packages\PIL\Image.py", line 2286, in open
% (filename if filename else fp))
IOError: cannot identify image file 'vote.jpg'
I am find this solution .It was problem with PIL so at first I have to uninstall this PIL module then again install it and job done Every thing is okay.

Categories