I made a code to save the picture after capturing it on Raspberry Pi, and I want to number it.

Asked 1 years ago, Updated 1 years ago, 365 views

StartTime = time.time()
LastTime = 0
for i in range (1) :
 while True :
    #[Do if there is Data to Process.]
  Length = Serial_PC_Justin.inWaiting()
  if Length == 0:

   continue
  data = Serial_PC_Justin.read(Length)
  File_Justin = open("LogData%s.jpg"%i,"ab")
  File_Justin.write(data)
  File_Justin.close()
  TotalLength += Length

    #[Print Periodically.]
  TimePassed = time.time() - StartTime
  if TimePassed > LastTime + 1:
   print(f"Total Length: {TotalLength} bytes")
   print(f"Average Speed: {TotalLength / (1024 ** 2 * TimePassed)} MB/s")
Serial_PC_Justin.close()   #[Close the Port.]

I want to save the picture as LogData.jpg, but I want to number every time I turn the code is turned.

예를 들면 LogData1.jpg,LogData2.jpg, ...

Now, it's always overwritten in one file.

raspberry-pi python

2023-01-03 10:56

1 Answers

for in range (1): Delete the loop and try writing it like this instead.

import datetime

file_dt = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
File_Justin = open("LogData%s.jpg"%file_dt, "ab")

그러면 대충 LogData20230103162241.jpg, LogData20230103162242.jpg, ... That's how the file name will be created.

If you look at the actual usage scenario, I think the timestamp would be better than the serial number starting with 1, so I recommend this.


2023-01-03 23:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.