A compact and high-precision gradient waveform generator for magnetic resonance imaging (MRI) is proposed to meet the requirements of developing a home-built MRI spectrometer. The generator is implemented using a field programmable gate array (FPGA) device with relatively low on-chip computing resources. The outputs of this FPGA are sent to several digital-to-analog converters to generate analog gradient waveforms that are needed in MRI scanning. Through optimizing calculating architecture, this FPGA can implement real-time coordinate transformation and pre-emphasis of channels of X, Y, Z, and Bo (main magnetic field). This approach does not only simplify the design of the MRI spectrometer but also reduce the consumption of computing resources of the FPGA. Software simulations and actual test were conducted. The experimental results show that the FPGA can generate gradient waveforms with precision of 20 bits and time resolution of $1\ \mu\mathrm{s}$, which meets the requirements of MRI scanning.