help with PIL


(glitchCORD~) #1

hi,
im hoping someonw whos familiar with PIL [and older versions of PIL] can help me.
ive got a program[script] running in the 2.37a GameEngine which automatically prints to my printer when a variable equals something. i want it too print when my framerate falls below a certain point, to do this ive had to recode in 2.25 publisher due to something to do with logic and physics[not too clear on it myself].

as 2.25publisher uses python 2.0 ive had to get older versions of win32ui and PIL. everything works fine except something in PIL[version 1.1.1]

heres the code


if own.printer == 1:
	import win32print
	import win32ui
	from PIL import Image, ImageWin
	
	import Rasterizer
	file_name = "test_a%04d.jpg" % own.printer_count
	#####filename = "anim%04d.tga" % obj.frame
	Rasterizer.makeScreenshot(file_name)
	
	#
	# Constants for GetDeviceCaps 
	#
	#
	# HORZRES / VERTRES = printable area
	#
	HORZRES = 8
	VERTRES = 10
	#
	# LOGPIXELS = dots per inch
	#
	LOGPIXELSX = 88
	LOGPIXELSY = 90
	#
	# PHYSICALWIDTH/HEIGHT = total area
	#
	PHYSICALWIDTH = 110
	PHYSICALHEIGHT = 111
	#
	# PHYSICALOFFSETX/Y = left / top margin
	#
	PHYSICALOFFSETX = 112
	PHYSICALOFFSETY = 113
	
	printer_name = win32print.GetDefaultPrinter ()
	####file_name = "test.jpg"
	
	#
	# You can only write a Device-independent bitmap
	#  directly to a Windows device context; therefore
	#  we need (for ease) to use the Python Imaging
	#  Library to manipulate the image.
	#
	# Create a device context from a named printer
	#  and assess the printable size of the paper.
	#
	hDC = win32ui.CreateDC ()
	hDC.CreatePrinterDC (printer_name)
	printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
	printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
	printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
	
	#
	# Open the image, rotate it if it's wider than
	#  it is high, and work out how much to multiply 
	#  each pixel by to get it as big as possible on 
	#  the page without distorting.
	#
	bmp = Image.open (file_name)
	if bmp.size[0] > bmp.size[1]:
	  bmp = bmp.rotate (90)
	ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
	scale = min (ratios)
	
	#
	# Start the print job, and draw the bitmap to
	#  the printer device at the scaled size.
	#
	hDC.StartDoc (file_name)
	hDC.StartPage ()
	
	dib = ImageWin.Dib (bmp)
	scaled_size = tuple ([scale * i for i in bmp.size])
	x = (printer_size[0] - scaled_size[0]) / 2
	y = (printer_size[1] - scaled_size[1]) / 2
	dib.draw (hDC.GetHandleOutput (), (x, y) + scaled_size)
	
	hDC.EndPage ()
	hDC.EndDoc ()
	
	own.printer_count +=1
	own.printer=0

this is part of my fullsc ript[ the printing bit] it runs fine in 2.37 with latest PIL and win32ui[pythonwin]. but using publisher and the Pil 1.1.1 i get an error relating to this line


	dib = ImageWin.Dib (bmp)

saying that
PYTHON SCRIPT ERROR:
Traceback (most recent call last):
File “Text.py”, line 152, in ?
TypeError: not enough arguments; expected 3, got 2

does anyone know what changed between PIL 1.1.1 and now [ a lot i guess] so that i can figure what i have to put in the above line to get my program running, thanks

or does anyone know where i can get a more recent version of PIL compiled for python 2.0
will


(IanC) #2

Have you tried

dib = ImageWin.Dib (bmp,None) 

?

Far as I can tell, that should be automatic, but try it anyway.

Edit - read your code a bit more, and I think its just that you need a second argument, the second argument (None by default, supposedly) should be the size of the image. None might not work, therefore.

Ian


(glitchCORD~) #3

hi Bulletdodge, that seems to work, thnakyou, i also got it working with (1,1) instead of None, which i guess is a form of scaling.

Cheers


(IanC) #4

No worries, if the code runs but doesnt do as expected, then it probably needs a different value there. Play around and find one that works.

Ian