<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://pzwiki.wdka.nl/mw-mediadesign/index.php?action=history&amp;feed=atom&amp;title=MotionImage</id>
	<title>MotionImage - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://pzwiki.wdka.nl/mw-mediadesign/index.php?action=history&amp;feed=atom&amp;title=MotionImage"/>
	<link rel="alternate" type="text/html" href="https://pzwiki.wdka.nl/mw-mediadesign/index.php?title=MotionImage&amp;action=history"/>
	<updated>2026-08-05T21:55:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>https://pzwiki.wdka.nl/mw-mediadesign/index.php?title=MotionImage&amp;diff=3488&amp;oldid=prev</id>
		<title>Michael Murtaugh: New page: Based on code of MotionBox, now we use images captured by Motion, plus the &quot;movement rectangle&quot; data to dynamically display the image data in PyGame.  Media:Motionimage.zip  &lt;s...</title>
		<link rel="alternate" type="text/html" href="https://pzwiki.wdka.nl/mw-mediadesign/index.php?title=MotionImage&amp;diff=3488&amp;oldid=prev"/>
		<updated>2009-06-01T10:11:40Z</updated>

		<summary type="html">&lt;p&gt;New page: Based on code of &lt;a href=&quot;/mediadesign/MotionBox&quot; title=&quot;MotionBox&quot;&gt;MotionBox&lt;/a&gt;, now we use images captured by &lt;a href=&quot;/mediadesign/Motion&quot; title=&quot;Motion&quot;&gt;Motion&lt;/a&gt;, plus the &amp;quot;movement rectangle&amp;quot; data to dynamically display the image data in PyGame.  &lt;a href=&quot;/mw-mediadesign/index.php?title=Special:Upload&amp;amp;wpDestFile=Motionimage.zip&quot; class=&quot;new&quot; title=&quot;Motionimage.zip&quot;&gt;Media:Motionimage.zip&lt;/a&gt;  &amp;lt;s...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Based on code of [[MotionBox]], now we use images captured by [[Motion]], plus the &amp;quot;movement rectangle&amp;quot; data to dynamically display the image data in PyGame.&lt;br /&gt;
