Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Extension and filename from photos.get_metadata()
-
Howdy,
I'm writing a little script that uploads the most recent photo to a cloud service, grabs, the direct URL, then throws that through bitly. I can't seem to figure out how to get the filename, so I'm setting it manually via console.alert, and I'm a little confused on what the file extension should be.
For example, in photos.get_metadata(-1) from a screenshot, I see a dictionary key
{PNG}
(not sure if backticks make code blocks on this forum...), which is correct, so I set the file extension to png. However, for photos taken with the camera, photos.get_metadata(-1) returns a few keys, including exif data, and a{TIFF}
key.However, I'm pretty sure that those are .jpg and not .tiff files... so is there any way I can detect what the proper file extension is for each file? I tried looking into the mimetypes module, but it looks like you need a file read to use that, and unfortunately I'm achieving that with photos.save to a StringIO... and I think the photos.save requires an extension (http://omz-software.com/pythonista/docs/ios/Image.html).
Also, I don't see any pattern that resembles the filenames that photos have when imported through iPhoto or what have you (IMG_XXXX.jpg). Is there any way to find what a photo's filename is?
Something that could retrieve the path to the file would accomplish both of these, but I'd be surprised if Apple hadn't sandboxed that ability.
Thanks for any help!
-
In comparison, looking at OMZ's ImageMail Gist https://gist.github.com/omz/4073599, I notice a few things:
- Use of io.BytesIO instead of StringIO
- Assigning jpeg format without trying to detect anything from the image
- Assigning a filename (image.jpeg) without trying to detect anything form the image
-
Also tried pulling in the imghdr module, but I can't figure out what kind of byte stream to feed it, imghdr.what(None, io.BytesIO(image_file)) is not working.
-
I'm sorry, but it's currently not possible to get an image's original file name. This is something I plan to add though.
-
No worries, thanks for the reply. I can just create one for the purposes of this script.
As far as the image extension -- would you recommend just using .png or .jpg based on the EXIF containing {PNG} or {TIFF}, respectively? (I can't imagine a whole lot of other image types ending up in my camera roll.)
-
It should be a pretty safe assumption that the images are either JPEG or PNG, though it actually doesn't really matter in the context of the photos module. What you get back is an already-decoded image, not the source data, so you could save it in any format you like, though you might want to use PNG to preserve transparency if the source image is in that format. But in some situations, you might also want to save any image as JPEG to reduce the file size.
-
Ah, that makes sense. Thanks for the explanation.
I'll probably just have it look for {PNG} and otherwise use .jpg.
-
JPEG 2000 is a new image coding system that uses state-of-the-art compression techniques based on wavelet technology. Its architecture should lend itself to a wide range of uses from portable digital cameras through to advanced pre-press, medical imaging and other key sectors.
PNG or <a href="http://www.yiigo.com/support/kb/formats/png/">Portable Network Graphics</a> is raster graphics file format that supports lossless data compression. PNG was created to replace Graphics Interchange Format (GIF) and Tagged Image File Format (TIFF) and add some functionality that GIF does not have.
Unlike other image file format, PNG supports 256 colors palette-based images with a relatively small file size; RGB, RGBA and Grayscale image with or without alpha channel; 1-48 bit True-color image and 1-16 bit grayscale image; gamma adjustment; storing image name, writer, version, time, annotation and more; and lossless data compression method known as DEFLATE. There are many image and document processing tools online. You can refer to their tutorials for help. Good luck.