SWendel-esristaff

How to rotate image Feature attachments on a Hosted Feature Service.

Blog Post created by SWendel-esristaff Employee on Aug 17, 2017

Have you ever attached an image to your hosted feature service but then realized it's not rotated correctly? 

 

With the following script sample, you can rotate your image and re-upload it in one script!

 

Pre-requisites:

Install Python 2.7.x (Use what comes with your ArcGIS Desktop install  ) 

Install the Image Module: The Image Module 

Install the Requests Module: Requests: HTTP for Humans — Requests 2.18.4 documentation 

 

Both can be installed using pip. 

 

Code sample to adjust our attachments: 

 

import requests
from PIL import Image
from StringIO import StringIO
import json

attachmentUrl = raw_input("URL including token to image that needs to be rotated: ")
rotateby = int(raw_input("Rotate Counterclockwise by: "))

featureURL = attachmentUrl.split("/attachments")[0]
serviceURL = attachmentUrl.rsplit("/", 3)[0]
attachmentID = ((attachmentUrl.split("/attachments")[1]).split('?token=')[0])[1:]
token = (attachmentUrl.split("/attachments")[1]).split('?token=')[1]
queryAttachmentsRequest = requests.get("{}/queryAttachments".format(serviceURL), params={'f':'json', 'token': token, 'objectIds': attachmentID})
name = json.loads(queryAttachmentsRequest.text)['attachmentGroups'][0]['attachmentInfos'][0]['name']
imgName = name.split('.')[0] + "_rotated.jpg"
imgTemp = r"C:/temp/" + imgName
attachmentRequest = requests.get(attachmentUrl)
img = Image.open(StringIO(attachmentRequest.content))
img2 = img.rotate(rotateby)
img2.save(imgTemp)
file = {'file': (imgName, open(imgTemp, 'rb'), 'image/jpg')}
addAttachmentURL = "{0}/addAttachment".format(featureURL)
params = {'f':'json','token':token}
addAttachmentRequest = requests.post(addAttachmentURL, files=file, data=params)
jsonresponse = addAttachmentRequest.text
print jsonresponse

 

The attachment URL can be gathered by opening the attachment through the map viewer. You should see the URL looks something like this:

http://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/Stephanie_Locations/FeatureServer/0/65/attachments/64?token=<OwnerToken>

 

Next you just need to provide the amount you want to rotate counterclockwise.

90 - Bottom right corner becomes top right corner.

180 - Bottom right corner becomes top left corner.

270 - Bottom right corner becomes bottom left corner.

 

I hope this gives you a starting place to start rotating image attachments! 

Outcomes