当前位置:网站首页>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 ~~
边栏推荐
- 2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
- After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
- Vscode reading Notepad Chinese display garbled code
- How to get the first and last days of a given month
- 4. Cross entropy
- 小金额炒股,在手机上开户安全吗?
- [loss function] entropy / relative entropy / cross entropy
- Saving and reading of network model
- The difference between distribution function and probability density function of random variables
- Chapter XI feature selection
猜你喜欢
Vscode reading Notepad Chinese display garbled code
5. Contrôle discret et contrôle continu
3、多智能体强化学习
Basic implementation of pie chart
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
Recommend a document management tool mendely Reference Manager
Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
The solution of frame dropping problem in gnuradio OFDM operation
Measure the voltage with analog input (taking Arduino as an example, the range is about 1KV)
Several frequently used OCR document scanning tools | no watermark | avoid IQ tax
随机推荐
2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
Different methods for setting headers of different pages in word (the same for footer and page number)
Know how to get the traffic password
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
Authorization code of Axure rp9
Basic realization of line graph
Design method and reference circuit of type C to hdmi+ PD + BB + usb3.1 hub (rj45/cf/tf/ sd/ multi port usb3.1 type-A) multifunctional expansion dock
Transportation, new infrastructure and smart highway
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
A speed Limited large file transmission tool for every major network disk
Introduction to the types and repair methods of chip Eco
AttributeError: ‘str‘ object has no attribute ‘strftime‘
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
Probability distribution
5. Over fitting, dropout, regularization
For the first time in China, three Tsinghua Yaoban undergraduates won the stoc best student thesis award
Chapter 5 neural network
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference
The difference between distribution function and probability density function of random variables