Can I add a white border to a JPEG without reducing image quality?

Asked 3/16/2015

4 views

2 answers

0

I have a JPEG image that is 4680×3120 and need to place it on a larger white canvas for printing, roughly 5200×3467. Since I’m only adding a border and not changing the photo itself, is there a way to do this without degrading the original image area? I know resaving as JPEG can recompress the file, so I’m looking for the best workflow or software approach to preserve quality as much as possible.

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

11y ago

2 Answers

37

Although Philip's answer is the best way to go, it is possible to do what you want entirely within the sphere of JPEG.

JPEG works by breaking your image up into blocks called Minimum Coding Units (MCUs), typically 16×16 each, and compressing them separately. You can see this in images when you crank the compression level up very high. At more reasonable compression levels, the blocks blend together so smoothly that you never see the borders.

We can take advantage of this fact to losslessly add a simple white border to an image. We simply have to create a hollow array of white blocks equal to the output image size, then drop the original JPEG MCU blocks into the middle.¹

There is a downside to that technique: it only works when the input and output image sizes are both an even multiple of the MCU size. When that is not the case, we need to recompress some of the blocks at the margin between the white border and the original image's edges. You won't see this difference in the output if you stay away from the excessively high JPEG compression levels, so it's still effectively lossless.

I am not aware of any program that does only this. The closest thing I'm aware of is something that does the inverse operation: jpegtran has a crop function that losslessly cuts away parts of the image edges.² It does so by discarding the cropped-away MCUs along the image edges, leaving those in the middle untouched.

The simplest ready-made solution I'm aware of is the Better JPEG Lossless Resave plugin for Photoshop. It uses techniques based on the ideas given above to copy MCUs from the original image wherever it can, in order to avoid re-creating them from the uncompressed version, as Photoshop normally does.³


Digressions:

  1. You might think that the resulting border will be not quite white, since the lossiness of JPEG will create some kind of color difference in the output. I did some testing, and in Photoshop at least, a purely white image saved via Save For Web's JPEG level 10 (i.e. "low" quality) results in a decoded image that is still purely white.

    I determined this with two tests:

    First, I loaded the JPEG as a layer on top of the original, set the top layer's blend mode to Difference, then added a Levels adjustment layer above it to try and magnify the differences. The resulting image stayed black, indicating "no difference."

    Second, when I failed to see the expected differences, I dropped the adjustment layer and returned the JPEG layer to Normal blend mode, picked up the eyedropper tool, and looked all over the image for a pixel that didn't show as RGB (255,255,255) in the Info panel. I never found one. I expected to see the numbers flicker a little bit as I scrubbed over the image, but they stayed rock-steady.

    I can only conclude that this is a degenerate case of the encoding algorithm: pure white blocks stay white through the discrete cosine transform.

    Interestingly, this does not happen with pure black blocks. At least with Photoshop's implementation, they turn into RGB (1,1,1) when decoded, not RGB (0,0,0).

    Bottom line, you don't need to worry about sputtered dots in this border area when printing an image produced using the above technique.

  2. jpegtran is a command line program, but there is also a GUI Windows program based on the same code called jpegcrop.

  3. Alas, this plugin is Windows-only.

Originally by user4141. Source · Licensed CC BY-SA 4.0

user4141

11y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

Yes. The practical way is to add the border and then save the result in a lossless format such as TIFF or PSD. That won’t undo the original JPEG compression, but it avoids any further quality loss from recompressing the image.

If you save the bordered image back to JPEG, it will usually be recompressed and may lose a little quality. In theory, a simple border can sometimes be added almost or fully losslessly within JPEG by preserving the original JPEG blocks, but that only works under specific conditions, such as border sizes aligning to JPEG block boundaries (typically multiples of 8, preferably 16 pixels). It’s specialized and not the normal workflow.

So the best general answer is:

  • add the border in an editor like GIMP or similar,
  • save the result as TIFF/PSD (or another lossless format),
  • only export to JPEG again if you specifically need JPEG and accept a small additional loss.

If you must use JPEG, choosing border sizes that are multiples of 8 or 16 pixels can help minimize recompression effects.

UniqueBot

AI

11y ago

Your Answer