import glob
from PIL import Image
marked = glob.iglob("D:/Users/username/Desktop/cells/Marked")
img = Image.open(marked)
img.show()
I am trying to create a neural network using an image dataset contained in the Folder marked. When I run the code I get the error:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2547, in open
fp.seek(0)
AttributeError: 'generator' object has no attribute 'seek'
I do not understand what the error means, it seems to be misdirected?
You'll need to specify a wildcard at the end of your path and iterate:
images = []
for f in glob.iglob("D:/Users/username/Desktop/cells/Marked/*"):
images.append(np.asarray(Image.open(f)))
images = np.array(images)
See this answer, which uses PIL.Image and glob to find all images in the folder and load them into an array.
from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.gif'): #assuming gif
im=Image.open(filename)
image_list.append(im)
Related
I want read image name like flower.png unnamed.png. Is there any way that I can read all images images one by one like we can read text file with open ("data.txt", "r") as myfile:
data = myfile.read().splitlines().. Hope you will give me a solution
thanks
enter code here from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.img'):
im=Image.open(filename)
image_list.append(im)
If you want to get all file name from a folder, you can use os.listdir
import os
filename = os.listdir("YOUR_FOLDER_NAME")
I have some images with 24 bit depth that I should convert them to 8 bit depth.
So I can do it for one image but for multiple images I get this error :
a=img.convert("P", palette=Image.ADAPTIVE, colors=8)
AttributeError: 'list' object has no attribute 'convert'
image:
I tried this code:
from PIL import Image
import os
os.chdir('D:\\background')
img=os.listdir()
a=img.convert("P", palette=Image.ADAPTIVE, colors=8)
a.save('D:\\test')
So if I want to convert multiple files with pillow how should I do it?
Check documentation here https://pillow.readthedocs.io/en/stable/reference/Image.html
img=os.listdir(), and img is a list of filenames under the folder 'D:\background'. Here's my implementation:
from PIL import Image
import os
base_dir_src = 'D:\\background'
base_dir_dst = 'D:\\test'
os.chdir()
for filename in os.listdir():
with Image.open(filename) as img:
converted_img = img.convert("P", palette=Image.ADAPTIVE, colors=8)
converted_img.save(os.path.join(base_dir_dst, filename))
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
DATADIR ="C:/Users/Park/Project/TrainingData"
CATEGORIES = ["doll", "machine", "puzzle"]
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_COLOR)
print(" Image shape : ", img_array.shape)
plt.imshow(img_array)
plt.show()
Each folder contains 50 image files in jpg format. The output is up to the second 'machine' folder and the 'puzzle' folder is not output to the third folder. If you change the order of the folder names, the pictures are displayed regardless of the number of images. When I try to output the third folder, I get a 'NoneType' object has no attribute 'shape' error.
To surface the problem quickly, add
if img_array is None:
print("imread failed on {}".format(img))
So I'm trying to use pillow to apply kernals to .jpg images in python and I'm trying to process an entire image set at once. I have a script here that will iterate through the training set, apply the edge finding filter, and save the result in a different folder. Whenever I try to do that using the .save command it throw an error telling me there is no such file as the one I'm saving it to. The thing is, I'm trying to create that file. It doesn't do this when I've got a definite file name, only when I'm trying to do it iterativly. Can anyone see what the problem here is?
import PIL
import os
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (FIND_EDGES)
num = 1
path = "/home/<me>/Desktop/<my documents>/<my project>/Training Set Cars/"
dirs = os.listdir(path)
for filename in dirs:
print(filename)
im = Image.open(path + filename)
aString = str(num)
print("/home/<me>/Desktop/<my documents>/<my project>/Training Car Edges/%s.jpg" % aString)
ERROR HERE>>> im.filter(FIND_EDGES).save("/home/<me>/Desktop/<my documents>/<my project>/Training Car Edges/%s.jpg" % aString)
num += 1
I am trying to execute this script
from PIL import Image
im = Image.open("image.jpg")
nx, ny = im.size
It is working fine when I run it in python shell
pytesser_v0.0.1]#env python
>>> from PIL import Image
>>> im = Image.open("image.jpg")
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=46x24 at 0x7FA4688F16D0>
but when I put it in a some test.py file and run it like python test.py
I am getting this error
File "test1.py", line 17, in <module>
im = Image.open("image.jpg")
File "/usr/local/python.2.7.11/lib/python2.7/site-packages/PIL/Image.py", line 2309, in open
% (filename if filename else fp))
IOError: cannot identify image file 'image.jpg'
please help me with this issue, Thanks
PS: Earlier I installed PIL from Imaging-1.1.7 setup.py, later I installed Pillow, I think the problem was in the mutual presence of the PIL and Pillow library on the machine.
Seems like PIL library haven't fixed this bug yet.
Here is my solution:
Open image using OpenCV library, then convert it to PIL image
from PIL import Image
import cv2
image_path = 'Folder/My_picture.jpg'
# read image using cv2 as numpy array
cv_img = cv2.imread(image_path)
# convert the color (necessary)
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
# read as PIL image in RGB
pil_img = Image.fromarray(cv_img).convert('RGBA')
Then you can operate with it as with a regular PIL image object.
Make sure that "image.jpg" is in the same directory as "test1.py".
If it isn't then you could either move it, or put the correct directory inside of Image.open().
I have the same issue.
This is because the test.py does not have the same pathname. If you are working in the same folder it will work.
However, the solution i found was to put in the full path + file name so that it is unambiguous.
"c:\...fullpath...\image.jpg"
You can do it like this:
from PIL import Image
import os
curDir = os.getcwd()
fileName = "image.jpg"
fn = curDir + "\\" + fileName
print(fn)
image = Image.open(fn)
image.show()
This works. Please let me know if you find better.