python 图片外围置零 np.pad操作

python 图片外围置零 np.pad操作

path1 = 'I:\\xj\\pillar_train_val_test\\val\\labels_half_new'
path2 = 'I:\\xj\\pillar_train_val_test\\val\\labels_half_new_draw'

for filename in os.listdir(path1):
    img = cv2.imread(os.path.join(path1, filename), 0)
    shape = img.shape
    img_new = img[2:shape[0]-2, 2:shape[1]-2]
    img_new = np.pad(img_new, ((2,2), (2,2)), 'constant',constant_values = (0,0))
    cv2.imwrite(os.path.join(path2, filename), img_new)
    print (filename)
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from matplotlib.colors import LinearSegmentedColormap # ==================== 数据精确配 ==================== # 构建2012-2016年平均q值矩阵(根据图片表格最后一行"平均值"列) q_matrix = np.array([ [np.nan, 0.147, 0.156], # 温度∩降水,温度∩NDVI [0.147, np.nan, 0.046], # 降水∩温度,降水∩NDVI [0.156, 0.046, np.nan] # NDVI∩温度,NDVI∩降水 ]) # 定义交互类型矩阵(根据地理探测器规则判断) interaction_type = np.array([ [np.nan, "双因子↑", "非线性↑↑"], ["双因子↑", np.nan, "单因子主导"], ["非线性↑↑", "单因子主导", np.nan] ]) labels = ['温度', '降水', 'NDVI'] # ==================== 可视化精密调控 ==================== # 创建阶梯式橙色渐变(匹配图片色阶) color_stops = ["#FFF5EB", "#FEE6CE", "#FDD0A2", "#FDAE6B", "#FD8D3C", "#F16913", "#D94801", "#A63603"] cmap = LinearSegmentedColormap.from_list("OrangeSteps", color_stops, N=8) plt.figure(figsize=(5.5, 5.5), dpi=150) sns.set_style("white") plt.rcParams.update({ 'font.sans-serif': 'SimHei', 'axes.unicode_minus': False, 'xtick.bottom': False, 'ytick.left': False }) # ==================== 矩阵遮罩生成 ==================== mask = np.zeros_like(q_matrix, dtype=bool) np.fill_diagonal(mask, True) # ==================== 热力图参数精调 ==================== heatmap = sns.heatmap( pd.DataFrame(q_matrix, index=labels, columns=labels), cmap=cmap, annot=False, mask=mask, vmin=0.04, # 精确匹配图片数据范围 vmax=0.16, # 图片最大可见值为0.156 linewidths=0.5, linecolor='#999999', cbar_kws={ 'label': 'q值', 'shrink': 0.75, 'aspect': 7, 'pad': 0.05, 'ticks': [0.04, 0.08, 0.12, 0.16] }, square=True ) # ==================== 分层标注系统 ==================== for i in range(3): for j in range(3): if not np.isnan(q_matrix[i, j]): # 主数值标注(中心位) plt.text(j + 0.5, i + 0.5, f'{q_matrix[i, j]:.3f}'.lstrip('0'), ha='center', va='center', color='white', fontsize=10, weight='bold') # 交互类型标注(右上定位) plt.text(j + 0.78, i + 0.22, interaction_type[i, j], ha='right', va='top', color='#333333', fontsize=8.5, weight='bold', rotation=35) # ==================== 图形元素精修 ==================== # 坐标轴标签设 heatmap.xaxis.tick_top() plt.xticks(rotation=45, ha='left', fontsize=9) plt.yticks(rotation=0, va='center', fontsize=9) # 标题设(精确匹配图片文字) plt.title('黑河流域地表蒸散发驱动因素交互作用强度\n(2012-2016平均值)', pad=12, fontsize=11, linespacing=1.5, loc='left', x=0.05) # 颜色条标签美化 cbar = heatmap.collections[0].colorbar cbar.set_ticklabels(['0.04', '0.08', '0.12', '0.16']) cbar.ax.tick_params(labelsize=8) cbar.outline.set_edgecolor('#666666') # 画布精细调整 plt.tight_layout(pad=0.5) plt.subplots_adjust(left=0.15, right=0.85, top=0.85, bottom=0.15) plt.show()修改整段代码,,美化出图
最新发布
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值