I'd like to resize the numpy image data set.
Specifically, I would like to resize the image data set with the shape of the numpy array (2000, 512, 512, 3) to the numpy array (2000, 256, 256, 3). ( 5512×512 images are resized to 256×256)
Therefore, I would appreciate it if you could tell me how to do it.
python tensorflow numpy keras
Code that resizes the size of the image in the directory specified by Jupiter in bulk.
You can now resize images in IMG('image') .convert('jpg') .resize(256,256)
in the following cells:
from PIL import Image
importos
import shutil
import cv2
from pathlib import Path
class IMG():
def__init__(self,dic):
"""Initialize, enter the path of the directory where the image is placed"""
self.directory=dic
def convert (self, suffix):
Method of changing the extension to any extension
US>The argument contains an extension excluding .(dot).
for i inos.listdir (self.directory):
fp=self.directory+'/'+i
img = cv2.imread(fp)
file_Path=Path(fp)
cv2.imwrite(self.directory+'/'+file_Path.stem+'.'+suffix,img)
if file_Path.suffix!='.jpg':
os.remove(fp)
return self
defrename(self, new_file):
"""Methods for Changing the File Name to Any File Name to a File Name
"In the argument, enter the name of the file you want to change."
data=os.listdir(self.directory)
for i, old_name in enumerate (data):
path=self.directory+'/'+old_name
# Determining the File Name
new_name = new_file+"_{0:03d}.jpg".format(i+1)
new_path=self.directory+'/'+new_name
# Renaming Files
os.rename(path, new_path)
return self
default (self, width, height):
"""Method of changing the file size to any size
"In the argument, add width and height."""
for i inos.listdir (self.directory):
path=self.directory+'/'+i
img=cv2.imread(path)
img_resize=cv2.resize(img,(width,height))
cv2.imwrite(path,img_resize)
return self
def make_zip(self,zip_name):
"""A method to zip an image in a directory
"In the argument, enter the zip file name to be created."""
shutil.make_archive(zip_name, 'zip', root_dir=self.directory)
© 2024 OneMinuteCode. All rights reserved.