#!/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
Related
The file evidently exists, but imread() is unable to identify the image. Is there something I am missing? I'm still new to python and its respective libraries.
Code (Python 3.7.9)
import os
import time
import numpy as np
import matplotlib.pyplot as plt
from randomlyShiftChannels import *
from alignChannels import *
from utils import *
#Path to your data directory
data_dir = os.path.join('..', 'data', 'demosaic')
#List of images
image_names = ['balloon.jpeg','cat.jpg', 'ip.jpg',
'puppy.jpg', 'squirrel.jpg', 'pencils.jpg',
'house.png', 'light.png', 'sails.png', 'tree.jpeg'];
print('Evaluating alignment...')
for imgname in image_names:
# Read image
imgpath = os.path.join(data_dir, imgname)
print("File exists:", os.path.exists(imgpath))
img = imread(imgpath)
Output
File exists: True
Traceback (most recent call last):
File "D:\Spring2021\CS370\week 6\p2-release\code\evalAlignment.py", line 48, in <module>
img = imread(imgpath)
File "D:\Spring2021\CS370\week 6\p2-release\code\utils.py", line 14, in imread
img = plt.imread(path).astype(float)
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\matplotlib\pyplot.py", line 2246, in imread
return matplotlib.image.imread(fname, format)
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\matplotlib\image.py", line 1496, in imread
with img_open(fname) as image:
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\PIL\Image.py", line 2944, in open
"cannot identify image file %r" % (filename if filename else fp)
UnidentifiedImageError: cannot identify image file '..\\data\\demosaic\\balloon.jpeg'
I faced the identical issue using Spyder (Spyder 5.0.0 and 5.0.1).
No errors appeared when running the script from the command line.
Found workaround for Spyder: use pil_to_array() instead of imread()
from PIL import Image
img = pil_to_array(Image.open(imgpath))
Try converting your path to an absolute path, there are reasons we call it good practice
Sample code:
import os
os.path.abspath("mydir/myfile.txt") #'C:/example/cwd/mydir/myfile.txt'
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)
I want use PIL .save() method for export my PIL image list to pdf.
in the PIL document , saving part say:
=> we can use append_images option for pdf format.
and in pillow's github page , this issue say : Added append_images to PDF saving #2526
I wrote this code:
import PIL
im1 = PIL.Image.open("1.jpg").convert("RGB")
im2 = PIL.Image.open("2.jpg").convert("RGB")
im3 = PIL.Image.open("3.jpg").convert("RGB")
images = [im1,im2,im3]
images[0].save("out.pdf", save_all=True, append_images=images[1:])
but it doesn't work!
These errors raised:
Traceback (most recent call last):
File "sample.py", line 13, in <module>
gif.save("out.pdf", save_all=True, append_images=images)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 1928, in save
save_handler(self, fp, filename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 55, in _save_all
_save(im, fp, filename, save_all=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 182, in _save
Image.SAVE["JPEG"](im, op, filename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 609, in _save
info = im.encoderinfo
AttributeError: 'Image' object has no attribute 'encoderinfo'
Try this format
from PIL import Image
im1 = PIL.Image.open("1.jpg").convert("RGB")
im2 = PIL.Image.open("2.jpg").convert("RGB")
im3 = PIL.Image.open("3.jpg").convert("RGB")
images = [im2,im3]
im1.save("out.pdf", save_all=True, append_images=images)
This problem is solved in PIL ver 5.0.0 (https://pillow.readthedocs.io/en/latest/).
Somewhat off topic, but if you have a large number of images to convert, using list comprehension is the way to go.
from PIL import Image
im1=Image.open('1.png').convert('RGB')
images = [Image.open(f”{imgNumber}.png”).convert('RGB') for imgNumber in range(2, 100)]
im1.save("imgBook.PDF", save_all=True, append_images=images)
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
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.