I am looking to access all photos on my iPhone using Python.
I want to be able to exract the Exif data to load into Leaflet / Folium.
It would seem that I cannot access the path for the photos because the phone is connected using a MTP Connection.
I found a script and managed to modify it to allow me to list all photos on the phone in the DCIM directory.
for pidl in folder.EnumObjects(0, shellcon.SHCONTF_NONFOLDERS):
name = folder.GetDisplayNameOf(pidl, shellcon.SHGDN_FORADDRESSBAR)
dirname = os.path.dirname(name)
basename, ext = os.path.splitext(os.path.basename(name))
#print (name)
#print (dirname)
#print (basename)
if ext.endswith("JPG") and i == 1:
photo_dict[dirname].append(name)
i =+ 1
print (photo_dict[dirname][0])
I am access the path to the image, but when I try and open the image using PIL, I receive an error that the path does not exist. Which I am guessing is due to the MTP connection and the way that interacts with the File Explorer.
How can I process imagery using the path provided by my script, is there a better method?
I want to keep the files on the phone, so copying off is not an option.
I am using Python 3.5 and Windows 10.
Related
for reference this is all using Pyqt5 and Python 3.6:
I've got a QStandardItemModel that is built from QStandardItems that are strings of the items in a zip (the model displays all the contents of a zipfile). I went with this choice as I can not cache the files locally, and my research shows that QFileSystemModel can not work on archives unless I unpack at least temporarily.
All items in the QStandardItemModel end in the correct extension for the file (.csv,.txt,ect), and I need to display the icon a user would see if they were looking at the file in windows explorer, however show it in the qtreeview (a user seeing content.csv should also see the icon for excel). On that note, this application is only running on windows.
How can I pull the extensions default system file icon, and set it during my setting of these items? Would I have to manually download the icons for my known file types and do this, or does the system store it somewhere I can access?
Here's some basic code of how I build and display the model and treeview:
self.zip_model = QtGui.QStandardItemModel()
# My Computer directory explorer
self.tree_zip = QTreeView()
self.tree_zip.setModel(self.zip_model)
def build_zip_model(self,current_directory):
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
model_item = QtGui.QStandardItem(item)
self.zip_model.appendRow(model_item)
You can use QFileIconProvider:
def build_zip_model(self, current_directory):
iconProvider = QtWidgets.QFileIconProvider()
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
icon = iconProvider.icon(QtCore.QFileInfo(item))
model_item = QtGui.QStandardItem(icon, item)
self.zip_model.appendRow(model_item)
At the organization I work for, different printers are set up at various locations. All are mainly used to print A4-sized documents, so the defaults are set up accordingly.
We are also using a bunch of custom-sized forms which people have up to now been filling in by hand.
Recently, I was tasked with setting up print-automation onto the said forms from our central database.
I'm using reportlab to create temporary pdf files which I am then trying to send to the default printer. All is relatively simple, save for getting the printers to register a custom paper size.
I got as far as the following code snippet, but I'm really stuck.
import tempfile
import win32api
import win32print
pdf_file = tempfile.mktemp(".pdf")
#CREATION OF PDF FILE WITH REPORTLAB
printer = win32print.GetDefaultPrinter()
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(printer, PRINTER_DEFAULTS)
level = 2
properties = win32print.GetPrinter(pHandle, level)
pDevModeObj = properties["pDevMode"]
pDevModeObj.PaperSize = 0
pDevModeObj.PaperLength = 2200 #SIZE IN 1/10 mm
pDevModeObj.PaperWidth = 1000 #SIZE IN 1/10 mm
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,level,properties,0)
#OPTION ONE
#win32api.ShellExecute(0, "print", pdf_file, None, ".", 0)
#OPTION TWO
win32api.ShellExecute (0,"printto",pdf_file,'"%s"' % printer,".",0)
win32print.ClosePrinter(pHandle)
It just does not work. Printers do not report a "paper size mismatch", like they should when a non-A4 document is being sent to them. And when I try printing to a PDF printer, it also defaults to A4.
When calling
print(pDevModeObj.PaperSize)
print(pDevModeObj.PaperLength)
print(pDevModeObj.PaperWidth)
everything seems to be in order, so I'm guessing I don't know how to send those paper size values back to the printer settings.
Here is a list of all the resources I checked out (examples not all in python, and a few are not using the win32api), and couldn't get the thing to work properly:
Programmatically Print a PDF File - Specifying Printer
Python's win32api only printing to default printer
https://mail.python.org/pipermail/python-win32/2005-August/003683.html
https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api
Print PDF file in duplex mode via Python
https://www.thinbug.com/q/39249360
Saving / Restoring Printer DevModes - wxPython / win32print
pywin32: how do I get a pyDEVMODE object?
https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-documentproperties
How to change printer preference settings using python
Print file to continuous paper using win32print Python
python win32print can't set custom page size
http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
https://newcenturycomputers.net/projects/pythonicwindowsprinting.html
Printing a file and configure printer settings
Change printer default paper size
https://grokbase.com/t/python/python-win32/085x5hdbtd/how-to-change-paper-size-while-printing
openpyxl - set custom paper size for printing
Python win32print changing advanced printer options
Printing PDF files with Python
Python silent print PDF to specific printer
https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
Printing PDF's using Python,win32api, and Acrobat Reader 9
Python print pdf file with win32print
How to chose Paper Format when printing a PDF File with Python?
Access denied when attempting to remove printer
https://www.programcreek.com/python/example/24860/win32api.ShellExecute
https://opensource.gonnerman.org/?p=192
Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?
https://mail.python.org/pipermail/python-win32/2008-May/007640.html
http://timgolden.me.uk/python/win32_how_do_i/print.html
ShellExecute is using the default printing parameters. If you need to use the reset DevMode for printing, you can use CreateDC.
Refer: GDI Print API
If you use SetPrinter to modify the default DEVMODE structure for a
printer (globally setting the printer defaults), you must first call
the DocumentProperties function to validate the DEVMODE structure.
Refer:
SetPrinter Remarks
Modify printer settings by using the SetPrinter function
You can also directly use DocumentProperties to modify printer initialization information.
Then pass pDevModeObj to CreateDC, and use StartDoc and StartPage to print.
Similar case: Change printer tray with pywin32
I would like to be able to write some metadata to a video, media file in Windows 10 using python3. Doing some searching online I figured out how to read the metadata with the code below. Nowhere online could I find any information about writing the data. I wonder if this is even possible with Python.
Any help would be appreciated.
from win32com.propsys import propsys, pscon
filename = "Video.mp4"
properties = propsys.SHGetPropertyStoreFromParsingName(filename)
title = properties.GetValue(pscon.PKEY_Title).GetValue()
print (title)
Im working on a simple Snake game in python, and its working as intended here at home, but when I put the code on github, it doesn't find the path to the songs, I want to make this path relative, intead of absolute so it works on every computer.
Here is the part of the code for the song files -
def game_sound(s):
""" Include the game sfx and music"""
if s == 0:
pygame.mixer.music.load("background.ogg")
pygame.mixer.music.play(-1)
elif s == 1:
pygame.mixer.Sound("eating.wav").play()
elif s == 2:
pygame.mixer.Sound("game-over.wav").play()
TL - DR- It works here at home and nowhere else, Im trying to find a way to fix that by making the path relative I just dont know how. Can someone help?
Standard method is to find real path to folder with application
import os, sys
APP_FOLDER = os.path.dirname(os.path.realpath(sys.argv[0]))
And later use it to create real path to file
full_path = os.path.join(APP_FOLDER, "eating.wav")
pygame.mixer.Sound(full_path).play()
Or you have to change "current working directory" (CWD) to application folder.
os.chdir(APP_FOLDER)
pygame.mixer.Sound("eating.wav").play()
You can check current working directory with
print(os.getcwd())
BTW: without this method problem is not only when you run on different computer but also when you run from different folder on the same computer - so it makes problem when you create shortcut/icon on desktop which executes program as python game_folder/game.py
I have a small program that works fine on my PC but I want to make it portable. What it does is download an image from the internet, set as desktop background, wait one minute and update the image. I know that I cannot write directly to folders like appdata, as I do not know the username of the person using the computer. I need to save the downloaded image somewhere, so I would save it in the windows Temp folder.
Some options I think would be to (However I don't know how to do this in python)
Use something like %temp% to access the folder.
Find out the username of the person running the program and insert into path
Use a variable for the username.
Use relative paths
I would like to try and not have to use another module not by default included in Python 3, as I want to cx_freeze it later on.
import pythoncom
from urllib import request
from win32com.shell import shell, shellcon
from time import sleep
def get_image():
f = open('C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg', 'wb') #Open old image
f.write(request.urlopen('blalbla.com/foo/img.jpg').read()) #Get new image and write
f.close()
pathtoimg = 'C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg'
count = 0
while 1:
get_image()
iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IActiveDesktop)
iad.SetWallpaper(pathtoimg, 0)
iad.ApplyChanges(shellcon.AD_APPLY_ALL)
count += 1
print(count)
sleep(60)
Use this to locate Temp:
import os
mytmpdir = os.environ['TEMP'] #Must be uppercase