Python绘制并保存指定大小图像的方法

绘制直线,三角形,正方形

import matplotlib.pyplot as plt
def plotLine():
 x = [1,2,3,4,5]
 y = [3,3,3,3,3]
 plt.figure(figsize=(100,100),dpi=1)
 plt.plot(x,y,linewidth=150)
 plt.axis('off')
 plt.savefig('C:\\Users\\Administrator\\Desktop\\分形图\\a.jpg',dpi=1)
 plt.show()
 plt.close()
def plotTriangle():
 x = [1,3,1,1]
 y = [1,1,3,1]
 plt.figure(figsize=(100,100),dpi=1)
 plt.plot(x,y,linewidth=150)
 plt.axis('off')
 plt.savefig('C:\\Users\\Administrator\\Desktop\\分形图\\b.jpg',dpi=1)
 plt.show()
 plt.close()
def plotSquare():
 x = [1,3,3,1,1]
 y = [1,1,3,3,1]
 plt.figure(figsize=(100,100),dpi=1)
 plt.plot(x,y,linewidth=150)
 plt.axis('off')
 plt.savefig('C:\\Users\\Administrator\\Desktop\\分形图\\c.jpg',dpi=1)
 plt.show()
 plt.close()
plotLine()
plotTriangle()
plotSquare()
from datetime import datetime 
import os 
import matplotlib.pyplot as plt 
import numpy as np 
import tensorflow as tf 
from six.moves import xrange 
data = np.load('data/final37.npy') 
data_images = data
data_images = data_images.reshape(-1,3,61)
# data_images = data_images[500:1000,:,:]
for i in range(2000):
 plt.figure(figsize=(100,100),dpi=1)
 plt.plot(data_images[i][0][0:30],data_images[i][0][30:60],color='blue',linewidth=150)
 plt.plot(data_images[i][1][0:30],data_images[i][1][30:60],color='red',linewidth=150)
 plt.plot(data_images[i][2][0:30],data_images[i][2][30:60],color='green',linewidth=150)
 plt.axis('off')
 plt.savefig('C:\\Users\\Administrator\\Desktop\\调整分辨率\\原始图\\resouce%d.jpg' %(i),dpi=1)
 plt.close()
