0

I'm starting to create a new 3D scanner with a Raspberry Pi 3B + and a Canon 6D. I have a part of the Python code to recover the images thanks to the gphoto2 library but I can not change the name of the recovered images, currently, I have two files: capt0000.cr2 and capt0000.jpg I must renames them to "time" + .jpg or .cr2 but impossible, they never change their name.

I tried several methods and currently I am with the os.listdir function that allows me to sort all the files on the desktop.

Program start:

from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess

shot_date = datetime.now().strftime("%d-%m-%Y")
shot_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
picID = "PiShots"
folder_name = shot_date + picID
save_location = "ScannerImages/" + folder_name

CaptureImageDownload = ["--capture-image-and-download"]
CaptureImage = ["--capture-image"]

Functions:

def captureImageDownload():
    gp(CaptureImageDownload)

def captureImage():
    gp(CaptureImage)

def createFolder():
    try:
        os.makedirs(save_location)
    except:
        print("Failed to create folder")
    os.chdir(save_location)

def renameFiles(ID):
    for filename in os.listdir("."):
        if len(filename) < 13:
            if filename.endswith(".jpg"):
                os.rename(filename, (shot_time + ID + ".jpg"))
            print("Renamed the JPG")
        elif filename.endswith(".cr2"):
            os.rename(filename, (shot_time + ID + ".cr2"))
            print("Renamed the CR2")

Main loop:

captureImageDownload()
createFolder()
renameFiles(ID)

Now I have two files that are created on the desktop see image below: https://i.imgur.com/DDhYe1L

Is it due to file permissions knowing that I am not the root user? If it is because of that how to change the permissions on a type of file in general for example .jpg because each time it is about a new image so the permissions return to the image below: https://i.stack.imgur.com/okfSx.jpg

ValentinDP
  • 323
  • 5
  • 14
  • 1
    Can you put the images in the question rather than in links to imgur? Not all of us have access to image hosting sites – C.Nivs Jan 29 '19 at 14:31
  • 1
    I think the problem is that you are changing the directory in the `createFolder()` function so that when you `os.listdir(".")` you list the files in *that* folder. – Ross MacArthur Jan 29 '19 at 14:32
  • @C.Nivs Sorry but i can't post image because I'm a new user. – ValentinDP Jan 29 '19 at 14:39
  • @Ross I just tried removing the function to create the folder but I still do not change the name of the files. – ValentinDP Jan 29 '19 at 14:41
  • Why not print out each filename when you `os.listdir(".")`, to make sure you are in the correct folder? That will help you debug. – Ross MacArthur Jan 29 '19 at 14:41
  • @Ross Ah thank you it works perfectly, it remains more than move them in the folder just create before. – ValentinDP Jan 29 '19 at 14:45

2 Answers2

0

I guess it's a Problem with os.chdir(save_location). You got to use the complete path (see https://www.tutorialspoint.com/python/os_chdir.htm) Try something like

path = os.path.join(os.getcwd(), save_location)
os.chdir(path)

If you want to change file permissions in your code, use os.getcwd() (see https://www.tutorialspoint.com/python/os_chown.htm). You can get your current UID by os.getuid(). So add to renameFiles:

uid = os.getuid()
gid = os.getgid()
for filename in os.listdir("."):
    filepath = os.path.join(os.getcwd(), filename)
    os.getcwd(filepath, uid, gid)
    ....

so all the files will belong to the current user. Maybe you need to run your script with "sudo"

JohnTanner
  • 66
  • 4
0

The problem is solved, here is the solution:

Main loop:

captureImageDownload()
renameFiles(ID)
createFolder()

You just had to rename the file before creating the image folder.

ValentinDP
  • 323
  • 5
  • 14