Forum

Author Topic: Import photos from a file list  (Read 6813 times)

tkwasnitschka

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Import photos from a file list
« on: May 20, 2016, 07:10:06 PM »
Okay I admit I am new to python.

I have a 200,000 photo project (yes) and need to subdivide it. Many overlapping survey tracks and runs, file structure by time not by coordinate. I will choose portions of it using GIS software and it gives me a file list with lines like this:

Z:\some\path\000000_000999\20160322_212257_IMG_000001.JPG   20160322_212257_IMG_000001.JPG   -15.00275855   -173.0095973   -455.4

How can I import photos from this list into photoscan? Everyone else on this forum lists the contents of a folder, which does not help me as they are spread across hundreds of folders.

This is what I tried but it terminates with a "cant load photos". I suspect I am parsing a line feed wrong.

import os
import PhotoScan
global doc
doc = PhotoScan.app.document

doc.addChunk()
chunk = doc.chunks[-1]
chunk.label = "toms chunk"

sourcepath = r"C:\Users\Arena\Desktop\test\list.txt"
sourcefile = open(sourcepath, 'r')
print (sourcefile)
files = sourcefile.readlines()
print (files)
chunk.addPhotos(files)

-end of code-
Many thanks!
Tom

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #1 on: May 20, 2016, 07:45:50 PM »
Hello Tom,

addPhotos() function requires the list of path, while in your example it is the list of strings that contain some additional information.

I can suggest the following code:

Code: [Select]
import PhotoScan

doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "toms chunk"

sourcepath = "C:\Users\Arena\Desktop\test\list.txt"
file = open(sourcepath, "rt")
photoslist = file.readlines()
file.close()
photoslist = [photo.split()[0] for photo in photoslist]
chunk.addPhotos(photoslist)
Best regards,
Alexey Pasumansky,
Agisoft LLC

tkwasnitschka

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Import photos from a file list
« Reply #2 on: May 20, 2016, 08:37:42 PM »
Hello Alexey,
thank you so much! (should have asked here right away).

The only addition for everyone else I have is that there needs to be an r before the source path, otherwise \U starts an eight-character Unicode escape:

Code: [Select]
sourcepath = r"C:\Users\Arena\Desktop\test\list.txt"
Besides, my code did not even work when I had only the first string with the file paths in the txt file. It still interpreted the line feed and thus failed, but your method resolves this AND reads the full line.

best greetings
Tom

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #3 on: March 13, 2020, 08:05:07 PM »
Hello guys,

I'm trying to import photos from a list.txt by using your script but i have some issues and the script doesn't seem to work.

This is the error message:
File "<console>", line 1
2020-03-13 16:36:58 import PhotoScan
doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "Traliccio"
sourcepath = r"D:\LAVORI\ADRIREEF\DA_PROCESSARE\Meta-to-RC/Traliccio.txt"
file = open(sourcepath, "rt")
photoslist = file.readlines()
file.close()
photoslist = [photo.split()[0] for photo in photoslist]
chunk.addPhotos(photoslist)
2020-03-13 16:36:58
^
2020-03-13 16:36:58 SyntaxError: invalid character in identifier

Could you please help me?!
Thank you in advance.

Ubi


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #4 on: March 13, 2020, 08:10:58 PM »
Hello Ubi,

Can you please post the complete script that you are using and specify, if you are running it via Run Script command or are copying to the Console pane?

The error is likely indicating to some type or incorrect character (symbol) used in the script body.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #5 on: March 16, 2020, 12:50:40 PM »
Hi Alexey,

I'm copying to the Console pane this script:

import PhotoScan

doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "Traliccio"

sourcepath = r"D:\LAVORI\ADRIREEF\DA_PROCESSARE\Meta-to-RC/Traliccio.txt"
file = open(sourcepath, "rt")
photoslist = file.readlines()
file.close()
photoslist = [photo.split()[0] for photo in photoslist]
chunk.addPhotos(photoslist)

Thank you in advance for your help!

Ubi

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #6 on: March 16, 2020, 03:04:23 PM »
Hello Ubi,

I've tried to copy and paste your code to the Console pane and am not observing any SyntaxError messages.
Which version of PhotoScan/Metashape you are using?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #7 on: March 16, 2020, 03:12:13 PM »
Hello Alexey,

The version is Metashape 1.5.3
I try to update Metashape with the new version and run the script.
I'll be back to you soon.
If you have any other suggestions please do not hesitate to write those here.
Thank You

Ubi

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #8 on: March 16, 2020, 03:32:09 PM »
Hi Alexey,

I updated to the new version 1.6.2.10247 and run the script but always the same syntaxerror message appears.
The file .txt is a list of the photos i would like to import as follows:
00344.JPG
00355.JPG
00356.JPG
00358.JPG
00360.JPG
00361.JPG
00362.JPG
00363.JPG
...

Should I include other information in the .txt file ?!

wojtek

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Import photos from a file list
« Reply #9 on: March 16, 2020, 04:34:13 PM »
Hi Alexey,

I updated to the new version 1.6.2.10247 and run the script but always the same syntaxerror message appears.
The file .txt is a list of the photos i would like to import as follows:
00344.JPG
00355.JPG
00356.JPG
00358.JPG
00360.JPG
00361.JPG
00362.JPG
00363.JPG
...

Should I include other information in the .txt file ?!

This does not seem to include the directory they are located in?

I'd change the code to:

photodir = "your path to folder/"
photoslist = [photodir + photo.split()[0] for photo in photoslist]

that would list the full dir

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #10 on: March 16, 2020, 06:10:38 PM »
Hello Woitek,

I change the code with:

import PhotoScan

doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "Traliccio"

sourcepath = r"D:\LAVORI\ADRIREEF\DA_PROCESSARE\Meta-to-RC/Traliccio.txt"
file = open(sourcepath, "rt")
photoslist = file.readlines()
file.close()
photodir = "D:\LAVORI\ADRIREEF\DA_PROCESSARE\Meta-to-RC/cc/"
photoslist = [photodir + photo.split()[0] for photo in photoslist]
chunk.addPhotos(photoslist)


Because in "cc" folder there are some photos i'd like to import.
Always the same SyntaxError message appears...
Do you have other suggestions?!
Thank you guys

Regards

Ubi


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #11 on: March 16, 2020, 07:21:02 PM »
Hello Ubi,

Can you please save your code as a .py file and execute it via Run Script dialog?

If the error persists, please attach the source .py file and the Console pane output as a text file.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #12 on: March 16, 2020, 07:21:59 PM »
And if you are copying all the lines at once, please make sure that you have Rich Python Console enabled in the Advanced preferences tab.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Ubi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Import photos from a file list
« Reply #13 on: March 17, 2020, 12:33:11 PM »
Hi Alexey,

I saved the code as .py file (here attached) and execute it via Run Script dialog. Here attached also the Console pane output as a text file (Error_message_py.txt).

I also attach  the list of the photos as .txt file.

I confirm that the Rich Python console is enabled.

Thank You for your help.

Best regards,

Ubi

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Import photos from a file list
« Reply #14 on: March 17, 2020, 01:49:26 PM »
Hello Ubi,

Please try to modify the 20th line in your code as following:
Code: [Select]
photoslist = [photodir + "\\" + photo.strip() for photo in photoslist]
Also in the photosdir definition use either slash or double-backslash symbols:
Code: [Select]
photodir = "D:\\LAVORI\\ADRIREEF\\DA_PROCESSARE\\Meta-to-RC\\cc"
Best regards,
Alexey Pasumansky,
Agisoft LLC