Editorial Workflows

insert_image

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: ###insert_image.py

- Prompt for image selection from the camera roll.
- Save selection as as a jpeg in the current document's directory using the current date as filename.
- Aappend an incrementing number if multiple images are inserted.
- Preserve the original image orientation.
- Insert a markdown image reference.

**Credits**

The workflow is forked from [Insert Image... ](http://www.editorial-workflows.com/workflow/5896882052136960/tbnRcIh3c38) written by [@pastrunho](http://twitter.com/pastrunho). The code for orientation was found in post from [scabbiaza](http://stackoverflow.com/users/1350976/scabbiaza) on [StackOverflow](http://stackoverflow.com/questions/13872331/rotating-an-image-with-orientation-specified-in-exif-using-python-without-pil-in). Putting it together and any other modifications and/or creations of code come from me.

https://github.com/matthewdunlap

Shared by: @matthew__dunlap

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
A document has to be open... ?
Run the block if
File Name
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
ON
Message
No Document Selected
…End If
Run Python Script ?
Source Code
#coding: utf-8

from PIL import Image, ExifTags
import photos
import workflow
import console
import editor
import os
import io
import time

timestr = time.strftime("%Y-%m-%d")

img = photos.pick_image()
if not img:
	workflow.stop()

try:
	for orientation, value in ExifTags.TAGS.items():
		if value == 'Orientation':
			break
	exif = dict(img._getexif().items())
	rot_degrees = {3: 180, 6: 270, 8: 90}.get(exif[orientation], 0)
	if rot_degrees:
		img = img.rotate(rot_degrees, expand=True)
# cases: image don't have getexif
except (AttributeError, KeyError, IndexError):
	pass

doc_path = editor.get_path()
doc_dir, fn = os.path.split(doc_path)
default_name = '{}_image'.format(timestr)

i = 1
while True:
	if not os.path.exists(os.path.join(doc_dir, default_name + '.jpg')):
		break
	default_name = '{}_image_{}'.format(timestr, i)
	i += 1

root, rel_doc_path = editor.to_relative_path(editor.get_path())
filename = default_name + '.jpg'

if not filename:
	workflow.stop()

img_data = io.BytesIO()
img.save(img_data, 'jpeg')

rel_doc_dir, fn = os.path.split(rel_doc_path)
dest_path = os.path.join(rel_doc_dir, filename)
editor.set_file_contents(dest_path, img_data.getvalue(), root)
workflow.set_output(filename)
Replace Selected Text ?
Replacement Text
![Image](Input)