Forum

Author Topic: Export Undistort cameras to new folder  (Read 2467 times)

magic

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Export Undistort cameras to new folder
« on: December 08, 2017, 04:12:36 PM »
Hi Alexey

Iv been trying to export and undistort my cameras to new folder with some python script , i found few old scripts but they dont work , Iv tried to modify them without any results positive, that my code
 
Code: [Select]
import PhotoScan
import os, sys
os.mkdir("F:\\Ps\\res_photo\\undisorted_done")
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\HD") #creation new directory 1st-undisorted_done, 2nd HD
my_dir = ("F:\\Ps\\res_photo\\undisorted_done")
chunk = doc.chunk #active chunk
camera = chunk.cameras[0]
image = camera.photo.image()
calib = camera.sensor.calibration
undist = image.undistort(calib,True, True)
save_img = os.path.join(my_dir)
undist.save(save_img) #path should be defined
Have an error
   Out[32]: 2017-12-08 15:04:43 False

How we can modify it Alexey ? I want to Undisort (without calibration) my cameras and safe them into new folders 'undisorted_done' and 'HD' how we can do this with python script , what i have to modify??
« Last Edit: December 08, 2017, 05:06:14 PM by magic »

magic

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Export Undistort cameras to new folder
« Reply #1 on: December 08, 2017, 07:20:48 PM »
I figured it out :-)

Code: [Select]
import PhotoScan
import os, sys
os.mkdir("F:\\Ps\\res_photo\\undisorted_done")
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\HD") #creation new directory 1st-undisorted_done, 2nd HD
my_dir = ("F:\\Ps\\res_photo\\undisorted_done\\HD")
chunk = doc.chunk #active chunk

for camera in chunk.cameras:
     #camera = chunk.cameras[0]
    currentCameraImage = camera.photo.image()
    calibration = camera.sensor.calibration
    undistortedCameraImage = currentCameraImage.undistort(calibration, True, True)
    undistortedFullImageName = os.path.split(camera.photo.path)[-1]
    undistortedImageName = os.path.splitext(undistortedFullImageName)[0]
    save_img = os.path.join(my_dir,os.path.basename(undistortedImageName) + '_.jpg')
    undistortedCameraImage.save(save_img)

thats works

magic

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Export Undistort cameras to new folder
« Reply #2 on: December 13, 2017, 11:47:16 AM »
Hi Alexey,
I have a small problem again Alexey, my script for undistorted cameras works well, and now iv added second one to Resize
and save in new directory undistorted cameras and PhotoScan crashed , and do this each time when i put my script to the console.
My new script works well in python console but dsnt works in PhotoScan, could You check it for me please why , whats wrong with , did is a PhotoScan python library problem ??
Iv added Pilllow module to main python PhotoScan Library.

Code: [Select]
import os, sys
import PhotoScan
from PIL import Image
from os import listdir
from os.path import isfile, join
from PySide2 import QtCore, QtGui, QtWidgets

os.mkdir("F:\\Ps\\res_photo\\undisorted_done")
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\HD") #creation new directory 1st-undisorted_done, 2nd HD
my_dir = ("F:\\Ps\\res_photo\\undisorted_done\\HD")
chunk = doc.chunk #active chunk

for camera in chunk.cameras:
     #camera = chunk.cameras[0]
    currentCameraImage = camera.photo.image()
    calibration = camera.sensor.calibration
    undistortedCameraImage = currentCameraImage.undistort(calibration, True, True)
    undistortedFullImageName = os.path.split(camera.photo.path)[-1]
    undistortedImageName = os.path.splitext(undistortedFullImageName)[0]
    save_img = os.path.join(my_dir,os.path.basename(undistortedImageName) + '_.jpg')
    undistortedCameraImage.save(save_img)

mypath = "F:\\Ps\\res_photo\\undisorted_done\\HD"
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\LT")
noveaux_dir = "F:\\Ps\\res_photo\\undisorted_done\\LT"
dirs = os.listdir( mypath )
basewidth = 256

for im in dirs:
    img = Image.open(join(mypath, im))
    src_fname, ext = os.path.splitext(mypath + im)
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), Image.ANTIALIAS)
    save_fname = os.path.join(noveaux_dir, os.path.basename(src_fname)+'_res.jpg')
    img.save(save_fname)

Fast explication: my script work till last save ( im2.save(save_fname)), iv used print to debug it and he do all actions , but dont like SAVE mode, whats i can do Alexey ????
« Last Edit: December 13, 2017, 11:56:00 AM by magic »

magic

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Export Undistort cameras to new folder
« Reply #3 on: December 13, 2017, 12:34:12 PM »
I figured it out

Code: [Select]
import PhotoScan
import os, sys
os.mkdir("F:\\Ps\\res_photo\\undisorted_done")
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\LT")
os.mkdir("F:\\Ps\\res_photo\\undisorted_done\\HD") #creation new directory 1st-undisorted_done, 2nd HD
my_dir = ("F:\\Ps\\res_photo\\undisorted_done\\HD")
noveaux_dir = "F:\\Ps\\res_photo\\undisorted_done\\LT"
chunk = doc.chunk #active chunk

for camera in chunk.cameras:
    myImage = camera.photo.image()
    calibration = camera.sensor.calibration
    newImage = myImage.undistort(calibration, True, True)
    newName = os.path.split(camera.photo.path)[-1]
    newName_undi = os.path.splitext(newName)[0]
    save_img = os.path.join(my_dir,os.path.basename(newName_undi) + '_.jpg')
    newImage.save(save_img)
    img = camera.photo.image()
    img = img.resize(256, 170)
    newName1 = os.path.split(camera.photo.path)[-1]
    newName_undi1 = os.path.splitext(newName1)[0]
    save_fname = os.path.join(noveaux_dir, os.path.basename(newName_undi1)+'_res.jpg')
    img.save(save_fname)

Action : create new directories , Undistorted cameras and resize undistorted cameras :-)