# 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):

• Just curious, what is your reason for wanting jpg, is it the smaller file size? In case it helps your specific situation, do you know about dxt compression or distance field textures? Aug 5, 2015 at 3:20
• It's unclear to me what your question is. Do you want to know whether it's good to compress using JPEG? Do you want to know what kinds of images compress well with JPEG? Or, are you already using JPEG and you'd like to know how to author your images in order to minimize the artifacts caused by JPEG? Aug 5, 2015 at 7:31
• @AlanWolfe I have encountered a few occasions that I was only able to use JPEG (mostly in web apps), and that's why I needed it to be in JPEG. Thanks, but I was not familiar with dxt compression nor distance field textures, from what I have seen in wikipedia, dxt algorithms are different from the ones used in JPEG, do you mean they can be used to create JPEGs? Aug 5, 2015 at 10:23
• @Moshoka thanks, it's more related to your last question: how to minimize the "visual impact" brought by artifacts in JPEG images? Aug 5, 2015 at 10:24
• keep in mind that mozjpeg compressor has a special trick to produce less distortion on these kinds of images. (and it's more efficient in general than usual jpeg writers too.) did you try it? Jan 14, 2017 at 13:43

JPEG compression involves three main steps:

1. 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.

2. 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.

3. 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—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.

• Thank you very much for your detailed explanation Nathan +1. I understand the compression algorithms have their limitations for JPEG, but I just wanted to find out if there's a right balance of the amount of compression together with other options (when saving it) that can make the artifacts less noticeable. I edited my question with samples. Aug 5, 2015 at 10:51