I am trying to resize all images in my samples folder and then save all the images back in the test folder. The code below resize the images but it doesn't save them into the destination folder "test". I fail to understand what is the problem and I have referred to all documentation and nothing can help me understand what goes wrong here.
import cv2
import glob
import os
from tqdm import tqdm
imgs = glob.glob('samples/*.jpg')
print('Images files:', imgs)
width = 100
height = 100
folder = 'test'
if not os.path.exists(folder):
os.makedirs(folder)
#Resizing & Saving
for img in tqdm(imgs):
pic = cv2.imread(img, cv2.IMREAD_UNCHANGED)
pic = cv2.resize(pic, (width, height))
cv2.imwrite(os.path.join(folder,img), pic)
print(img)
The script can read the images and resize them successfully, the only part is imwrite doesn't function as expected.
May be the special character. Try to change the name of the image.
I don't have reputation to add this idea as comment. I'm Sorry.
Related
I wrote the following code to resize images in a folder to 100*100 and save the images in the same folder using for loop.I am wondering why it isn't working.
The following is the code I have written:
import cv2
import glob
images=glob.glob("*.jpg")
for image in images:
img=cv2.imread(image,0)
re=cv2.resize(img,(100,100))
cv2.imshow("Hey",re)
cv2.waitKey(500)
cv2.destroyAllWindows()
cv2.imwrite("resized_"+image,re)
I executed this:
nsu#NSU:~/Desktop/cryptology$ python3 img2.py
I got no error:
nsu#NSU:~/Desktop/cryptology$ python3 img2.py
nsu#NSU:~/Desktop/cryptology$
But the folder where i have saved the images and code is as it is...
what should i do?
**A viewer posted an answer which resulted the same.
**The problem MAY not be with the code.
**Please consider this
If you like you can use very robust module of python called Pillow for image manipulation, so first do pip3 install pillow
then run this code
from PIL import Image
import glob
images=glob.glob("*.jpg")
for im in images:
# print(im)
img = Image.open(im)
img = img.resize((100, 100), Image.ANTIALIAS)
img.save(im+'_resized.jpg')
hope it helps ... you need to improve the code if you want to keep the aspect ratio of the image
I have read a folder containing pictures using glob and imread. Now my I want to resize all of those pictures using for loop in cv2.resize.
following is my code but the output is not correct--
import cv2
import glob
path = glob.glob("C:/Users/RX-91-9/Desktop/prescriptions/*.jpg")
for file in (path):
img=cv2.imread(file)
cv2.imshow("Image", img)
cv2.cv2.waitKey(3)
cv2.destroyAllWindows()
for i in img:
resized_image = cv2.resize(i, (1600,1600))
cv2.imshow('resized_image', resized_image)
cv2.waitKey(3)
cv2.destroyAllWindows()
I don't know why the last for loop is not giving the expected output, i want all the images in 'img' to be resized. Please help if you find what is wrong in my for last for loop.
I assume that you have a list of images in some folder and you to resize all of them. You can run
import cv2
import glob
for filename in glob.glob('images/*.jpg'): # path to your images folder
print(filename)
img=cv2.imread(filename)
rl=cv2.resize(img, (500,500))
cv2.imwrite(f'{filename}resized.jpg', rl)
After cropping and saving images, I found that there are many full black images (RGB = 0,0,0). I want to delete these images.
The followings are the codes I have tried:
import os, glob
from PIL import image
def CleanUp_images():
for filename in glob.glob('/Users/Xin/Desktop/TestFolder/*.jpg'):
im = Image.open(filename)
pix = list(im.getdata())
if pix == [(0,0,0)]:
os.remove(im)
CleanUp_images()
However, the above codes didn't work out
Can anyone give me a help?
With os.remove(im) you're passing an image object to os.remove which only accepts strings when you just have to do:
os.remove(filename)
filename is the absolute path to your file (thanks to glob), so it will work.
Also if pix == [(0,0,0)]: this isn't going to work because the list has more than 1 element, even if all elements are black pixels. What works is creating a set of rgb tuples. If there are only black pixels the set has a size of 1:
if set(pix) == {(0,0,0)}:
I am new to python, so go easy on me! I am trying to apply a blur to every .bmp image in a folder. I am able to get the first half of the code to work, so the filter is applied to all the photos, but then I can't get it to re-save each image. I want to keep the original images, and add the new images to the folder. Here's what I have:
from PIL import Image from PIL import ImageFilter import os, fileinput, sys
##for every item in X folder that ends in X, apply a basic blur to the image##
for entry in os.scandir('/Users/kh'):
if entry.path.endswith('.bmp'):
img = Image.open(entry.path)
img = img.filter(ImageFilter.BoxBlur(2))
img.show()
##and then re-save each of those new images under a new filename##
# Split our original filename into name and extension
(name, extension) = os.path.splitext(filepath)
# Save with "_blur" added to the filename
img.save(name + '_blur' + extension)
# Save the image as a BMP
img.save(name + '.bmp')
I've tried a bunch of other stuff, but this code is the closest I've gotten. Thanks so much for your help.
I tried your code. I actually get:
NameError: name 'filepath' is not defined
because you have a filepath which should be entry.path:
(name, extension) = os.path.splitext(entry.path)
Besides this, your code works, except that you have both images blurred. The last line:
img.save(name + '.bmp')
is cleary not needed, it overwrites the original images with the blurred ones.
I'm working on a project where two imported librairies are not working well with each other, I know it is possible to get the size of an image using :
from PIL import Image
img = Image.open(logo)
width, height = img.size
But I'd like to know if it is also possible to do that usin io ? I couldn't find anything on that
logo = request.FILES.get('logo')
img = io.BytesIO(logo.read())
... ?