Skip to content

Instantly share code, notes, and snippets.

/random_noise.py Secret

Created June 14, 2010 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f6d479f286ad75bf72b7 to your computer and use it in GitHub Desktop.
Save anonymous/f6d479f286ad75bf72b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from random import randint, random
import Image, ImageQt
###############################################################################
class Canvas(QWidget):
WIDTH = 300
def __init__(self, parent=None):
super(QWidget,self).__init__(parent)
self.qwhite_noise = None
self.white_noise = None
self.qsignal = None
self.signal = None
self.square = (randint(0,Canvas.WIDTH-15), randint(0,Canvas.WIDTH-15), 15)
self.bias = 0.25
self.generate_white_noise()
self.setFixedSize(QSize(Canvas.WIDTH*2+1,Canvas.WIDTH))
def generate_white_noise(self):
im = Image.new("L", (Canvas.WIDTH, Canvas.WIDTH))
im.putdata([randint(0,1)*255 for x in im.getdata()])
self.white_noise = im
self.white_noise.save('/tmp/white_noise.bmp')
# self.qwhite_noise = QPixmap.fromImage(ImageQt.ImageQt(im))
self.qwhite_noise = QPixmap('/tmp/white_noise.bmp')
self.redraw_square()
def redraw_square(self):
signal = self.white_noise.copy()
x, y, length = self.square
npixels = length*length
# Generate a square with bias percent of white pixels
nwhite = int(float(npixels)*self.bias + 0.5)
square = [255]*nwhite + [0]*(npixels-nwhite)
# Random pixel permutation
for i in range(nwhite,len(square)):
j = randint(0,i+1)
square[i], square[j] = square[j], square[i]
# Paste the square in the image
sq = Image.new("L", (length,length))
sq.putdata(square)
signal.paste(sq,(x,y))
self.signal = signal
self.signal.save('/tmp/signal.bmp')
# self.qsignal = QPixmap.fromImage(ImageQt.ImageQt(signal))
self.qsignal = QPixmap('/tmp/signal.bmp')
self.repaint()
def paintEvent(self, e):
paint = QPainter(self)
paint.fillRect(self.rect(), QBrush(QColor(250,0,0)))
if self.signal:
paint.drawPixmap(QPoint(0,0),self.qsignal)
paint.drawPixmap(QPoint(Canvas.WIDTH+1,0),self.qwhite_noise)
def randomize_square_location(self):
if self.square:
x, y, len = self.square
x = randint(0, Canvas.WIDTH-len)
y = randint(0, Canvas.WIDTH-len)
self.square = (x, y, len)
print "x = ",x,", y = ",y
self.redraw_square()
def set_length(self, length):
length = max(1, length)
length = min(Canvas.WIDTH, length)
if self.square:
x, y, _ = self.square
x = min(x, Canvas.WIDTH-length)
y = min(y, Canvas.WIDTH-length)
self.square = (x, y, length)
else:
self.square = (0, 0, length)
self.redraw_square()
def set_bias(self, bias):
bias = max(0.0, bias)
bias = min(1.0, bias)
self.bias = bias
self.redraw_square()
###############################################################################
class Window(QWidget):
def __init__(self, title=None, parent = None):
QWidget.__init__(self, parent)
controlsLayout = QVBoxLayout()
self.canvas = Canvas()
controlsLayout.addWidget(self.canvas)
controlsLayout.addWidget(QLabel(self.tr('bias')))
controlsLayout.addWidget(QLabel(self.tr('length')))
self.setLayout(controlsLayout)
def keyPressEvent(self, event):
if type(event) == QKeyEvent:
c = event.key()
if c == Qt.Key_Q:
quit()
event.accept()
elif c == Qt.Key_R:
self.canvas.randomize_square_location()
event.accept()
else:
event.ignore()
###############################################################################
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment