- Initial steps towards using PIL (if it is there, otherwise PNG)

This commit is contained in:
ccremers 2006-08-04 23:22:03 +00:00
parent a9fa1f0156
commit 51da34c0df
2 changed files with 66 additions and 18 deletions

View File

@ -5,6 +5,12 @@
""" Import externals """ """ Import externals """
import wx import wx
import time import time
# Python Imaging library?
usePIL = True
try:
import Image
except ImportError:
usePIL = False
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
@ -33,7 +39,15 @@ class AttackDisplay(wx.ScrolledWindow):
self.SetSizer(self.hbox) self.SetSizer(self.hbox)
filename = attack.file filename = attack.file
self.original = wx.Image(filename,wx.BITMAP_TYPE_PNG) if attack.filetype == "png":
self.original = wx.Image(filename,wx.BITMAP_TYPE_PNG)
elif attack.filetype == "ps":
# depends on PIL lib
self.original = Image.open(filename)
else:
print "Unknown file type %s." % (self.filetype)
self.update() self.update()
@ -47,16 +61,11 @@ class AttackDisplay(wx.ScrolledWindow):
def update(self): def update(self):
self.SetScrollbars(0,0,0,0,0,0) self.SetScrollbars(0,0,0,0,0,0)
(sh,sw) = self.win.GetClientSizeTuple()
(W,H) = (sw,sh)
bmp = self.original def makefit(H,W):
if not bmp.Ok():
bmp = wx.EmptyBitmap((1,1))
else:
if self.win.fit: if self.win.fit:
W = bmp.GetWidth()
H = bmp.GetHeight()
(sw,sh) = self.win.GetClientSizeTuple()
if W > sw: if W > sw:
# correct width # correct width
factor = float(sw) / W factor = float(sw) / W
@ -67,18 +76,40 @@ class AttackDisplay(wx.ScrolledWindow):
factor = float(sh) / H factor = float(sh) / H
H = sh H = sh
W = W * factor W = W * factor
return (int(H),int(W))
if self.attack.filetype == "png":
bmp = self.original
if not bmp.Ok():
bmp = wx.EmptyBitmap((1,1))
else:
(H,W) = (bmp.GetWidth(), bmp.GetHeight())
if self.win.fit:
(H,W) = makefit(H,W)
bmp = self.original.Scale(W,H)
self.Image.SetBitmap(wx.BitmapFromImage(bmp))
elif self.attack.filetype == "ps":
pil = self.original.copy()
(H,W) = (sh,sw)
# (H,W) = pil.size
# (H,W) = makefit(H,W)
# we really only want antialias when it's smaller
pil.thumbnail((H,W),Image.ANTIALIAS)
image = wx.EmptyImage(pil.size[0],pil.size[1])
image.SetData(pil.convert('RGB').tostring())
self.Image.SetBitmap(image.ConvertToBitmap())
else:
print "Unknown file type %s." % (self.attack.filetype)
bmp = self.original.Scale(W,H)
self.Image.SetBitmap(wx.BitmapFromImage(bmp))
#self.box.SetItemMinSize(self.Image.GetContainingSizer()) #self.box.SetItemMinSize(self.Image.GetContainingSizer())
self.box.Layout() self.box.Layout()
# wx.StaticBitmap(self, -1, bmp, (0, 0), (bmp.GetWidth(), bmp.GetHeight())) # wx.StaticBitmap(self, -1, bmp, (0, 0), (bmp.GetWidth(), bmp.GetHeight()))
step = 20 step = 20
xn = int(bmp.GetWidth() / step) + 1 xn = int(W / step) + 1
yn = int(bmp.GetHeight() / step) + 1 yn = int(H / step) + 1
self.SetScrollbars(step,step,xn,yn,0,0) self.SetScrollbars(step,step,xn,yn,0,0)
self.Refresh() self.Refresh()

View File

@ -4,13 +4,19 @@
""" Import externals """ """ Import externals """
import wx import wx
import wx.lib.newevent
import os import os
import sys import sys
import re import re
import threading import threading
import StringIO import StringIO
# Python Imaging library?
usePIL = True
try:
import Image
except ImportError:
usePIL = False
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
""" Import scyther components """ """ Import scyther components """
@ -94,14 +100,25 @@ class AttackThread(threading.Thread):
def makeImage(self,attack): def makeImage(self,attack):
""" create image for this particular attack """ """ create image for this particular attack """
global usePIL
(fd2,fpname2) = Tempfile.tempcleaned(".png") if usePIL:
pw,pr = os.popen2("dot -Tpng -o%s" % (fpname2)) # If we have the PIL library, we can do postscript! great
# stuff.
type = "ps"
ext = ".ps"
else:
# Ye olde pnge file
type = "png"
ext = ".png"
(fd2,fpname2) = Tempfile.tempcleaned(ext)
pw,pr = os.popen2("dot -T%s -o%s" % (type,fpname2))
pw.write(attack.scytherDot) pw.write(attack.scytherDot)
pw.close() pw.close()
pr.close() pr.close()
attack.file = fpname2 # this is where the file name is stored attack.file = fpname2 # this is where the file name is stored
attack.filetype = "png" attack.filetype = type
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------