#################################################################################
# 生成随机分叉图
# import random
# import numpy as np
# import operator
# import os
# import copy
# from matplotlib.font_manager import FontProperties
# from scipy.interpolate import lagrange
# import random
# import matplotlib.pyplot as plt
# np.set_printoptions(threshold=np.inf) #输出全部矩阵不带省略号
# # random.seed(10)
# finaldata = []
# for iy in range(100):
#  #固定一个点,尽量使点固定在0-1正方形的中间 #小数点后16位
#  pointx = random.uniform(0.3,0.7)
#  pointy = random.uniform(0.3,0.7)
# #################################################
#  #主分支在上方
#  a1x = random.uniform(pointx,0.8)#使第二个点尽量不那么大
#  a2x = random.uniform(a1x,1)
#  a3x = random.uniform(a2x,1)
#  a1y = random.uniform(pointy,0.8)
#  a2y = random.uniform(a1y,1)
#  a3y = random.uniform(a2y,1)
#  ax = [pointx,a1x,a2x,a3x]
#  ay = [pointy,a1y,a2y,a3y]
# #  print(ax)
# #  print(ay)
#  #对主分支a段进行插值
#  #在ax相同索引直接分别插两个点,最后a段长度由4变成10,既得final_ax
# #  print(ay)
#  final_ax = []
#  final_ay = []
#  for i in range(len(ax)-1):
#   #round(data,8)小数点保留8位四舍五入
#   f = lagrange([round(ax[i],8),round(ax[i+1],8)],[round(ay[i],8),round(ay[i+1],8)])
#   insertax = np.linspace(ax[i],ax[i+1],4)#插入2个点,小数点后8位
#   insertay = f(insertax)
#   for axi in insertax:
#    final_ax.append(axi)
#   for ayi in insertay:
#    final_ay.append(ayi)
#  del final_ax[4]
#  del final_ax[7]
#  del final_ay[4]
#  del final_ay[7]
#  #################################################
# #  #左下分支
#  b1x = random.uniform(0.2,pointx)#使第二个点尽量不那么小
#  b2x = random.uniform(0,b1x)
#  b3x = random.uniform(0,b2x)
#  b1y = random.uniform(0.2,pointy)
#  b2y = random.uniform(0,b1y)
#  b3y = random.uniform(0,b2y)
#  bx = [b3x,b2x,b1x,pointx]
#  by = [b3y,b2y,b1y,pointy]
#  #对左下分支b段进行插值
#  final_bx = []
#  final_by = []
#  for i in range(len(bx)-1):
#   f = lagrange([round(bx[i],8),round(bx[i+1],8)],[round(by[i],8),round(by[i+1],8)])
#   insertbx = np.linspace(bx[i],bx[i+1],4)
#   insertby = f(insertbx)
#   for bxi in insertbx:
#    final_bx.append(bxi)
#   for byi in insertby:
#    final_by.append(byi)
#  del final_bx[4]
#  del final_bx[7]
#  del final_by[4]
#  del final_by[7]
#   
#  ##################################################
#  #右下分支
#  c1x = random.uniform(pointx,0.8)#使第二个点尽量不那么大
#  c2x = random.uniform(c1x,1)
#  c3x = random.uniform(c2x,1)
#  c1y = random.uniform(0.2,pointy)
#  c2y = random.uniform(0,c1y)
#  c3y = random.uniform(0,c2y)
#  cx = [pointx,c1x,c2x,c3x]
#  cy = [pointy,c1y,c2y,c3y]
#  #对右下分支段进行插值
#  final_cx = []
#  final_cy = []
#  for i in range(len(cx)-1):
#   f = lagrange([round(cx[i],8),round(cx[i+1],8)],[round(cy[i],8),round(cy[i+1],8)])
#   insertcx = np.linspace(cx[i],cx[i+1],4)
#   insertcy = f(insertcx)
#   for cxi in insertcx:
#    final_cx.append(cxi)
#   for cyi in insertcy:
#    final_cy.append(cyi)
#  del final_cx[4]
#  del final_cx[7]
#  del final_cy[4]
#  del final_cy[7]
# ####################################################
#  x = [final_ax,final_bx,final_cx]#三分叉,上为a,左下b,右下c
#  y = [final_ay,final_by,final_cy]
#  diameter_a = round(random.uniform(0.2,0.25),8)
#  diameter_b = round(random.uniform(0.1,0.2),8)
#  diameter_c = round(random.uniform(0.1,0.2),8)
#  final_a = []#长度为21前10个x坐标点,后面10个是y坐标点,最后一个是管径
#  for ax in final_ax:
#   final_a.append(ax)
#  for ay in final_ay:
#   final_a.append(ay)
#  final_a.append(diameter_a)
#  final_b = []
#  for bx in final_bx:
#   final_b.append(bx)
#  for by in final_by:
#   final_b.append(by)
#  final_b.append(diameter_b)
#  final_c = []
#  for cx in final_cx:
#   final_c.append(cx)
#  for cy in final_cy:
#   final_c.append(cy)
#  final_c.append(diameter_c)
#  finalabc = [final_a,final_b,final_c]
#  finaldata.append(finalabc)
# finaldata = np.array(finaldata)
# #复制改变a,不改变b
# finaldata1 = finaldata.copy()
# finaldata2 = finaldata.copy()
# finaldata3 = finaldata.copy()
# #以定点为中心,进行镜像处理
# finaldata1[:,:,0:10] = 2 * pointx -finaldata[:,:,0:10]
# finaldata2[:,:,10:20] = 2 * pointx -finaldata[:,:,10:20]
# finaldata3[:,:,0:20] = 2 * pointx -finaldata[:,:,0:20]
# final = np.concatenate((finaldata,finaldata1,finaldata2,finaldata3),axis=0)
# np.random.shuffle(final)#随机打乱数据,若没有次句,将连续输出一个方向
# print(final.shape)
# # np.save('C:\\Users\\Administrator\\Desktop\\第9周\\80000.npy',final)
# ###########################################
# # 单个可视化图像
# for i in range(len(final)):
#  abc = final[i]
#  plt.plot(abc[0][0:10],abc[0][10:20],color='blue',linewidth=1.5)
#  plt.plot(abc[1][0:10],abc[1][10:20],color='red',linewidth=1.5)
#  plt.plot(abc[2][0:10],abc[2][10:20],color='green',linewidth=1.5)
#  plt.axis('off')
#  plt.savefig('C:\\Users\\Administrator\\Desktop\\ttt\\原图2\\random%d.jpg' %i,dpi=100)
#  plt.close()
###########################################
# 分块可视化图像
# data = np.load('C:\\Users\\Administrator\\Desktop\\第8周\\10000.npy')
# print(data.shape)
# rows,cols = 5,5
# fig,axs = plt.subplots(rows,cols)
# cnt = 0
# for i in range(rows):
#  for j in range(cols):
#   xy = final[cnt]#第n个分叉图,有三个分支,每个分支21个数
#   for k in range(len(xy)):
#    x = xy[k][0:10]
#    y = xy[k][10:20]
#    if k == 0 :
#     axs[i,j].plot(x,y,color='blue',linewidth=xy[k][20]*15)
#    if k == 1:
#     axs[i,j].plot(x,y,color='red',linewidth=xy[k][20]*15)
#    if k == 2:
#     axs[i,j].plot(x,y,color='green',linewidth=xy[k][20]*15)
#    axs[i,j].axis('off')
#   cnt +=1
# # plt.savefig('C:\\Users\\Administrator\\Desktop\\第9周\\')
# plt.show()

以上这篇Python绘制并保存指定大小图像的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。