The emergence of drug-resistant bacteria is becoming the focus of global public health. Early-stage pathogen bioimaging will offer a unique perspective to obtain infection information in patients. A photoacoustic (PA) contrast agent based on functional peptide modified gold nanoparticles (AuNPs@P1) is developed. These nanoparticles can be specifically tailored surface peptides by bacterial overexpressed enzyme inducing in situ aggregation of the gold nanoparticles. In the meantime, the close aggregation based on the hydrogen bonding, π-π stacking, and hydrophobic interaction of the peptide residues on the surface of gold nanoparticles exhibits a typical redshifted and broadened plasmon band. In addition, this active targeting and following in situ stimuli-induced aggregation contribute to increased nanoparticle accumulation in the infected site. Finally, the dynamic aggregation of AuNPs@P1 results in dramatically enhanced photoacoustic signals for bioimaging bacterial infection in vivo with high sensitivity and specificity. It is envisioned that this PA contrast agent may provide a new approach for early detection of bacterial infection in vivo.