[Python] How to convert raw images acquired with SLR and smartphones to generic raw.



I am sure that many of you who work with image sensors, such as development cameras, deal with general-purpose Raw. From these raw images, image evaluation and image processing are performed. However, the raw images that can be obtained from cameras on the market are compressed using a special method defined by each camera company. Therefore, the usability is different from general-purpose raw images, which are just a series of binaries.
Some of you looking at this site want to convert raw images (DNG, CR2, CR3, etc.) acquired with iPhones and SLRs into a generic raw format for handling! I am sure there are people who would like to do this. In this article, I will teach you how to convert from each company’s Raw format to generic Raw using Python. (super easy)

First, install rawpy, which can handle raw from various companies.

pip install rawpy

I believe rawpy can now be installed.

Next, prepare the Raw image you wish to convert. First, check the image size of the Raw image.

import rawpy

#Path of the raw image to be converted
rawpath = "*****.CR2" 
raw = rawpy.imread(rawpath)


Execute this code to obtain the following information

ImageSizes(raw_height=3024, raw_width=4032, height=3024, width=4032, top_margin=0, left_margin=0, iheight=3024, iwidth=4032, pixel_aspect=1.0, flip=0)

The raw_height of this information is the size of the raw image in the y-direction and the raw_width is the size in the x-axis direction.
Use this information to transform the image.

import numpy as np
import matplotlib.pyplot as plt
import math
from PIL import Image
import rawpy

#Image Setting
rawname = '********' #Raw Name
raw_kaku = '.CR2' #(filename) extension
rawpath = rawname + raw_kaku

raw = rawpy.imread(rawpath)

array = raw.raw_image
plt.imshow(array,cmap = "gray")

Executing this code will display the loaded Raw image and generate a generic Raw image in the same folder with the extension changed to “.raw”.

Please check this page for an explanation of how to analyze iPhone Raw images.