Python - Tools: FIG sliced into a large panel, the panel larger than the combination of FIG.

Training keras encountered a problem that is out of memory after the .fit .fit_generator still fit into a picture (I picture is 8192 × 8192 big picture == 64M). So the solution is to cut large map small map, the small figure throwing to train, ran out of the map and then makes up a larger image

It was found that my keras (win10 - 16G memory) just let go up to four panels (2048 × 2048 × 4 == 16M),

More will be given exit: Allocation of 4831838208 exceeds 10% of system memory.

The reason is probably to keep itself apart from numpy these figures, keras training will correspond to additional consumption

A large panel of FIG sliced ​​into

'' ' 
Reads a picture 0.bmp, cut into a specified number of small pictures (16) 
Folder name OUT 
' '' 
from PIL Import Image
 Import SYS, os 
cut_num = 4 # 4 * 4 = 16 picture 
# will filled square image 
DEF fill_image (image): 
    width, height = image.size    
     # selected length and width of the larger value as a new image 
    new_image_length width = IF width> height the else height    
     # generates a new image [white] 
    # new_image = Image.new (image.mode, (new_image_length, new_image_length), Color = 'White')     
    new_image =Image.new (image.mode, (new_image_length, new_image_length))
     # previously pasted on FIG FIGS new, centrally 
    IF width> height: # picture wider than high, the vertical dimension of the picture is populated 
    # (X, Y ) paste on FIG tuple represents the relative location of the start of FIG 
        new_image.paste (Image, (0, int ((new_image_length - height) / 2 )))
     the else : 
        new_image.paste (Image, (int ((new_image_length - width) / 2 ), 0))    
     return new_image
 # tangential FIG 
DEF cut_image (Image): 
    width, height = image.size 
    item_width = int (width / cut_num) 
    box_list = []    
     # (left, upper, right, lower) 
    for i in range(0,cut_num):#两重循环,生成图片基于原图的位置 
        for j in range(0,cut_num):           
            #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
            box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width)
            box_list.append(box)

    image_list = [image.crop(box) for box in box_list]    
    return image_list
#保存
def save_images(image_list):
    index = 1 
    for image in image_list:
        image.save('out/'+str(index) + '.bmp', 'BMP')
        index += 1

if __name__ == '__main__':
    file_path = "0.bmp"
    os.mkdir("out")
    image = Image.open(file_path)   
    #image.show()
    image = fill_image(image)
    image_list = cut_image(image)
    save_images(image_list)

 

 

 

Second, larger than the panel of FIG combination

'' ' 
Specified folder inside the images together to form a big picture 
' ' 
Import PIL.Image AS Image
 Import OS 
 
IMAGES_PATH = ' OUT \\ '   # picture set address 
IMAGES_FORMAT = [ ' .bmp ' , ' .BMP ' ]   # Image format 
IMAGE_SIZE = 2048   # each small size of the picture 
image ~ = 4   # picture interval, that is, after the merger into one picture, there are a few lines 
IMAGE_COLUMN = 4   # picture interval, that is merged into a map after a total of a few column 
IMAGE_SAVE_PATH = ' final.bmp '   # after the address conversion picture 
 
# get the name of all the pictures in the photo Gallery address
= image_names [name for name in the os.listdir (IMAGES_PATH) for Item in IMAGES_FORMAT IF 
               os.path.splitext (name) [ . 1] == Item] 
 
# simple number determination is performed for the image size setting and the actual parameter set 
IF len (image_names) = image ~ *! IMAGE_COLUMN:
     the raise a ValueError ( " ! number of parameters and requirements of the composite image does not match " ) 
 
# define an image stitching function 
DEF image_compose (): 
    to_image = Image.new ( ' the RGB ' , (IMAGE_COLUMN IMAGE_SIZE *, image ~ * IMAGE_SIZE)) # create a new map 
    #Loop through, in order to each image pasted to a corresponding position 
    for Y in Range (. 1, image ~ +. 1 ):
         for X in Range (. 1,. 1 IMAGE_COLUMN + ): 
            from_image = Image.open (IMAGES_PATH + image_names [IMAGE_COLUMN * (Y -. 1) + X -. 1 ]) a resize (. 
                (IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS) 
            to_image.paste (from_image, ((X -. 1) * IMAGE_SIZE, (Y -. 1) * IMAGE_SIZE)) 
    to_image = to_image.convert ( ' L ' )
     return to_image.save (IMAGE_SAVE_PATH) # save the new FIG 
image_compose () # call function

 Note the name of the file in numerical order, 000 102 111 213 .... so ...

Guess you like

Origin www.cnblogs.com/dzzy/p/11401800.html