&lt;br /&gt;
[[Media:Motionimage.zip]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm *.jpg&lt;br /&gt;
motion -c motionimage.conf | python -u motionimage.py&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
&lt;br /&gt;
import sys&lt;br /&gt;
import pygame&lt;br /&gt;
from pygame.locals import *&lt;br /&gt;
from pygame.time import Clock&lt;br /&gt;
from time import sleep&lt;br /&gt;
import thread&lt;br /&gt;
&lt;br /&gt;
def scaleToFitBox(size, box):&lt;br /&gt;
	&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	size should be a tuple (pair) of form (w, h)&lt;br /&gt;
	box is also a tuple (bw, bh)&lt;br /&gt;
	returns a new tuple of a size,&lt;br /&gt;
	preserving the ratio of size, but fitting in the box&lt;br /&gt;
	(scaling up or down as needed)&lt;br /&gt;
	&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	(sw, sh) = size&lt;br /&gt;
	(bw, bh) = box&lt;br /&gt;
	# nw, nh represent the &amp;quot;new&amp;quot; width and height to return&lt;br /&gt;
	# 1. try fitting box width&lt;br /&gt;
	nw = bw&lt;br /&gt;
	# nh can be calculated based on the aspect ratio of the sourcea&lt;br /&gt;
	nh = int((float(sh)/sw)*nw)&lt;br /&gt;
	# fit the box?&lt;br /&gt;
	if (nh &amp;lt;= bh):&lt;br /&gt;
		# if so, return it&lt;br /&gt;
		return (nw, nh)&lt;br /&gt;
	else:	&lt;br /&gt;
		# two big? then fit the height&lt;br /&gt;
		nh = bh&lt;br /&gt;
		# nw can be calculated based on the aspect ratio of the sourcea&lt;br /&gt;
		nw = int((float(sw)/sh)*nh)&lt;br /&gt;
		return (nw, nh)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
	mfullimage = None&lt;br /&gt;
	mimage = None&lt;br /&gt;
	(mx, my, mw, mh) = (10, 10, 50, 50)&lt;br /&gt;
	pygame.init()&lt;br /&gt;
	# screen = pygame.display.set_mode((640, 480), FULLSCREEN, 32)&lt;br /&gt;
	screen = pygame.display.set_mode((640, 480), 0, 32)&lt;br /&gt;
	pygame.display.set_caption(&amp;quot;motionbox&amp;quot;)&lt;br /&gt;
	clock = Clock()&lt;br /&gt;
	while True:&lt;br /&gt;
		# TIMING&lt;br /&gt;
		clock.tick(30)&lt;br /&gt;
		&lt;br /&gt;
		# PROCESS EVENTS&lt;br /&gt;
		for event in pygame.event.get():&lt;br /&gt;
			if event.type==QUIT or \&lt;br /&gt;
			(event.type == KEYDOWN and event.key == K_ESCAPE):&lt;br /&gt;
				sys.exit()&lt;br /&gt;
			elif event.type == KEYDOWN and event.key == K_f:&lt;br /&gt;
				# http://www.pygame.org/docs/ref/display.html#pygame.display.toggle_fullscreen&lt;br /&gt;
				pygame.display.toggle_fullscreen()&lt;br /&gt;
			elif event.type == USEREVENT:&lt;br /&gt;
				mx = event.x&lt;br /&gt;
				my = event.y&lt;br /&gt;
				mw = event.w&lt;br /&gt;
				mh = event.h&lt;br /&gt;
				mx -= mw/2&lt;br /&gt;
				my -= mh/2&lt;br /&gt;
				mfilename = event.filename&lt;br /&gt;
				mfullimage = pygame.image.load(mfilename).convert()&lt;br /&gt;
				# &amp;quot;crop&amp;quot; the full image by creating a new surface of the right size&lt;br /&gt;
				# and using the blit command to copy the interesting rect to it&lt;br /&gt;
				subimage = pygame.Surface((mw, mh), 0, mfullimage)&lt;br /&gt;
				subimage.blit(mfullimage, (0, 0), (mx, my, mw, mh))&lt;br /&gt;
				# mimage = mfullimage.subsurface((mx, my, mw, mh))&lt;br /&gt;
				(nw, nh) = scaleToFitBox((mw, mh), (640, 480))&lt;br /&gt;
				subimage = pygame.transform.scale(subimage, (nw, nh))&lt;br /&gt;
				# figure out new position to draw the scaled up image&lt;br /&gt;
				# (simply center the image based on it's new size)&lt;br /&gt;
				mx = (640 - nw) / 2&lt;br /&gt;
				my = (480 - nh) / 2&lt;br /&gt;
		#mx += 1&lt;br /&gt;
		#if (mx &amp;gt; 500): mx = 0&lt;br /&gt;
		screen.fill((0, 0, 0))&lt;br /&gt;
		# draw rectangle using current position &amp;amp; size&lt;br /&gt;
		if mfullimage:&lt;br /&gt;
			#screen.blit(mfullimage, (0,0))&lt;br /&gt;
			# screen.blit(mfullimage, (mx, my), (mx, my, mw, mh))&lt;br /&gt;
			screen.blit(subimage, (mx, my))&lt;br /&gt;
			# draw test rect&lt;br /&gt;
			#pygame.draw.rect(screen, (255, 0, 0), (mx, my, mw, mh), 1)&lt;br /&gt;
		&lt;br /&gt;
		pygame.display.update()&lt;br /&gt;
		# sleep(0.01)&lt;br /&gt;
&lt;br /&gt;
# start pygame in a separate thread&lt;br /&gt;
# starting a thread is like running a program with &amp;amp; on the shell&lt;br /&gt;
thread.start_new_thread(main, ())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# READ INFO FROM STDIN&lt;br /&gt;
while 1:&lt;br /&gt;
	line = sys.stdin.readline()&lt;br /&gt;
	if (not line): break&lt;br /&gt;
	line = line.strip()&lt;br /&gt;
	try:&lt;br /&gt;
		if line.startswith(&amp;quot;image&amp;quot;):&lt;br /&gt;
			# print &amp;quot;read image data:&amp;quot;, line&lt;br /&gt;
			(x, y, w, h, numpixels, filename) = line.split()[1:]&lt;br /&gt;
			x = int(x)&lt;br /&gt;
			y = int(y)&lt;br /&gt;
			w = int(w)&lt;br /&gt;
			h = int(h)&lt;br /&gt;
			# create a custom &amp;quot;event&amp;quot; and send it (post it) to the pygame loop&lt;br /&gt;
			evt = pygame.event.Event(USEREVENT, {&amp;quot;x&amp;quot; : x, &amp;quot;y&amp;quot;: y, &amp;quot;w&amp;quot;:w, &amp;quot;h&amp;quot;:h, &amp;quot;filename&amp;quot;: filename})&lt;br /&gt;
			pygame.event.post(evt)&lt;br /&gt;
	except ValueError:&lt;br /&gt;
		print &amp;quot;bad image line&amp;quot;, line&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Michael Murtaugh</name></author>
	</entry>
</feed>