Compose multiple JPEG files without re-compression
How can I compose (adjoin) multiple JPEG files without re-compre开发者_C百科ssion?
I know there is jpegtran that can losslessly crop and resize JPEG images, so I wonder if there is similar tool to adjoin images lossless?
Their size is a multiple 1 MCU block (16 pixels in both directions).
Newer versions of jpegtran include lossless "crop 'n' drop" (or "cut & paste") features. Via the command line, you can use -crop
to create a larger image based on one of the pieces, and -drop
to paste subsequent images into the combined image. Unfortunately, multiple -drop
options aren't currently supported, so jpegtran needs to be invoked once for every part.
jpegtran -outfile combined.jpg -copy all -crop ${totalWidth}x${totalHeight} part-0.jpg
jpegtran -outfile combined-tmp.jpg -copy all -drop +${xOff[1]}+${yOff[1]} part-1.jpg combined.jpg && mv combined-tmp.jpg combined.jpg
...
jpegtran -outfile combined-tmp.jpg -copy all -drop +${xOff[$n]}+${yOff[$n]} part-$n.jpg combined.jpg && mv combined-tmp.jpg combined.jpg
Your mileage may vary depending on the exact properties of the image parts.
What you want to do is certainly possible, but in order for it to be truly lossless, both images must use the same quantization tables and color subsampling option. If, for example, the 2 images come from the same source (e.g. camera), then you can be pretty sure that it would be possible. You would need to entropy-decode each MCU in both images, rearrange them the way you want and then re-entropy code them and output the JPEG bitstream. If you want to overlay one on the other or crop/merge them on non-MCU boundaries, then you hit the same problem as above and must do a full decode and re-encode.
I've used ImageMagick's composite binary to watermark a JPG with a PNG image.
Like this:
composite -tile -dissolve 15 targetimage.jpg watermark.png
You can obviously use two JPEGs to achieve what you're looking for.
Using the -quality
and -sampling-factor
arguments will help retain quality. There is also a -compress <type>
argument, where type can by Lossless
. However, ImageMagick does not recommend the use of this value as the JPEG library must be patched to support this.
It's worth investigating though if it will achieve a genuine lossless image, and you're not happy with the quality of the compress
and sampling-factor
arguments.
精彩评论