Reputation: 29
I am reading a jpeg image (input.jpg) and on writing the same image on disk(as output.jpg), why does its size getting changed. Can i retain the same size.
library(jpeg)
img <- readJPEG('input.jpg')
file.info('input.jpg')$size
writeJPEG(img,'output.jpg',1)
file.info('output.jpg')$size
#is different from input.jpg size
Upvotes: 1
Views: 462
Reputation: 21627
There are a number of factors that affect the compression:
If you do not compress using the same settings as the input, you will get a different size.
You can also get rounding errors in the decompression process that could cause slight differences.
Upvotes: 0
Reputation: 3787
Well, what's you doing is not reading and writing back the same file. readJPEG
decodes compressed (lossy) JPEG data into raster array, writeJPEG
encodes it back again. To get approximately the same size, you should (at least) set quality
parameter to appropriate value. See ?writeJPEG
:
writeJPEG(image, target = raw(), quality = 0.7, bg = "white", color.space)
Upvotes: 1