SHARE
TWEET

IconEditor.py

a guest Nov 11th, 2014 1,845 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. from Tkinter import *
  3.  
  4. W, H = 84, 48
  5.  
  6. def hexf(x):
  7.   return hex(x)[2:].upper().rjust(2, "0")
  8.  
  9. class Application(Frame):
  10.   def __init__(self, master = None):
  11.     Frame.__init__(self, master)
  12.     self.pack()
  13.     self.canvas = Canvas(self, width = 588, height = 336, highlightthickness = 0)
  14.     self.canvas.bind("<Button-1>", self.click)
  15.     self.canvas.pack(padx = 5, pady = 5)
  16.     frame = Frame(self)
  17.     frame.pack()
  18.     Label(frame, text = "Width:").pack(padx = 5, pady = 5, side = LEFT)
  19.     self.sw = Spinbox(frame, values = [i for i in xrange(W, 0, -1)], width = 4, state = "readonly")
  20.     self.sw.pack(padx = 5, pady = 5, side = LEFT)
  21.     Label(frame, text = "Height:").pack(padx = 5, pady = 5, side = LEFT)
  22.     self.sh = Spinbox(frame, values = [i for i in xrange(H, 0, -1)], width = 4, state = "readonly")
  23.     self.sh.pack(padx = 5, pady = 5, side = LEFT)
  24.     Button(frame, text = "Apply", command = self.resize).pack(padx = 5, pady = 5, side = LEFT)
  25.     frame = Frame(self)
  26.     frame.pack(padx = 5, fill = X)
  27.     scrollbar = Scrollbar(frame)
  28.     scrollbar.pack(side = RIGHT, fill = Y)
  29.     self.text = Text(frame, font = ('times', 12), width = 50, height = 5, wrap = WORD, yscrollcommand = scrollbar.set)
  30.     self.text.pack(fill = X)
  31.     scrollbar.config(command = self.text.yview)
  32.     frame = Frame(self)
  33.     frame.pack()
  34.     Button(frame, text = "Load", command = self.load).pack(padx = 5, pady = 5, side = LEFT)
  35.     Button(frame, text = "Save", command = self.save).pack(padx = 5, pady = 5, side = LEFT)
  36.     self.area, self.pix = [], []
  37.     self.w, self.h, self.l = W, H, (W+7)/8
  38.     for y in xrange(H):
  39.       for x in xrange(W):
  40.         self.area.append(self.canvas.create_rectangle(x*7, y*7, x*7+6, y*7+6, fill = "white", width = 0))
  41.         self.pix.append(0)
  42.  
  43.   def load(self):
  44.     txt = self.text.get('1.0', END)
  45.     txt = txt.strip()
  46.     if not txt:
  47.       return
  48.     self.text.delete('1.0', END)
  49.     try:
  50.       txt += ","
  51.       txt = eval("(%s)"%txt)
  52.       txt = [bin(i)[2:].rjust(8, "0") for i in txt]
  53.     except:
  54.       print "wrong byte array format"
  55.       return
  56.     if len(txt) != self.h*self.l:
  57.       print "wrong byte array lenght"
  58.       return
  59.     for x, y, xx, yy, index, tag in self.walk():
  60.       bit = txt[yy*self.l+xx/8][xx%8]
  61.       self.pix[index] = int(bit)
  62.       color = "black" if self.pix[index] else "white"
  63.       self.canvas.itemconfig(tag, state = NORMAL, fill = color)
  64.  
  65.   def walk(self):
  66.     index = 0
  67.     for y in xrange(H):
  68.       for x in xrange(W):
  69.         tag = self.area[index]
  70.         if ((x < (W-self.w)/2.0) or (x >= (W-self.w)/2.0+self.w)) or ((y < (H-self.h)/2.0) or (y >= (H-self.h)/2.0+self.h)):
  71.           self.canvas.itemconfig(tag, state = HIDDEN)
  72.         else:
  73.           xx, yy = x-int((W-self.w)/2.0+0.5), y-int((H-self.h)/2.0+0.5)
  74.           yield x, y, xx, yy, index, tag
  75.         index += 1
  76.  
  77.  
  78.   def save(self):
  79.     self.text.delete('1.0', END)
  80.     txt = [["0", "0", "0", "0", "0", "0", "0", "0"] for i in xrange(self.h*self.l)]
  81.     for x, y, xx, yy, index, tag in self.walk():
  82.       txt[yy*self.l+xx/8][xx%8] = str(self.pix[index])
  83.     txt = [int("".join(i), 2) for i in txt]
  84.     txt = ["0x%s"%hexf(i) for i in txt]
  85.     tmp = []
  86.     for i in xrange(0, len(txt), self.l):
  87.       tmp.append(", ".join(txt[i:i+self.l]))
  88.     txt = ",\n".join(tmp)
  89.     self.text.insert('1.0', txt)
  90.  
  91.   def resize(self):
  92.     w, h = int(self.sw.get()), int(self.sh.get())
  93.     if self.w == w and self.h == h:
  94.       return
  95.     self.w, self.h, self.l = w, h, (w+7)/8
  96.     for x, y, xx, yy, index, tag in self.walk():
  97.       self.canvas.itemconfig(tag, state = NORMAL, fill = "white")
  98.       self.pix[index] = 0
  99.  
  100.   def click(self, event):
  101.     tag = self.canvas.find_withtag(CURRENT)
  102.     if not tag:return
  103.     tag = tag[0]
  104.     if self.canvas.type(tag) == "rectangle" and tag in self.area:
  105.       index = self.area.index(tag)
  106.       color = "white" if self.pix[index] else "black"
  107.       self.canvas.itemconfig(tag, fill = color)
  108.       self.pix[index] ^= 1
  109.  
  110.  
  111. root = Tk()
  112. app = Application(master = root)
  113. app.master.title("Icon Editor")
  114. app.master.resizable(width = FALSE, height = FALSE)
  115. app.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top