Uploading images to this blog can be kinda fiddly. Say I have an image in the windows clipboard (e.g. a screen clip from OneNote and I want it on the blog, I have to fire up a graphics program, paste it in, save it to a file, fire up filezilla, upload it, delete the image file.
This little script does all this for me. It asks for a file name, writes the image in the clipboard to the file, uploads it and deletes the image. Job Done.
The uploading is done using pscp, a version of scp from the putty camp. It uses my putty private key so I don't have to give a password for access to the server (I didn't use the cygwin version of ssh as it didn't like having 'c:\' in file names). The script uses the Python Imaging Library to do the grabbing and wxPython to ask for the file name.
1
2
3
4 import wx
5 import ImageGrab
6 import Image
7 import os
8 import tempfile
9
10 class MyApp(wx.App):
11 def OnInit(self):
12 oImage = ImageGrab.grabclipboard()
13
14 if not isinstance( oImage, Image.Image):
15
16
17
18 dlg = wx.MessageDialog( None, 'No image in clipboard', 'Error', wx.OK)
19 dlg.ShowModal()
20 dlg.Destroy()
21 return True
22
23 dlg = wx.TextEntryDialog( None, 'Enter a file name', 'Upload Clipboard', '')
24 if dlg.ShowModal() != wx.ID_OK:
25 dlg.Destroy()
26 return True
27
28 strName = dlg.GetValue()
29 dlg.Destroy()
30
31 strTempFile = tempfile.mktemp( strName)
32 oImage.save( open( strTempFile, 'wb'))
33
34 strCmd = 'pscp -q -i "c:\my documents\puttykey.ppk" %s me@myserver.com:www/images/%s' % (strTempFile, strName)
35
36 os.system( strCmd)
37
38 os.unlink( strTempFile)
39
40 return True
41
42 app = MyApp(0)
43 app.MainLoop()
This is the result of alt-print screen and running this script:
The script itself inside VIM
ToDo:
-
handle file names in clipboard
-
detect upload errors
-
allow resizing images