Guest User

Untitled

a guest
Oct 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import os
  2. import wx
  3.  
  4. from PIL import Image
  5. from wx.lib.pubsub import pub
  6.  
  7. PhotoMaxSize = 240
  8.  
  9.  
  10. class DropTarget(wx.FileDropTarget):
  11.  
  12. def __init__(self, widget):
  13. wx.FileDropTarget.__init__(self)
  14. self.widget = widget
  15.  
  16. def OnDropFiles(self, x, y, filenames):
  17. print(filenames)
  18.  
  19. image = Image.open(filenames[0])
  20. image.thumbnail((PhotoMaxSize, PhotoMaxSize))
  21. image.save('thumbnail.png')
  22. pub.sendMessage('dnd', filepath='thumbnail.png')
  23. return True
  24.  
  25.  
  26. class PhotoCtrl(wx.App):
  27. def __init__(self, redirect=False, filename=None):
  28. wx.App.__init__(self, redirect, filename)
  29. self.frame = wx.Frame(None, title='Photo Control')
  30.  
  31. self.panel = wx.Panel(self.frame)
  32. pub.subscribe(self.update_image_on_dnd, 'dnd')
  33.  
  34. self.PhotoMaxSize = 240
  35.  
  36. self.createWidgets()
  37. self.frame.Show()
  38.  
  39. def createWidgets(self):
  40. instructions = 'Browse for an image'
  41. img = wx.EmptyImage(240,240)
  42. self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
  43. wx.BitmapFromImage(img))
  44. filedroptarget = DropTarget(self)
  45. self.imageCtrl.SetDropTarget(filedroptarget)
  46.  
  47. instructLbl = wx.StaticText(self.panel, label=instructions)
  48. self.photoTxt = wx.TextCtrl(self.panel, size=(200,-1))
  49. browseBtn = wx.Button(self.panel, label='Browse')
  50. browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)
  51.  
  52. self.mainSizer = wx.BoxSizer(wx.VERTICAL)
  53. self.sizer = wx.BoxSizer(wx.HORIZONTAL)
  54.  
  55. self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
  56. 0, wx.ALL|wx.EXPAND, 5)
  57. self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
  58. self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
  59. self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
  60. self.sizer.Add(browseBtn, 0, wx.ALL, 5)
  61. self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)
  62.  
  63. self.panel.SetSizer(self.mainSizer)
  64. self.mainSizer.Fit(self.frame)
  65.  
  66. self.panel.Layout()
  67.  
  68. def onBrowse(self, event):
  69. """
  70. Browse for file
  71. """
  72. wildcard = "JPEG files (*.jpg)|*.jpg"
  73. dialog = wx.FileDialog(None, "Choose a file",
  74. wildcard=wildcard,
  75. style=wx.OPEN)
  76. if dialog.ShowModal() == wx.ID_OK:
  77. self.photoTxt.SetValue(dialog.GetPath())
  78. dialog.Destroy()
  79. self.onView()
  80.  
  81. def update_image_on_dnd(self, filepath):
  82. self.onView(filepath=filepath)
  83.  
  84. def onView(self, filepath=None):
  85. if not filepath:
  86. filepath = self.photoTxt.GetValue()
  87.  
  88. img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
  89. # scale the image, preserving the aspect ratio
  90. W = img.GetWidth()
  91. H = img.GetHeight()
  92. if W > H:
  93. NewW = self.PhotoMaxSize
  94. NewH = self.PhotoMaxSize * H / W
  95. else:
  96. NewH = self.PhotoMaxSize
  97. NewW = self.PhotoMaxSize * W / H
  98. img = img.Scale(NewW,NewH)
  99.  
  100. self.imageCtrl.SetBitmap(wx.Bitmap(img))
  101. self.panel.Refresh()
  102.  
  103. if __name__ == '__main__':
  104. app = PhotoCtrl()
  105. app.MainLoop()
Add Comment
Please, Sign In to add comment