Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Trying to write a Python script to convert RAW DNG files to PNG

HunterAP
 Share

I take pictures with the OpenCamera app on my Android phones in the DNG format, but the only Windows apps I have that can work with DNG files don't pragmatically convert them for me, or I have to go through some steps to load the programs themselves (Photoshop has to load up and go through some GUI to get to the convervion & it only batch converts to JPEG, IrfanView is a similar case).

 

I wrote this Python script that uses multiple modules to handle reading and writing RAW type files, as well as displaying a progress bar in the console window.

First it scans for any files with the DNG extension, appends those files to a list, then convert them from DNG to PNG and move them into newly created folders named after each individual DNG file.

import rawpy
import imageio
import os
import shutil
import sys

myDNG = []
for root, directories, filenames in os.walk(str(os.getcwd())):
    print("Total Count of Files: " + str(len(filenames)))
    dngCount = 0
    for filename in filenames:
        if filename.endswith(".dng"):
            myDNG.append(filename)
            dngCount += 1

print("Total Count of DNG Files: " + str(dngCount))
if dngCount == 0:
    print("No DNG files to convert, exiting...")
    exit()
else:
    for i in range(len(myDNG)):
        check = myDNG[i].replace('.dng', '.png')
        if os.path.isfile(check) is not True:
            try:
                print("Working on " + str(myDNG[i]))
                raw = rawpy.imread(str(myDNG[i]))
                rgb = raw.postprocess()
                newName = myDNG[i].replace('.dng', '.png')
                newFile = os.path.join(root, newName)
                imageio.imsave(newFile, rgb)
                print("Done.")

                folderCheck = str(os.getcwd() + "\\" + str(myDNG[i]).replace('.dng', ''))
                if os.path.isfile(folderCheck) is not True:
                    print("Making folder " + folderCheck)
                    os.mkdir(folderCheck)
                newName = myDNG[i].replace('.dng', '.png')
                newFile = os.path.join(root, newName)
                raw.close()
                print("Moving " + str(myDNG[i]) + " to " + str(folderCheck))
                shutil.move(myDNG[i], folderCheck)
                print("Moving " + str(newFile) + " to " + str(folderCheck))
                shutil.move(newFile, folderCheck)
            except Exception as e:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
                print(exc_type, fname, exc_tb.tb_lineno)
        else:
            folderCheck = str(os.getcwd() + "\\" + str(myDNG[i]).replace('.dng', ''))
            if os.path.isfile(check) is not True:
                print("Making folder " + folderCheck)
                os.mkdir(folderCheck)
            newName = filename.replace('.dng', '.png')
            newFile = os.path.join(root, newName)
            print("Moving " + str(myDNG[i]) + " to " + str(folderCheck))
            shutil.move(myDNG[i], folderCheck)
            print("Moving " + str(newFile) + " to " + str(folderCheck))
            shutil.move(newFile, folderCheck)
        inp = input("")

 

The problem is, DNG is one of the few RAW file types that don't work with this method: the default configuration for handling RAW files, but it adds a sepia-like tone to the pictures, and there is no way to fix it by changing the conversion settings.

 

Is there any change I can make with this script, or another script entirely to fix this issue?

Link to comment
Share on other sites

Link to post
Share on other sites

The module is partially the issue: the rawpy module is a python wrapper for the libraw program, but libraw itself doesn't have good support for DNG files either.

 

I ended up using IrfanView in a batch file to convert the files, but for testing purposes I also used Photoshop's Batch converter to see what looked better.

IrfanView is on the left, Photoshop is on the right.

IMG_20180521_201331.thumb.jpg.f7e18eaf77a81667a12a86b6564449cf.jpg

IMG_20180521_2013311_2.thumb.jpg.18ba52d3faba0d04ec107996eb85eb75.jpg

 

Even though the Photoshop one is much brighter and more saturated, it's more accurate to the actual photo shooting conditions.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

Newegg

×