Editorial Workflows

Upload & Insert Relative 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: Modified from [Upload & Insert Image](http://www.editorial-workflows.com/workflow/5890337931264000/qI0pAzIk4Y0).
The folder creation was inspired by [Bible 2](http://www.editorial-workflows.com/workflow/5814178497429504/icZSMb6REcs)

- Change the markdown reference to a relative one for better portability.
- Use shortcut `iii`
- Save to the folder "resources" (automatically created)

Shared by: DavidGogh

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
from time import gmtime, strftime

FOLDER = 'resources'
	
# timestr = time.strftime("%Y-%m-%d")
timestr=strftime("%Y-%m-%dT%H%M%S", gmtime())

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 = 'figure_{}'.format(timestr)

root, rel_doc_path = editor.to_relative_path(editor.get_path())

filename = default_name + '.jpg'

if img:
	pass
	# console.alert(default_name, filename)
	# filename = console.input_alert('test title', 'test message', filename, 'Button')

if not filename:
	workflow.stop()
	
img_data = io.BytesIO()
img.save(img_data, 'jpeg')

rel_doc_dir, fn = os.path.split(rel_doc_path)

local = os.path.join(os.path.expanduser("~"), "Documents", rel_doc_dir)
os.chdir(local)

try:
	os.makedirs(FOLDER)
except:
	console.hud_alert('Already Made')
	
dest_path = os.path.join(rel_doc_dir, FOLDER, filename)
editor.set_file_contents(dest_path, img_data.getvalue(), root)

md_path = os.path.join('./', FOLDER, filename)

workflow.set_output(md_path)
Replace Selected Text ?
Replacement Text
![Image](Input)