Forum

Author Topic: Masking  (Read 5284 times)

SAV

  • Hero Member
  • *****
  • Posts: 710
    • View Profile
Masking
« on: June 12, 2014, 12:01:36 PM »
I would like to use the same mask on all my images. Has someone a script to share?

Cheers
SAV

SAV

  • Hero Member
  • *****
  • Posts: 710
    • View Profile
Re: Masking
« Reply #1 on: June 13, 2014, 07:31:58 AM »
Hello everyone.
After a night-session of coding I was able to come up with a working python script (I guess a programmer would have done it in 10 minutes ;)). Nevertheless, I am happy because the script is doing what it's supposed to do (applying the same mask to all images).

Code: [Select]
#################################################################
# Script will apply the same mask to images in selected folder  #
# STEPS                                                         #
# 1) Select image folder                                        #
# 2) Select mask folder                                         #
# 3) Wait until images are processed / masked                   #
#                                                               #
#################################################################
#Name of mask file you want to use                              #
m = "mask.png"                                                  #
#do not change code below this line  ;-)                        #
#################################################################

import PhotoScan
import os
import sys

app = PhotoScan.Application()


path_photos = PhotoScan.app.getExistingDirectory("Select folder with images to be masked")
path_photos += "/"

path_mask = PhotoScan.app.getExistingDirectory("Select the location of the mask")
path_mask += "/"

mask_name=path_mask + m

doc = PhotoScan.app.document
chunk = PhotoScan.Chunk()
Image=PhotoScan.Image()
PhotoScan.app.update()

chunk.label = PhotoScan.app.getString(label="Please enter a name for the new chunk", value="New Chunk")

image_list=os.listdir(path_photos)

# Loop to load images and apply mask
for i in range(0, len(image_list)):
    photo = PhotoScan.Photo()
    photo.open(path_photos + image_list[i])
    photo.label = image_list[i]

    Image.load(mask_name, layer=0, format="U8")
    mask1_name=Image.copy()

    photo.setMask(mask1_name)
    chunk.photos.add(photo)
    print(image_list[i] + " processed")
   
PhotoScan.app.document.chunks.add(chunk)
PhotoScan.app.update()

print("Congratulations: " + str(len(image_list)) + " images processed!")


I thought I would share it - maybe someone can use it.

Cheers
Stefan