When creating a JPEG, how can I minimize the occurence of artifacts? There are 2 main factors that seem to lead to digital artifacts when creating JPEG images: Aliasing and Compression.

Example:


  Converting a PNG with characters on it to a JPEG or placing vector images over a photograph, will result in pixelization on their edges. The antialiasing generally creates a kind of blur around them, but if the image is lossy compressed, part of the details are also lost, therefore the blur and the pixelization may become less noticeable.


Is this the right thing to do? I.e. since a lossless compression generates a highly detailed image, the artifacts resulting from aliasing will be more noticeable, so a balance may be found by using the right compression, although compromising the image quality.

Edit

I just saved this JPEG in mspaint (3.46KB):



Here's the same JPEG with maximum compression (lowest quality, 0.5KB):



Here's yet the same JPEG with 50% compression (notice the difference in size, 1.29KB):



The same 50% compression but saved as "progressive JPG", kept the original EXIF and XMP data, and "tried to save with original JPG quality" (you can notice that there are no grey pixels around, 2.96KB):



And finally the same as before with chroma subsampling disabled (same file size, 2.96KB):
JPEG compression involves three main steps:


Chroma subsampling. The image is converted from RGB into YCbCr color space, in which the luma or brightness (Y) is stored separately from the chroma or color components, Cb and Cr.  The Y component is kept at full resolution, but Cb and Cr are downsampled, typically to half resolution on each axis.  This exploits the fact that the human visual system is more sensitive to fine details of brightness than of color.
Frequency quantization. The Y, Cb, and Cr images are converted into a frequency representation, by breaking them up into 8x8 blocks and applying the discrete cosine transform (a variant of the Fourier transform) to each block.  The result is a matrix of numbers that describe the amplitudes of different spatial frequencies in the block.  These numbers can then be quantized (rounded off to a chosen number of bits of precision). Different levels of quantization are used for different frequencies, exploiting our visual system's relatively lower sensitivity to high frequencies.  This is where the JPEG encoder's quality setting comes into play: lower qualities use coarser quantization.
Entropy coding. The  quantized DCT values are passed through an entropy coder, which losslessly compresses the bit stream by using fewer bits to represent the more common values, sort of like a zip file.


Steps 1 and 2 are the lossy ones, and each produces its own type of artifacts (though there is some overlap).  Chroma subsampling tends to blur sharp edges between regions of different colors.  This is particularly visible in vector art, where brightly colored shapes pick up vicious jaggies around their edges. Frequency quantization blurs fine details in general, and also creates block-shaped artifacts at low quality settings, because the DCT is done on a block-by-block basis.  It is particularly visible on text.

This is why JPEG usually isn't used for images containing vector graphics or text&mdash;its compression algorithms are poorly suited for those cases, although they work well for photographs and other images with complex texture and not-too-sharp edges.