How can I convert a JPEG image to a RAW image with a Linux command?

5,859

Solution 1

Original RAW images cannot be restored.

Selecting JPEG instead of RAW saves space by sacrificing quality.
This operation is irreversible and lossy.
Simply speaking this means you can't go back.

Sadly you may only select RAW format instead of JPEG next time before you shot.

Note: You could technically find a way to turn the current jpg image into RAW format, but it will not be the same RAW image you might have if you shot and saved directly in RAW format.


Searching in the Canon support you may read

the camera can capture images in JPEG and RAW format.

JPEG
Images Processed in the camera for optimal image quality and compressed to reduce file size. However, the compression process is irreversible, and images cannot be restored to their original, unprocessed state. Image processing may also cause some loss of image quality.

RAW "Raw" (unprocessed) data, recorded with essentially no loss of image quality from the camera's image processing. The data cannot be used in this state for viewing on a computer or printing. You must first process images on the camera or use the software (Digital Photo Professional) to convert images to JPEG or TIFF files. Images can be adjusted with minimal loss of image quality.

Solution 2

"Raw" is not a well defined format. Examining Wikipedia Raw image format, I counted 43 raw formats, each depending on the camera model.

One of the best image packages around is ImageMagick, available as a package on Linux or as a download, which supports a few RAW formats. Use its commands of magick or convert.

However, the quality of the result would be very poor. JPEG is processed image data while RAW is not. Typically, RAW-files have 12-14-bit per pixel which means up to 16384 values. JPEG can have only 256 luminance values per RGB channel. This means that JPEG contains much less data than a corresponding RAW-file. So there is no good way to convert a JPEG to RAW.

Solution 3

Difficulties and opportunity of trying to convert JPEG into RAW format.

It is not always possible to convert a JPEG file to the original RAW file.
Note: I speak about original or specific RAW format because it is not a standard: RAW formats are service formats, different one from another, used by the manufacturer to quickly save the contents of the CCD into the buffer and in case on the storage device.

Limits. You might be able to turn it into a valid RAW format, but that doesn't mean the individual values stored in the new RAW file are the same as you might have if you saved the shot directly in RAW format.

Opportunity. It is not said that even if you can convert a JPEG image to RAW format and start from it for processing, the final result will be better than if you start directly from the JPEG you already have. In fact you will apply a filter (algorithm) that transforms one format into another and that can introduce further alterations.

Difficulties. Nonetheless, once you know the specific RAW format you may find a way to formally convert the image into your RAW format. This will involve not trivial programming efforts, starting from the modeling of the inverse transformation and ending into coding in the preferred language the algorithm.

Existing Programs. The convert program from the Imagemagick package under Linux (or other OSes) cannot convert any formats directly into the Canon RAW format, it is only capable to read (R) them... So it can do the opposite of what the OP is searching for: to convert RAW to whatever.

convert -list format | grep Canon
      CR2  DNG       r--   Canon Digital Camera Raw Image Format
      CRW  DNG       r--   Canon Digital Camera Raw Image Format

But Imagemagick is even a scripting/programming language. Once that you have access to the single pixel values you may save them as you want... programming it. But you need to know the specifications of the desired format.

Moreover, since it is an open source program, you can see in the code the routines used to convert from RAW into whatever and see if you are able to invert them.

On the irreversibility of RAW to JPEG conversion.

This happens for many reasons. Among them:

  • Some formats are lossy, JPEG is usually one of them. You save space at the cost of artifacts and a general modification of the original values for individual pixels.

  • In the classic JPEG format there are only 8 bits for color (it means 256 shades). In a CCD sensor usually the couters are of 12 or 14 bits (that means 4096 and 16384 shades).
    If you think of your image as a histogram, you are going to average in a single bin (column) of the JPEG image, the values that are contained in 16 and 64 bins in the 12 and 14 bits counters respectively. How can you restore individual bin values from the average?

  • Even if your JPEG is in the recent 12-14-16 bit format (JPEG_XT), you are not aware of the algorithm used for this transformation inside the camera.
    (In simple terms, the sensor can have a non-linear efficiency curve. The manufacturer can take this into account by transforming the number from RAW format to a linear shade level in the JPEG, TIFF, ..., format.)

  • The geometry of the CCD is different from the geometry of an image. Roughly speaking, in the images we have a pixel with 3 or 4 values relative to the exact same geometric square. All squares are adjacent.

    In the CCD instead we have geometrically separated sensors sensitive to different wavelengths, again speaking roughly for the R, G, B values. These sensors may or may not have the same surface, and certainly have a different efficiency which, among other things, changes according to the number of counts per second.

    How these RGB sensor sequences are aligned and spaced from each other depends on the design of the CCD. You can see some examples of Color filter array on internet.

    enter image description here Image from Wikipedia

    enter image description here Image from Ephotozine

    How the values of the counts of the red sensors translate, for example, into an image value of a pixel R, depends on their geometric position and on the algorithm chosen which may or may not also take into account the values of nearby sensors, either of those sensitive to the same as to other wavelengths.

    All of these things are hardly reversible.

Solution 4

You can not do that.

You have to save as raw from the camera.

Once it is in JPEG format it would be like removing the cream from your coffee to try to make it a raw file.

Solution 5

As others pointed out, there's no way to get the usual RAW format benefits when converting from JPG.

However, with Canon Powershot it's possible to use a custom firmware from CHDK project that makes it possible to take RAW images. Of course this won't help with existing images.

Share:
5,859

Related videos on Youtube

lolotux
Author by

lolotux

Updated on September 18, 2022

