Forum

Author Topic: Export textures only  (Read 6261 times)

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Export textures only
« on: February 17, 2017, 06:07:41 PM »
Hello,
I'm currently trying to export textures from my model, the "hard" way as I did not find a way of doing it smoothly: I'm extracting the model then I have my texture.

Is there any smoother way to do it? Maybe directly, and I missed it?

Current code:

Code: [Select]
import os
import sys
import PhotoScan
doc = PhotoScan.app.document
chunk = PhotoScan.Chunk()
chunk.label = "New Chunk"
doc.chunks.add(chunk)
nbTrous = int(sys.argv[1])
path_photos = PhotoScan.app.getExistingDirectory("""Spécifiez le dossier
    contenant les photos : """)
path_photos += "/"
path_export = PhotoScan.app.getExistingDirectory("""Spécifiez le dossier
    contenant les exports en fin de traitement : """)
path_export += "/"
for chunk in doc.chunks
    # Export Texture
    exportModel(path_export, binary = False, texture_format = "jpg",
        texture = True, normals = False, colors = False, cameras = False,
strip_extensions = False, format = "obj", projection = "EPSG::3948")
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #1 on: February 17, 2017, 06:33:27 PM »
Hello Thibaud,

Maybe something like that?

Code: [Select]
chunk = PhotoScan.app.document.chunk
model = chunk.model
texture = model.texture
texture.save("D:/texture" + chunk.label  + ".jpg")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #2 on: February 17, 2017, 06:39:57 PM »
Hello Alexey,

Thanks for you quick answer.

