aman2357
aman2357

Reputation: 29

Different sizes of JPEG image in R

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

Answers (2)

user3344003
user3344003

Reputation: 21627

There are a number of factors that affect the compression:

  1. Sampling Rates
  2. Quantization table selection
  3. Huffman tables (optimal or canned)
  4. Progressive or Sequential If progressive, the breakdown of the scans.
  5. Metadata included

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

cyberj0g
cyberj0g

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

Related Questions