how to read images from my home directory and resize it

cv2 resize
cv2 resize multiple images

I am trying to read the images and resize the image in my home directory file but didn't work please help how to read images and resize it.

    import cv2
    from PIL import Image
    img = cv2.resize('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))

AttributeError                            Traceback (most recent call last)
<ipython-input-103-dab0f11a9e2d> in <module>()
      1 import cv2
      2 from PIL import Image
----> 3 img = cv2.resize('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))

AttributeError: module 'cv2.cv2' has no attribute 'read'

To read all images of particular extension, e.g. "*.png", one can use cv::glob function

void loadImages(const std::string& ext, const std::string& path, std::vector<cv::Mat>& imgs, const int& mode)
    std::vector<cv::String> strBuffer;
    cv::glob(cv::String{path} + cv::String{"/*."} + cv::String{ext}, strBuffer, false);

    for (auto& it : strBuffer)
        imgs.push_back(cv::imread(it, mode));

std::vector<cv::Mat> imgs;
loadImages("*.png", "/home/img", imgs, cv::IMREAD_COLOR);

And then resize each image in buffer

for (auto& it : imgs)
    cv::resize(it, it, cv::Size{WIDTH, HEIGHT});

It should be easy to rewrite to python since almost all functions / data types have equivalents in python.

filenames = glob("/home/img/*.png").sort()
images = [cv2.imread(img) for img in filenames]

for img in images:
    cv2.resize(img, (WIDTH, HEIGHT))

The code is divided to parts instead of one-liner because it is more readable, at least for me.

Recursively resize images from one directory tree to , IrfanView has a nice batch processing tool built in to it. You can achieve exactly what you asked for with it. Start Batch Conversion/Rename (press B ); Tick Use  It’s also very fast, because it doesn’t upload the images to their servers. Your images never leave your computer. On the site, click the “Choose Images” button, and then find and select the images you want to resize. You can choose a single images or hundreds at once. Click the “Open” button when you’re ready.

Using glob module:

dpath = "C:/your_path/"
fmt = "png"    
size = (28, 28)

imgs = [cv2.resize(cv2.imread(fpath), size) for fpath in glob.glob("{}/*.{}".format(dpath, fmt))]

How can I scale all images in a folder to the same width?, This is very easy to do with imagemagick. You should be able to install it in the Software Center. I would suggest it for batch processing of  At some point or another, pretty much everybody has had to bulk process all the images in a directory. Either to resize them to a smaller size to share them, or do some other simple transformation. Well, here is a very easy way to do it with a dozen lines of Python.

the error here is that you are giving a directory to cv2.imread but it only accepts image-path as input else it throws the error.

so we can do is parse all the files in a folder using os module and then read the images one by one and than resize and do other operations on them.

import os
import cv2
from PIL import Image
size = (28, 28)
imagesPath = "C://Users//NanduCn//jupter1//train-scene classification//train"

for imageName in os.listdir(imagesPath):

    imageFullPath = os.path.join(imagesPath,imageName)
    img = cv2.resize(cv2.imread(imageFullPath), size)
    #do your processing here

also i here consider that you only have images in that folder. if you have other types of files or other folders inside it you can put a check before the os.path.join line.

Hope this helps.

how to shrink all images, jpg in a directory to another directory using , I read Using mogrify (imagemagick) to batch convert all my files (with params) and it This is a minimal version of the commands to give to resize to width 600 height 400 (if cd /home/shirish/input_directory/ mogrify -path  I needed these to update our website. Unfortunately, most of them were too large for the page and needed to be resized. I quickly created a .NET Core console app to fix the image sizes for me. I was able to write the code, resize the images, and then blog about it before 10:00am. Code is below. Enjoy!

Command-line Tools: Mogrify, Home (current) · Download · Tools · Command-line · Resources · Develop · Community. Search Use the magick mogrify program to resize an image, blur, crop, despeckle, dither, draw on, flip, join, Finally, we convert all our PNG images in a folder to the JPEG format: -display server, get image or font from this X server. How to Resize All Photos in a Folder. Digital photo files can be more than 2 megabytes for a single picture, depending on the camera’s settings. The size of these large files can cause problems for storing, emailing or online sharing, especially when there are a number of photos to manage.

ImageMagick v6 Examples -- Image File Handling, This modifier will resize the image that was just read in, immediately before file in the " .magick " sub-directory of my Linux/UNIX home directory, of the form. This code attempts to resize images in a directory called "imgs". Unfortunately for some reason when I uncomment the listFiles(..) loop will return null. Yet processing the same file straightaway outside the loop (res("imgs/foto_3.jpg")) works. So apparently, this loop is preventing the files from being read

How to store resize images into new directory?, How to store resize images into new directory?. Learn more about resize, imwrite, strcat Image Processing Toolbox. So, read out the entire post to know how to resize images without losing quality. Today we are here with the method to resize an image without losing quality. In Windows and another operating system, we resize the image with the software like paint etc or any other third party software which diminishes the size of the image but the pixel