admin 管理员组

文章数量: 887021

tiff

根据图像的不同大小进行裁剪图像

质量重点说明一下利用io能够读取内存占用比较大的图像

导入图像处理的包

import os
import skimage.io as io
from PIL import Image
import cv2
Image.MAX_IMAGE_PIXELS = None
import time
import shutil

创建一个文件夹用来存放裁剪的图像

def cut_image(input_path):temp_dir=input_path.strip(" ").split(".")[0]a=(temp_dir.strip('').split('\\')[-1])if os.path.exists(temp_dir):shutil.rmtree(temp_dir)os.makedirs(temp_dir)else:os.makedirs(temp_dir)
cut_image(input_path)

判断原始的tiff转变成的PNG 图像大小

1.如果大于200M

if (os.path.getsize(input_path) / 1024 / 1024) > (200): #todo 这里的大小大于200data = io.imread(input_path)[:, :, ::-1]# data2=data.copy()# print(data.shape)block_yise = 1024block_xize = 1024block_xize_w = int(int(block_xize) / 4)block_yise_h = int(int(block_yise) / 4)xsize = data.shape[1]ysize = data.shape[0]i = 0for x in range(0, xsize, block_xize):if x + block_xize < xsize:cols = block_xizeelse:cols = xsize - x# if cols==0:#     continuefor y in range(0, ysize, block_yise):if y + block_yise < ysize:row = block_yiseelse:row = ysize - ynew_data = data[x:(x + cols), y:(y + row)]if new_data.shape[0] <= block_xize_w or new_data.shape[1] <= block_yise_h or new_data.shape[2] <= 1:# print(f"error形状:{new_data.shape}")continueelse:image_path = os.path.join(temp_dir, f"aaanew_{a}_{i}.bmp")# print(new_data.shape)cv2.imwrite(image_path, new_data)time.sleep(1)i += 1

2.如果PNG图像大于20M<200M

elif (os.path.getsize(input_path) / 1024 / 1024) > (20):#todo 这里的图像是大于20Mdata = io.imread(input_path)[:, :, ::-1]# data2 = data.copy()# print(data.shape)block_yise = 640block_xize = 640block_xize_w = int(int(block_xize) / 4)block_yise_h = int(int(block_yise) / 4)xsize = data.shape[1]ysize = data.shape[0]i = 0for x in range(0, xsize, block_xize):if x + block_xize < xsize:cols = block_xizeelse:cols = xsize - x# if cols==0:#     continuefor y in range(0, ysize, block_yise):if y + block_yise < ysize:row = block_yiseelse:row = ysize - ynew_data = data[x:(x + cols), y:(y + row)]if new_data.shape[0] <= block_xize_w or new_data.shape[1] <= block_yise_h or new_data.shape[2] <= 1:# print(f"error形状:{new_data.shape}")continueelse:image_path = os.path.join(temp_dir, f"aaanew_{a}_{i}.bmp")# print(new_data.shape)cv2.imwrite(image_path, new_data)

3.如果图像小于5M

elif 0< (os.path.getsize(input_path) / 1024 / 1024) < (5):#todo 这里的图像是O<5# data2=cv2.imread(self.input_path)data = io.imread(input_path)[:, :, ::-1]image_path = os.path.join(temp_dir, f"aaanew_{a}.bmp")# print(new_data.shape)cv2.imwrite(image_path, data)

4.图像5M<图像大小<20M

else:data = io.imread(input_path)[:, :, ::-1] #这里的图像是# data2=data.copy()block_yise = 480block_xize = 480block_xize_w = int(int(block_xize) / 4)block_yise_h = int(int(block_yise) / 4)xsize = data.shape[1]ysize = data.shape[0]i = 0for x in range(0, xsize, block_xize):if x + block_xize < xsize:cols = block_xizeelse:cols = xsize - x# if cols==0:#     continuefor y in range(0, ysize, block_yise):if y + block_yise < ysize:row = block_yiseelse:row = ysize - ynew_data = data[x:(x + cols), y:(y + row)]print(new_data.shape)if new_data.shape[0] <= block_xize_w or new_data.shape[1] <= block_yise_h or new_data.shape[2] <= 1:continueelse:image_path = os.path.join(temp_dir, f"aaanew_{a}_{i}.bmp")print(new_data.shape)cv2.imwrite(image_path, new_data)time.sleep(1)i += 1

本文标签: tiff