当前位置:网站首页>How does Matplotlib and PIL image integrate and save multiple pictures into one picture
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
2022-07-08 01:26:00 【Bosong】
Catalog
1. Law 1 : Generate multi row and multi column graph
2. Law two : Generate a single line / Single column diagram
stay This article How to generate multiple graphs is described in , But many times, we need to comprehensively compare and display the pictures , Therefore, it needs to be merged in one picture .
Here is 2 Methods : Method 1 usually shows the idea of multiple rows and columns , Reference resources writing 1; Method 2 can be used to send a single line / Single column long graph , Reference resources writing 2.
1. Law 1 : Generate multi row and multi column graph
import os
import inspect
import pandas as pd
import numpy as np
from datetime import datetime
from PIL import Image
import glob, os
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
prefix = 'flow'
# * matching 1 individual or Multiple characters ,files It can display all file names ( Tabular form )
files=glob.glob('fig_lots/flow_*')
num=len(files)
img_np = []
# sz Represents the pixel of the picture
sz = (2000, 1400)
for i in range(0, num):
file = prefix+'_'+str(i+1)+'.png'
# Traverse and open these pictures , and resize by sz
im = Image.open('fig_lots/'+file)
im = im.resize(sz,Image.ANTIALIAS)
# Turn the picture into array
im_array = np.atleast_2d(im)
img_np.append(im_array)
# The length of the picture 、 wide 、 The channel number
print(im_array.shape[0], im_array.shape[1], im_array.shape[2])
# Canvas size figsize according to sz and rows cols Jointly determine , That is, the canvas is horizontal : Canvas portrait = sz Length *cols :sz The width of *rows
fig = plt.figure(figsize = (20, 39))
rows = 11
cols = 4
for i in range(1, rows*cols + 1):
img_array = img_np[i-1]
# Subgraph location
ax = fig.add_subplot(rows, cols, i)
plt.axis('off') # Remove the axis of each subgraph
plt.imshow(img_array)
plt.subplots_adjust(wspace = 0, hspace = 0) # Modify the interval between subgraphs
plt.savefig('flowrc.png', dpi=400)
2. Law two : Generate a single line / Single column diagram
import numpy as np
from PIL import Image
import glob, os
# Use only PIL Library by converting pictures to array Format , Merge export . Unable to adjust the size and spacing of the picture for the time being 、 Only one way 、 Single column Export .
if __name__=='__main__':
# * matching 1 individual or Multiple characters ,files It can display all file names ( Tabular form )
files=glob.glob('fig_lots/flow_*')
num=len(files)
filename_lens=[len(x) for x in files] #length of the files
min_len=min(filename_lens) #minimal length of filenames
max_len=max(filename_lens) #maximal length of filenames
if min_len==max_len:#the last number of each filename has the same length
files=sorted(files) #sort the files in ascending order
else:#maybe the filenames are:x_0.png ... x_10.png ... x_100.png
index=[0 for x in range(num)]
for i in range(num):
filename=files[i]
start=filename.rfind('_')+1
end=filename.rfind('.')
file_no=int(filename[start:end])
index[i]=file_no
index=sorted(index)
files=[prefix+'_'+str(x)+'.png' for x in index]
print(files[0], files)
# Image.open Picture attribute
baseimg=Image.open('fig_lots/'+files[0])
# .size Display size (2000,1400)
sz=baseimg.size
# 0-255 Of the displayed picture 2 Dimension group
basemat=np.atleast_2d(baseimg)
# From 2 The picture begins , Merge with the previous figure
for i in range(1,num):
file=files[i]
im=Image.open('fig_lots/'+file)
im=im.resize(sz,Image.ANTIALIAS)
# The image is converted into an array
mat=np.atleast_2d(im)
basemat=np.append(basemat,mat,axis=0)
# The image is converted back from the array
final_img=Image.fromarray(basemat)
print(final_img)
final_img.save('merged.png')
Generating multiple graphs is “ discharge ”, Aggregated into a graph is “ closed ”,“ discharge ” Yes “ From thin to thick ” The feeling of ,“ closed ” Yes “ From thick to thin ” The feeling of .
See a bunch of beautiful pictures , Happy ~~
边栏推荐
- Macro definition and multiple parameters
- 2021-04-12 - new features lambda expression and function functional interface programming
- Fundamentals - integrating third-party technology
- [loss function] entropy / relative entropy / cross entropy
- [deep learning] AI one click to change the sky
- 2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
- A little experience from reading "civilization, modernization, value investment and China"
- Application of state mode in JSF source code
- STM32GPIO口的工作原理
- Ag9311maq design 100W USB type C docking station data | ag9311maq is used for 100W USB type C to HDMI with PD fast charging +u3+sd/cf docking station scheme description
猜你喜欢
Redis master-slave replication
Chapter 7 Bayesian classifier
USB type-C docking design | design USB type-C docking scheme | USB type-C docking circuit reference
Vscode reading Notepad Chinese display garbled code
Micro rabbit gets a field of API interface JSON
Vscode is added to the right-click function menu
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
The combination of relay and led small night light realizes the control of small night light cycle on and off
Common fault analysis and Countermeasures of using MySQL in go language
The solution of frame dropping problem in gnuradio OFDM operation
随机推荐
Kuntai ch7511b scheme design | ch7511b design EDP to LVDS data | pin to pin replaces ch7511b circuit design
NPM Internal Split module
Kafka-connect将Kafka数据同步到Mysql
Connect to the previous chapter of the circuit to improve the material draft
The Ministry of housing and urban rural development officially issued the technical standard for urban information model (CIM) basic platform, which will be implemented from June 1
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
STM32GPIO口的工作原理
Smart grid overview
npm 內部拆分模塊
How to use education discounts to open Apple Music members for 5 yuan / month and realize member sharing
A little experience from reading "civilization, modernization, value investment and China"
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Codeforces Round #804 (Div. 2)
2021-03-14 - play with generics
4. Strategic Learning
Saving and reading of network model
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
2021-03-06 - play with the application of reflection in the framework
Definition and classification of energy
General configuration tooltip