Comments

  • lolotux
    lolotux over 1 year

    I have a series of JPEG images taken with Canon Powershot to JPEG. How can I convert/transform them to RAW images?

    Not with a graphical tool, but from the command line!

    NB : Using raw images to stack them in Siril astrophoto application (https://siril.org/fr/)

  • Tetsujin
    Tetsujin about 3 years
    Absolutely. You shouldn't even attempt this. You will not get what you think you might get. See photo.stackexchange.com/questions/73475/… for some more reasons & possibilities for conversion if you're not convinced yet. There is no way to get back the information lost by saving your originals as jpg. Just remember to switch your camera to RAW before using it again.
  • Hastur
    Hastur about 3 years
    @Steve Hello. If you talk about (scientfic) data, you have already compromised it. I agree with you that if the filter is well done you will not introduce further changes in the single pixel values by converting them from jpg to RAW. But if not, you will do it... again. And if you just need to work on actual jpg images, you don't need any other layers of processing, you can start right from the images you have.
  • gronostaj
    gronostaj about 3 years
    The bits-per-pixel numbers you've quoted are so misleading that it actually turns out JPEG has more bits per pixel than RAW (14 bits vs. 3x8 bits). Without explanation how RAW files work this part is very confusing.
  • harrymc
    harrymc about 3 years
    @gronostaj: I meant 12-14 bits per pixel color for RAW, compared to 8 for JPEG. There are formats which use even more bits.
  • gronostaj
    gronostaj about 3 years
    @harrymc That would be a bit better, but in RAWs a single pixel may be composed from multiple subpixels for each channel, so it's still not an accurate comparison.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    "Very poor" is a matter of opinion. It will be pretty much exactly as "poor" as the JPEG, that is, all but indistinguishable to the layman's eye from the original camera's raw image!
  • Charles Duffy
    Charles Duffy about 3 years
    @Peter-ReinstateMonica, true, but if someone is generating a raw image, they're very unlikely to be using it for display to laypersons.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    @Charles also true ;-). I meant that the typical jpeg is quite good, at least to good to characterize it as "very poor". It contains less information, of course, but it's not "bad".
  • Hastur
    Hastur about 3 years
    Hi, sorry to say but Imagemagick programs and libraries only support Reading the Canon CRW and CR2 formats... (Check it with the command line in this answer) So you can convert from CRW to JPEG but not the other way. At least not in a simple way.
  • RockPaperLz- Mask it or Casket
    RockPaperLz- Mask it or Casket about 3 years
    Note that there is not currently a single standard "RAW format".
  • Hastur
    Hastur about 3 years
    @RockPaperLz-MaskitorCasket Right and proper. RAW formats are service formats used by the manufacturer to quickly save the contents of the CCD into the buffer and in case on the storage device. They depend on choices dictated by the geometry of the CCD etc etc... Seldom only the manufacturer knows the functions used to convert the RAW picture into another format. Often the transformation is not reversible. See this answer below for some words more.
  • RockPaperLz- Mask it or Casket
    RockPaperLz- Mask it or Casket about 3 years
    "Seldom only the manufacturer knows the functions used to convert the RAW picture into another format." Very interesting... I just learned something new! I think for the first word of that sentence, you meant "Usually" instead of "Seldom"... is that correct?
  • Hastur
    Hastur about 3 years
    Maybe it is poorly worded. With "Seldom ... Often..." I just wanted to express my impression: sometimes the algorithm is not disclosed, more often there are (see convert) ways of converting a specific RAW format into (e.g.) JPEG, even more often the algorithms are not invertible (simple example: shrinking 12 or 14-bits into 8)...
  • Hastur
    Hastur about 3 years
    I am always curious about downvoters ... Please comment and let me (we) know what you disagree, it might be useful to know.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    That's simply not true. If the formats are known you can surely convert them. (It seems obvious to me but I'll state it explicitly to avoid misunderstandings: You cannot recover information that is not present in the source format. But that is self-understood, I hope.) By the way, I do not understand your metaphor.
  • data
    data about 3 years
    @RockPaperLz-MaskitorCasket what about DNG?
  • RockPaperLz- Mask it or Casket
    RockPaperLz- Mask it or Casket about 3 years
    @data I'm sorry, I don't quite understand your question. Can you add some more detail?
  • data
    data about 3 years
    @RockPaperLz-MaskitorCasket you said "there is not currently a single standard RAW format", which is not true - DNG is a standard raw format.
  • RockPaperLz- Mask it or Casket
    RockPaperLz- Mask it or Casket about 3 years
    @data I see. I think your interpretation is likely due to the ambiguities of the English language. Try reading what I wrote this way: "RAW format is not a single standard". In other words, there are many RAW formats.
  • psmears
    psmears about 3 years
    @Hastur: "Seldom" means "very rarely" i.e. using it implies that whatever you're talking about happens hardly at all. In this context you probably want something like "Occasionally" :)
  • billyG windoze user
    billyG windoze user about 3 years
    Pour cream in your coffee. Stir. Then try to separate the cream and the coffee.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    Just so that I understand you better: You are using a metaphor of cream and coffee. What in the raw/jpeg problem corresponds to the cream, and what to the coffee?
  • billyG windoze user
    billyG windoze user about 3 years
    the point is once you change to jpeg from raw you can not go back to raw as there were unfixable changes done to the image.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    Obviously you cannot reconstruct the original image; but you surely can change the format. You still have not explained your metaphor.
  • billyG windoze user
    billyG windoze user about 3 years
    I explained the answer to the OP is correct.
  • billyG windoze user
    billyG windoze user about 3 years
    You cant un-ring the gong. You cant unkill a person. Some things have no reverse.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 3 years
    That is correct; but converting between file formats is not one of those things. To support your opinion, you should be more specific and concrete (e.g., refer to the data formats and the contents which prevent conversion) instead of providing metaphors.