The traditional dictionary learning algorithms have slow convergence rate when learning the training image. And the effect of dictionary learning becomes worse if the images are corrupted by noise. Therefore, a dictionary learning algorithm based on variational inference is proposed to solve this problem. The algorithm firstly sets the conjugate sparse prior distribution of the parameters in the model, and then the joint probability density function of all parameters is calculated based on the Bayesian network. Finally, the optimal edge distribution of the parameters is calculated by the variational Bayesian inference, and the adaptive dictionary training is completed. The image denoising experiment and the compressed sensing image reconstruction experiment are carried out by the adaptive dictionary. The simulation results show that the algorithm can significantly increase the efficiency of dictionary learning, and the visual effect of the denoising and the reconstruction of the test images are improved.