pytorch修改图片尺寸大小
import cv2from torch.utils.data import Datasetfrom PIL import Imageimport osimport matplotlib.pyplot as pltclass MyData(Dataset):# 初始化 root_dir大的路径和label_dir具体内容/获取地址def __init__(self, root_dir, label
·
import cv2 from torch.utils.data import Dataset from PIL import Image import os import matplotlib.pyplot as plt class MyData(Dataset): # 初始化 root_dir大的路径和label_dir具体内容/获取地址 def __init__(self, root_dir, label_dir): # 创建全局变量 self.root_dir = root_dir self.label_dir = label_dir # 获取一个路径地址 join()作用为拼接地址 self.path = os.path.join(self.root_dir + "/" + self.label_dir).replace("jpg", "png") # bmp # self.path = os.path.join(self.root_dir + "/" + self.label_dir).replace("bmp", "png") # 获取路径下的所有列表 self.img_path = os.listdir(self.path) print(self.img_path) print("ok") # idex作为一个编号 def __getitem__(self, idx): # 读取其中的一个图片 img_name = self.img_path[idx] print(img_name) # 程序的相对路径 img_iten_path = os.path.join(self.root_dir, self.label_dir, img_name) # 图片打开 img = Image.open(img_iten_path) # 160 x120 == 640x480 # new_img = img.resize((640, 480)) # 256x256 == 1024 x1024 # new_img = img.resize((1024, 1024)) # 320x240 == 1280 x960 # new_img = img.resize((1280, 960)) # 384x288 == 1536x1152 # new_img = img.resize((1536, 1152)) # 640x480 == 2560 x1920 # new_img = img.resize((2560, 1920)) # 640x512 == 2560 x2048 # new_img=img.resize((2560, 2048)) # 1280 x1024 == 5120 x4196 new_img = img.resize((5120, 4196)) # 要保存的图片 # 160x120 == 640x480 # if not os.path.exists("../opencv/tu/480/"): # os.mkdir("../opencv/tu/480/") # print("目录已经创建") # new_img.save("../opencv/tu/480/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 256x256 == 1024x1024 # if not os.path.exists("../opencv/tu/1024/"): # os.mkdir("../opencv/tu/1024/") # print("目录已经创建") # new_img.save("../opencv/tu/1024/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 320x240 == 1280x960 # if not os.path.exists("../opencv/tu/960/"): # os.mkdir("../opencv/tu/960/") # print("目录已经创建") # new_img.save("../opencv/tu/960/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 384x288 == 1536x1152 # if not os.path.exists("../opencv/tu/1152/"): # os.mkdir("../opencv/tu/1152/") # print("目录已经创建") # new_img.save("../opencv/tu/1152/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 640x480 == 2560x1920 # if not os.path.exists("../opencv/tu/1920/"): # os.mkdir("../opencv/tu/1920/") # print("目录已经创建") # new_img.save("../opencv/tu/1920/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 640x512 == 2560 x2048 # if not os.path.exists("../opencv/tu/2048/"): # os.mkdir("../opencv/tu/2048/") # print("目录已经创建") # new_img.save("../opencv/tu/2048/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 1280x1024 == 5120x4196 if not os.path.exists("../opencv/tu/1024/"): os.mkdir("../opencv/tu/1024/") print("目录已经创建") new_img.save("../opencv/tu/1024/" + img_name.replace("jpg", "png").replace("bmp", "png")) # 它的一个文件 label = self.label_dir return img, label # 返回列表的一个长度 def __len__(self): return len(self.img_path) # 数据集路径 root_dir = "./tu" # 路径要处理的图片 # 160x120==640x480 # low_label_dir="160x120" # 256x256 == 1024 x1024 # low_label_dir="256x256" # 320x240 == 1280 x960 # low_label_dir="320x240" # 384x288==1536x1152 # low_label_dir="384x288" # 640x480==2560x1920 # low_label_dir="640x480" # 640x512==2560x2048 # low_label_dir="640x512" # 1280x1024==5120x4196 low_label_dir = "1280x1024" # 低分辩的数据集 low_dataset = MyData(root_dir, low_label_dir) print("图片数量:", len(low_dataset)) for x in low_dataset: img, label = x
上面可根据自己需要更改,代码懒优化了就复制粘贴了,需要用到的路径和大小,不要的可以删除。这个是根据B站代码进行学习,然后做的修改B站的up主是小土堆,有兴趣的可以去b站学习一下,内容还是讲的非常细致的
更多推荐
已为社区贡献2条内容
所有评论(0)