论文绘图与合成图片过程中常见问题
生活随笔
收集整理的這篇文章主要介紹了
论文绘图与合成图片过程中常见问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、PIL格式與cv2格式的轉換
問題詳見Stack overflow
To convert from PIL image to OpenCV use:
To convert from OpenCV image to PIL image use:
import cv2 import numpy as np from PIL import Imageopencv_image=cv2.imread("demo2.jpg") # open image using openCV2# convert from openCV2 to PIL. Notice the COLOR_BGR2RGB which means that # the color is converted from BGR to RGB color_coverted = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB) pil_image=Image.fromarray(color_coverted)2、圖片拼接
詳見鏈接
3、添加文字
目前用到的添加文字的方法由兩種——使用PIL包或者cv2包。
3.1、使用cv2包添加文字
import cv2 img = cv2.imread('caijian.jpg') font = cv2.FONT_HERSHEY_SIMPLEX # 設置字體imgzi = cv2.putText(img, '000', (50, 300), font, 1.2, (255, 255, 255), 2)# img_data = cv2.putText(img_board, 'Data', (1, 300), font, 2, (0, 0, 0), 3) # # 圖像,文字內容, 坐標 ,字體,大小,顏色,字體厚度詳見鏈接
這個還可以設置文字類別
3.2、使用PIL包添加文字
強烈建議使用PIL包添加文字cv2能添加的文字類型非常少,而PIL包可以讀取ttf文件,理論上所有字體都可以用
# In[] 添加上側白板與文字 # 上側白板,這里需要注意一下,設置大小的時候是這樣的:(寬度,高度),與cv2剛好相反 img_board_up = Image.new('RGB', (img_left_font.size[0], up_board_height), (255, 255, 255))#選擇文字字體和大小 setFont = ImageFont.truetype(r'C:\Windows\Fonts\times.ttf', 100) #設置文字顏色 fillColor = "#000000" # 黑色# 新建繪圖對象并添加文字(文字都是通過繪圖對象加到原圖上去的) img_words_up = ImageDraw.Draw(img_board_up) for i, class_name in enumerate(img_name_list.keys()):img_words_up.text(((fig_width+interval)*i+left_board_width, 35),class_name,font=setFont,fill=fillColor,direction=None)4、cv2圖片保存為PDF
先轉為PIL圖片格式,然后用
image[0].save(out_fname, save_all = True, quality=100, append_images = images[1:])將整個列表中的圖片都保存為PDF
5、matplotlib使用
5.1、提高圖像的質量(分辨率)
plt.rcParams['figure.figsize'] = (6.0, 4.02) plt.rcParams['savefig.dpi'] = 600 #圖片像素 plt.rcParams['figure.dpi'] = 600 #分辨率5.2、去除多余的白邊
plt.savefig(save_path, bbox_inches='tight',pad_inches=0)5.3、設置子圖間距
matplotlib之pyplot模塊——調整子圖布局(subplots_adjust、tight_layout)
How to remove gaps between subplots in matplotlib
5.4 字體設置為新羅馬
plt.rcParams.update({'font.family': 'sans-serif','font.sans-serif': ['Times New Roman'],})6、日志文件讀取與數據查找
def catch_data(file):# file = "cifar-regular-0"# 讀取并整合數據with open(file, "rb") as f:data = f.readlines()data = [str(i) for i in data] # 注意這里一定得有str()將byte類型數據轉為str類型data_str = "".join(data) # 將data變成一個字符串# 正則匹配pattern_Iter = r'Iter (\d+)'Iter = re.findall(pattern_Iter, data_str)Iter = [int(i) for i in Iter]pattern_bits = r'Bit/dim ([^(a-z]+)\('bits = re.findall(pattern_bits, data_str)bits = [float(i) for i in bits]pattern_NFE = r'NFE [^(a-z]+\((.{6,10})\) \|'NFEs = re.findall(pattern_NFE, data_str)NFEs = [float(i) for i in NFEs]pattern_bits_epoch = r' Bit\/dim ([^:f]{6,10}), 'bits_epoch = re.findall(pattern_bits_epoch, data_str)bits_epoch = [float(i) for i in bits_epoch]pattern_Epoch = r'Epoch (\d+)'Epoch = re.findall(pattern_Epoch, data_str)Epoch = [int(i) for i in Epoch]return Iter, bits, NFEs, bits_epoch, Epoch最終我們是實現過程中用到的代碼
part 1:圖像裁剪
# -*- coding: utf-8 -*- import cv2 import osresult_fig_size = 600def get_img(input_dir):img_paths = []for (path,dirname,filenames) in os.walk(input_dir):for filename in filenames:img_paths.append(path+'/'+filename)print("img_paths:",img_paths)return img_paths# if __name__ == '__main__': output_dir = "./toy_stage2" # 保存截取的圖像目錄 input_dir = "./toy_stage" # 讀取圖片目錄表 img_paths = get_img(input_dir) print('圖片獲取完成 。。。!')scale = len(img_paths) for i,img_path in enumerate(img_paths):a = "#"* int(i/1000)b = "."*(int(scale/1000)-int(i/1000))c = (i/scale)*100img_name = img_path.split('/')[-1]print('正在處理圖像: %s' % img_name)img = cv2.imread(img_path) # cropImg_01 = img[50:250,360:560] # 裁剪cropImg_01 = cv2.resize(img[52:248,362:558],(result_fig_size,result_fig_size)) # 放縮cv2.imwrite(output_dir + '/'+img_path.split('/')[-1], cropImg_01)print('{:^3.3f}%[{}>>{}]'.format(c,a,b))part2:圖像拼接與添加文字
使用Image模塊實現圖像拼接與文字添加
# -*- coding: utf-8 -*- """ Created on Wed Nov 3 16:27:35 2021@author: Chen Wei 人一能之,己百之;人十能之,己千之。 """import cv2 import os import numpy as np from PIL import Image, ImageDraw, ImageFontfig_width = 600 fig_height = 600 interval = 20 left_board_width = 500 up_board_height = 150def get_img(input_dir):img_paths = []for (path,dirname,filenames) in os.walk(input_dir):for filename in filenames:img_paths.append(path+'/'+filename)print("img_paths:",img_paths)return img_paths# In[1] 準備工作 ### 讀取整個文件夾 output_dir = "./toy_stage3" # 保存截取的圖像目錄 input_dir = "./toy_stage2" # 讀取圖片目錄表 img_paths = get_img(input_dir)# 名單表 img_name_list = {'checkerboard':0, '8gaussian':1, 'rings':2,'swissroll':3,'moons':4,'2spirals':5,'circles':6,'pinwheel':7} method_name_list = {'ffjord':0, 'opt':1} stage_list = {'1000':0,'3000':1,'5000':2,'7000':3,'9000':4}# In[] # 水平間隔白板 r = np.ones((fig_height,interval),dtype=np.uint8) * 255 g = np.ones((fig_height,interval),dtype=np.uint8) * 255 b = np.ones((fig_height,interval),dtype=np.uint8) * 255 # 合并通道,形成圖片 board_h = cv2.merge([b, g, r]) all_imgs = {} for k, class_name in enumerate(img_name_list.keys()):# 找出這一類的所有樣本class_paths = list(filter(lambda img_path: class_name in img_path, img_paths))all_imgs[class_name] = {}for i, method_name in enumerate(method_name_list.keys()):# 找出這種方法的所有樣本class_method_path = list(filter(lambda class_path: method_name in class_path, class_paths))# 讀取它們imgs = []# all_imgs[class_name][method_name] = []for j, img_path in enumerate(class_method_path):imgs.append(cv2.imread(img_path))if j < len(stage_list) - 1:imgs.append(board_h)# 水平拼接all_imgs[class_name][method_name] = cv2.hconcat(imgs)# cv2.imwrite(output_dir+ '/'+method_name+class_name+'.jpg', all_imgs[class_name][method_name])# In[] 豎直拼接 # 豎直間隔白板 r = np.ones((interval,all_imgs[class_name][method_name].shape[1]),dtype=np.uint8) * 255 g = np.ones((interval,all_imgs[class_name][method_name].shape[1]),dtype=np.uint8) * 255 b = np.ones((interval,all_imgs[class_name][method_name].shape[1]),dtype=np.uint8) * 255 # 合并通道,形成圖片 board_v = cv2.merge([b, g, r])all_imgs_concat = {} for k, class_name in enumerate(img_name_list.keys()):class_all_methods = list(all_imgs[class_name].values())class_all_methods.insert(1, board_v)all_imgs_concat[class_name] = cv2.vconcat(class_all_methods)# cv2.imwrite(output_dir+ '/'+class_name+'.jpg', all_imgs_concat[class_name])# In[] 添加文字 ############################## 添加左側白板和文字 # 左側白板 img_board_left = Image.new('RGB', (left_board_width,all_imgs_concat[class_name].shape[0]), (255, 255, 255))#選擇文字字體和大小 setFont = ImageFont.truetype(r'C:\Windows\Fonts\times.ttf', 100) #設置文字顏色 fillColor = "#000000" # 黑色# 添加左側文字 img_words_left = ImageDraw.Draw(img_board_left) img_words_left.text((1,fig_height//2-60),"FFJORD",font=setFont,fill=fillColor,direction=None) img_words_left.text((1,fig_height//2+ fig_height+interval-40),"TO-FLOW",font=setFont,fill=fillColor,direction=None)############################## 添加上側白板和文字 # 上側白板 img_board_up = Image.new('RGB', (left_board_width+all_imgs_concat[class_name].shape[1], up_board_height), (255, 255, 255))# 添加上側文字 img_words_up = ImageDraw.Draw(img_board_up) for i, stage in enumerate(stage_list.keys()):img_words_up.text(((fig_width+interval)*i+left_board_width+fig_width//2-100, 35),stage,font=setFont,fill=fillColor,direction=None)# In[] 合成最終圖像 for k, class_name in enumerate(img_name_list.keys()):# OpenCV 2 PILimg_pil = Image.fromarray(cv2.cvtColor(all_imgs_concat[class_name],cv2.COLOR_BGR2RGB))# 合并圖像# 將上側文字和圖片合并img_left_up_font = Image.new('RGB', (img_board_up.size[0], up_board_height+img_board_left.size[1]), (255,255,255)) img_left_up_font.paste(img_board_up, (0, 0))img_left_up_font.paste(img_board_left, (0, up_board_height)) img_left_up_font.paste(img_pil, (left_board_width,up_board_height))img_left_up_font.save(output_dir + "/"+class_name+".pdf", save_all = True, quality=100)使用matplotlib模塊實現大小不一圖像拼接與文字添加
以下實例中每一行由左側文字作為一張圖片,右側九張圖片作為另一張圖,顯然兩者是大小不一的圖片,此時需要用到plt.subplot2grid函數.
# -*- coding: utf-8 -*- """ Created on Sat Sep 17 17:40:28 2022@author: PC """import os import cv2 import numpy as np import re from PIL import Image, ImageDraw, ImageFont import matplotlib.pyplot as plt import matplotlib from pathlib import Pathinterval = 0 left_board_width = 68 font_size = 14matplotlib.rcParams["font.size"] =font_size plt.rcParams['pcolor.shading'] = 'nearest' # 'auto', 'nearest' or 'gouraud' plt.rcParams['savefig.dpi'] = 1000 #圖片像素 plt.rcParams['figure.dpi'] = 1000 #分辨率 plt.rcParams['figure.autolayout']=False plt.close('all')def get_white_board(interval, width):# 豎直間隔白板r = np.ones((interval,width),dtype=np.uint8) * 255g = np.ones((interval,width),dtype=np.uint8) * 255b = np.ones((interval,width),dtype=np.uint8) * 255# 合并通道,形成圖片white_board = cv2.merge([b, g, r])return white_boarddef makedirs(path):if not os.path.exists(os.path.dirname(path)):os.makedirs(os.path.dirname(path))def axis_setting(ax, npts=None):ax.set_aspect('equal')if npts is not None:ax.set_xlim(1, npts)ax.set_ylim(1, npts)# cmap = matplotlib.cm.get_cmap(None) # set mesh color mode# ax.set_facecolor(cmap(0.))ax.invert_yaxis() # invert yaxisax.get_xaxis().set_ticks([]) # ticks offax.get_yaxis().set_ticks([])ax.spines['top'].set_visible(False) # box offax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)# In[] path = r'...' all_images = [] labels = [] for root, dirs, files in os.walk(path+'\semantic'):# 讀取每一個圖片并放入一個列表for k, file in enumerate(files):labels.append(re.findall(r'_([a-zA-Z\_]+)_', file)[0].replace('_',' '))image = cv2.imread(os.path.join(root,file))image = image[1:67,:,:]if k < len(files)-1: # image = image[:66,:,:]if interval!=0:all_images.append(get_white_board(interval, all_images[k].shape[1]))all_images.append(image)# 豎直拼接 image_concat = cv2.vconcat(all_images)# In[] plt.clf() fig = plt.figure(figsize=(20, 8)) fig.subplots_adjust(wspace=0.0,hspace=0.0) for k,image in enumerate(all_images):# 添加文字# subplot2grid的第一個參數表示grid為4x10的網格,第二個參數表示當前這張圖的左上角所在的坐標,第三個參數表示該圖占的列的個數,第四個表示占的行的個數ax = plt.subplot2grid((4,10),(k,0),colspan=1,rowspan=1)# ax = fig.add_subplot(4, 2, 2*k+1)axis_setting(ax)whiteboard = np.full((left_board_width, left_board_width, 3), (255,255,255), np.uint8)ax.imshow(whiteboard)ax.text(left_board_width/2, left_board_width/2, labels[k],verticalalignment='center',horizontalalignment='center',font=Path(r'C:\Windows\Fonts\times.ttf')) # 'Times New Roman.ttf'# 添加圖片ax = plt.subplot2grid((4,10),(k,1),colspan=9,rowspan=1)axis_setting(ax)ax.imshow(Image.fromarray(cv2.cvtColor(all_images[k],cv2.COLOR_BGR2RGB)))path_save = os.path.join(path, 'semantic_manipulation.pdf')makedirs(path_save)plt.savefig(path_save, bbox_inches='tight',pad_inches=0)part3:日志讀取與繪圖
# -*- coding: utf-8 -*-import re import matplotlib.pyplot as plt import osplt.rcParams['figure.figsize'] = (6.0, 4.02)output_file = './results' plt.rcParams['savefig.dpi'] = 600 #圖片像素 plt.rcParams['figure.dpi'] = 600 #分辨率fontsize=12# In[] def catch_data(file):# file = "cifar-regular-0"# 讀取并整合數據with open(file, "rb") as f:data = f.readlines()data = [str(i) for i in data]data_str = "".join(data)# 正則匹配pattern_Iter = r'Iter (\d+)'Iter = re.findall(pattern_Iter, data_str)Iter = [int(i) for i in Iter]pattern_bits = r'Bit/dim ([^(a-z]+)\('bits = re.findall(pattern_bits, data_str)bits = [float(i) for i in bits]pattern_NFE = r'NFE [^(a-z]+\((.{6,10})\) \|'NFEs = re.findall(pattern_NFE, data_str)NFEs = [float(i) for i in NFEs]pattern_bits_epoch = r' Bit\/dim ([^:f]{6,10}), 'bits_epoch = re.findall(pattern_bits_epoch, data_str)bits_epoch = [float(i) for i in bits_epoch]pattern_Epoch = r'Epoch (\d+)'Epoch = re.findall(pattern_Epoch, data_str)Epoch = [int(i) for i in Epoch]return Iter, bits, NFEs, bits_epoch, Epoch# In[] # 讀取文件夾中的每一個文件 path = r'D:\桌面文檔\桌面\regularization\regularization' all_files = os.listdir(path) all_data = {'cifar':{}, 'fashion':{}, 'mnist':{}} dataset_label = {'cifar':'CIFAR-10', 'fashion':'FASHION-MNIST','mnist':'MNIST'} for file in all_files:file_name_split = file.split('-')Iter, bits, NFEs, bits_epoch, Epoch = catch_data(os.path.join(path,file))all_data[file_name_split[0]][file_name_split[2]] = {'Iter':Iter, 'bits':bits_epoch, 'NFEs':NFEs, 'Epoch':Epoch}# In[] for var_name in ['NFEs' ,'bits']:for i, dataset in enumerate(all_data.keys()):if not os.path.exists(output_file):os.makedirs(output_file)save_path = os.path.join(output_file,dataset+'-'+var_name+'.pdf')plt.figure()if var_name == 'NFEs':for j, regular in enumerate(all_data[dataset].keys()):data_j = all_data[dataset][regular]plt.plot(data_j['Iter'], data_j[var_name], label=r'$\alpha$'+'='+regular)plt.xlabel('Iterations',fontsize=fontsize)plt.ylabel('Average NFE',fontsize=fontsize)else:for j, regular in enumerate(all_data[dataset].keys()):data_j = all_data[dataset][regular]plt.plot(data_j['Epoch'][8:len(data_j['Epoch']):17], data_j[var_name][8:len(data_j['Epoch']):17], '-|',label=r'$\alpha$'+'='+regular)plt.xlabel('Epochs',fontsize=fontsize)plt.ylabel('Bits/dim',fontsize=fontsize)plt.title(dataset_label[dataset])plt.grid(True, linestyle="--", alpha=0.5) # 網格plt.legend(fontsize=fontsize)plt.savefig(save_path, bbox_inches='tight',pad_inches=0)plt.close()part4: 彩色示意圖
# -*- coding: utf-8 -*- """ Created on Sat Sep 3 10:33:49 2022@author: PC """import torch import os import matplotlib import matplotlib.pyplot as plt import numpy as npplt.rcParams['pcolor.shading'] = 'nearest' # 'auto', 'nearest' or 'gouraud' plt.rcParams['savefig.dpi'] = 600 #圖片像素 plt.rcParams['figure.dpi'] = 600 #分辨率 plt.rcParams['figure.autolayout']=Falsenumbers = 9 middle_status_i = torch.rand((4*numbers, 1, 32, 32)) batch_index = 1 i = 1 seed = 0 current_epoch = 1 def makedirs(path):if not os.path.exists(os.path.dirname(path)):os.makedirs(os.path.dirname(path)) path_batch_result = "D:\\desktop\\results" makedirs(path_batch_result)##### plot figure of results plt.clf() fig = plt.figure(figsize=(17.5, 8)) fig.subplots_adjust(wspace=0.02,hspace=0.02) # plt.tight_layout(h_pad=0.02,w_pad=0.001)npts = middle_status_i.shape[2] side = np.linspace(1, npts, npts) xx, yy = np.meshgrid(side, side) xx = torch.from_numpy(xx).type(torch.float32) yy = torch.from_numpy(yy).type(torch.float32)for k in range(middle_status_i.shape[0]):ax = fig.add_subplot(4, numbers, k+1)# ax = plt.subplot(4, numbers, k+1)ax.set_aspect('equal')zz = middle_status_i[k,:,:,:].squeeze()plt.pcolormesh(xx, yy, zz)ax.set_xlim(1, npts)ax.set_ylim(1, npts)cmap = matplotlib.cm.get_cmap(None)ax.set_facecolor(cmap(0.))# ax.invert_yaxis()ax.get_xaxis().set_ticks([]) # ticks offax.get_yaxis().set_ticks([])ax.spines['top'].set_visible(False) # box offax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)# ax.set_title("{}".format(k), fontsize=10)path_middle_status_i = os.path.join(path_batch_result, f"middle_status_scale{i}_ep{current_epoch}_s{seed}.jpg") makedirs(path_middle_status_i) plt.savefig(path_middle_status_i, bbox_inches='tight',pad_inches=0)Part5: 無邊框圖像
def axis_setting(ax, npts=None):ax.set_aspect('equal')if npts is not None:ax.set_xlim(1, npts)ax.set_ylim(1, npts)cmap = matplotlib.cm.get_cmap(None) # set mesh color modeax.set_facecolor(cmap(0.))ax.invert_yaxis() # invert yaxisax.get_xaxis().set_ticks([]) # ticks offax.get_yaxis().set_ticks([])ax.spines['top'].set_visible(False) # box offax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)plt.clf() fig = plt.figure(figsize=(22, 8)) fig.subplots_adjust(wspace=0.02,hspace=0.02) ax = fig.add_subplot(4, channel_nums+1, 1) axis_setting(ax)總結
以上是生活随笔為你收集整理的论文绘图与合成图片过程中常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataHunter完成千万级A轮融资
- 下一篇: webserver的使用