Would it work if I use my path_export before the chunk.label instead?
chunk is already defined earlier in the code (this is part of a bigger code I'll problably need more help about later) so it ends up this way:
Code: [Select]
path_export = PhotoScan.app.getExistingDirectory("""Specify Export folder """)
path_export += "/"
# Export Texture
model = chunk.model
texture = model.texture
texture.save(path_export + chunk.label  + ".jpg")

Best regards
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #3 on: February 17, 2017, 06:40:59 PM »
Hello Thibaud,

It was just an example, you can use any path you like.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #4 on: February 17, 2017, 07:08:38 PM »
Hello Alexey,

I wanted to try this very specific portion of code and it doesn't work, I have these error messages based on whether or not I'm trying to use a "for" loop.

Without "for" loop:
Code: [Select]
2017-02-17 17:00:10 SyntaxError: invalid syntax
2017-02-17 17:00:51 Traceback (most recent call last):
2017-02-17 17:00:51   File "C:/PFE_CAPRA/Essais_Script_1/_S1_PA-170217_ExportTexture.py", line 4, in <module>
2017-02-17 17:00:51     chunk = PhotoScan.Chunk()
2017-02-17 17:00:51 TypeError: cannot create 'Chunk' instances
With "for" loop:
Code: [Select]
2017-02-17 17:01:11   File "C:/PFE_CAPRA/Essais_Script_1/_S1_PA-170217_ExportTexture.py", line 11
2017-02-17 17:01:11     for chunk in doc.chunk
2017-02-17 17:01:11                          ^
2017-02-17 17:01:11 SyntaxError: invalid syntax

Exact code:
Code: [Select]
import os
import PhotoScan
doc = PhotoScan.app.document
chunk = PhotoScan.Chunk()
chunk.label = "New Chunk"
doc.chunks.add(chunk)
path_export = PhotoScan.app.getExistingDirectory("""Spécifiez le dossier
    contenant les exports en fin de traitement : """)
path_export += "/"

for chunk in doc.chunks
chunk = PhotoScan.app.document.chunk
model = chunk.model
texture = model.texture
texture.save("D:/texture" + chunk.label  + ".jpg")
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #5 on: February 17, 2017, 07:26:02 PM »
Hello Thibaud ,

I think you are using partially outdated Python API.

If you need to create a new chunk, you can just use:
Code: [Select]
doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "New Chunk"
Also not sure, why you are using so many quotes in the .getExistingDirectory() function.

The loop is also incorrectly configured:
Code: [Select]
for chunk in doc.chunks:
      texture = chunk.model.texture
      texture.save("D:/texture" + chunk.label  + ".jpg")
But it will work only if every chunk in the document has the textured model. If the code you are providing is complete, then it is not the case, because you are adding and empty chunk.

Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #6 on: February 17, 2017, 07:43:25 PM »
Hello Alexey,
I'm using PhotoScan 1.2.6., how do I check if my API is outdated?

The multiple quotes are here to color the strings in a different way as I'm writing my script using NotePad++ (See attached). It should allow me to write multiline texts without using \n, right? As I'mfairly new to Python, colors help me find what's wrong and identify things in my code.

I'll attach the full, commented code for you to see more in depth.

You'll notice that there are two parts in the code: they're meant to be split into two scripts later, as there will be a manual intervention bewteen the photo alignment step and the dense point cloud generation step: the user will have to define the bounding box in each chunk because only parts of the point cloud need to be densified. The sparse point cloud is here to be used as a way to identify places.

Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #7 on: February 17, 2017, 07:58:20 PM »
Hello Thibaud,

The error message indicates that you cannot create PhotoScan.Chunk() instances, you need to use doc.addChunk() option.

The script description says it is for the version 1.1.

This line has proved to be incorrect also:
Code: [Select]
if ("jpg" or "jpeg" or "tif" or "png") in photo.lower():instead you need to check:
Code: [Select]
if photo.rsplit(".",1)[1].lower() in ["jpg", "jpeg", "tif", "png"]:Additionally I can suggest to append all paths to the list and then after loop use chunk.addPhotos(that_list) option to add all the photos at once.

The parameters in most of processing and export functions were changes from string types to enum, so you need to refer to the actual Python API reference.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #8 on: February 20, 2017, 01:20:10 PM »
Hello Alexey,

Using the Python API for the 1.2.0 release, I have a significant lower amount of errors in my code.

The main problem now is that when I run my script, PhotoScan is "unable to load photos" (2017-02-20 11:08:12 Error: Can't load photos)

Since my photos' extensions are in upper case, I figured this code would be the problem:

Code: [Select]
# Add Photos to Project
image_list = os.listdir(path_photos)
for photo in image_list:
    if photo.rsplit(".",1)[1].lower() in ["jpg", "jpeg", "tif", "png"]:
        chunk.addPhotos(path_photos + photo)
else:
    print("""Pas de photo exploitable dans le dossier.""")
PhotoScan.app.update()

So I edited that part to consider the extension in UPPER case.

Code: [Select]
image_list = os.listdir(path_photos)
for photo in image_list:
    if photo.rsplit(".",1)[1].upper() in ["JPG", "JPEG", "TIF", "PNG"]:
        chunk.addPhotos(path_photos + photo)
else:
    print("""Pas de photo exploitable dans le dossier.""")
PhotoScan.app.update()

No idea what's wrong here, can you help me?
As for the rest, PhotoScan then tries to start a Photo Align process but fails because I have no photo loaded. The chunk duplication part has an issue in the renaming process but that's not an important issue.

I hope you had a great week end!
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #9 on: February 20, 2017, 01:24:59 PM »
Hello Thibaud,

You can use print statements for debugging purposes, for example, print path_photos before the loop, print photo and photo.rsplit() inside the loop before the conditions.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #10 on: February 20, 2017, 03:55:32 PM »
Hello Alexey,

I wrote a quick script to print all my statements here, and everything looks normal to me, the path in the end is correct, the photo.rsplit() sends back 'JPG'... Everything is "normal" according to my very small knowledge!

Code: [Select]
import os
import sys
import PhotoScan

print(""">>> Initialisation du script <<<""")

nbTrous = int(sys.argv[1])
path_photos = PhotoScan.app.getExistingDirectory("""Spécifiez le dossier
 contenant les photos : """)
path_photos += "\\"
path_export = PhotoScan.app.getExistingDirectory("""Spécifiez le dossier
 contenant les exports en fin de traitement : """)
path_export += "\\"

print(nbTrous)
print(path_photos)
print(path_export)
image_list = os.listdir(path_photos)
print(image_list)

image_list = os.listdir(path_photos)
for photo in image_list:
    print(path_photos + photo)

print(""">>> Fin du script <<<""")

I provided results in images attached.

Here's the code I'm using to unsuccessfully load the images in PhotoScan:

Code: [Select]
# Add Photos to Project
image_list = os.listdir(path_photos)
for photo in image_list:
    if photo.rsplit(".",1)[1].upper() in ["JPG", "JPEG", "TIF", "PNG"]:
        chunk.addPhotos(path_photos + photo)
else:
    print("""Pas de photo exploitable dans le dossier.""")
PhotoScan.app.update()

Thanks for your help so far
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #11 on: February 20, 2017, 05:13:03 PM »
Hello Thibaud,

Can you add some print statements to the working code that you are using:

Code: [Select]
# Add Photos to Project
image_list = os.listdir(path_photos)
photo_list = list()
for photo in image_list:
    if photo.rsplit(".",1)[1].upper() in ["JPG", "JPEG", "TIF", "PNG"]:
           photo_list.append(path_photos + photo)
           print(photo)
     else:
           print("""Pas de photo exploitable dans le dossier.""")
print(photo_list)
chunk.addPhotos(photo_list)
PhotoScan.app.update()

also not sure if it is just a copying issue, but "else" should have the same indentation, as "if".
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #12 on: February 20, 2017, 07:17:04 PM »
Hello Alexey,

Looks like this one's working, the list() line did the trick I guess!

My workstation is currently aligning photos. Next step is supposed to be duplicating chunks to a user-defined number set as an input argument before running the script.

Code: [Select]
import os
import sys
import PhotoScan

doc = PhotoScan.app.document
chunk = doc.addChunk()
nbTrous = int(sys.argv[1])

# Create a user-defined number of chunk duplicates
chunkIter = 1
strIter = str(chunkIter)
nomChunk = "Trou" + strIter
while chunkIter <= nbTrous:
    doc.chunk.copy()
    chunk.label("nomChunk")
    chunkIter += 1
print("""----------- Chunks OK -----------------------""")

As the iteration goes on, I want to rename my chunks "Trou X" where X is an increasing number (from 1 to nbTrous  (user-defined])
But it looks like I can't use a string to rename my chunk!
Code: [Select]
2017-02-20 17:12:19   File "C:/PFE_CAPRA/Scripts/Test_ChunkDplct.py", line 15, in <module>
2017-02-20 17:12:19     chunk.label("nomChunk")
2017-02-20 17:12:19 TypeError: 'str' object is not callable

Any other way round?


Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14839
    • View Profile
Re: Export textures only
« Reply #13 on: February 20, 2017, 07:26:12 PM »
Code: [Select]
chunkIter = 1
chunk = doc.chunk #active chunk, if there's any in the project
while chunkIter <= nbTrous:
      chunk.label = "Trou" + str(chunkIter)
      chunk =  doc.chunk.copy()
      chunkIter += 1
Best regards,
Alexey Pasumansky,
Agisoft LLC

Thibaud Capra

  • Full Member
  • ***
  • Posts: 101
  • Master Student in Geodetic Engineering & Surveying
    • View Profile
    • INSA de Strasbourg, Topography Engineering (French)
Re: Export textures only
« Reply #14 on: February 21, 2017, 12:51:48 PM »
Hello Alexey,

Again, thanks a lot for helping me fixing my code.

As we underlined the potentially outdated Python API, I figured I had to use the 1.2.0 one. Everything works great so far, up until the texture export... Which throws us back to the origin of the topic!

I tried to adapt the code you first provided in 1.2.0 as follows:

Code: [Select]
    # Export Texture
    path_tex = path_export + chunk.label + "_Tex" + ".jpg"
    PhotoScan.Model.saveTexture(path_tex)

I have the following error:
PhotoScan.Model.saveTexture(path_tex)
TypeError: descriptor 'saveTexture' requires a 'PhotoScan.Model' object but received a 'str'


According to the PhotoScan Python Reference 1.2.0, the saveTexture method is expecting a string, hence getting me confuzzled about what I should do...

Best regards.
Best regards.
--
Thibaud CAPRA
Master Student in Geodetic Engineering, Cartography & Surveying
Master Thesis in Automated Processing of UAV-based Photogrammetric Data (ResearchGate Link)
INSA de Strasbourg, FRANCE
--