Hello all,
I've written and used other's python scripts (You've probably seen it) to print a bunch of photos in *.jpg format and seem to be having an Memory Error problem. After the first image is printed the script loads the next and I get the Memory Error. Each .jpg is between 40 and 75 mb. This script work with smaller (5 to 10mb) .jpg's.
Here's the script. If you have suggestions for better image handling, I'm all ears.
Thanks.
import win32print
import win32ui
import sys, os
import glob, time
import Image, ImageDraw, ImageFont
from PIL import Image, ImageWin
os.chdir ('R:/library/ninebynine/scanned/jpeg/ait/y2007')
img_list = glob.glob('*.jp g')
##def take90():
## time.sleep(90)
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 10
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.GetD efaultPrinter()
##os.char('D:/Ortho/jpg_outputs/sem/quarters/complete')
##img_list = glob.glob('*.jp g')
##file_name = "R:/library/ninebynine/scanned/jpeg/ita/y1995/ita01003.jpg"
##file_name = "D:/Ortho/jpg_outputs/sem/quarters/complete/fil12001_b.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.
for img in img_list:
print(img)
print "Sleeping for 5 minutes"
time.sleep(300)
print "Done sleeping attempting to print photo"
name = img
hDC = win32ui.CreateD C()
hDC.CreatePrint erDC (printer_name)
printable_area = hDC.GetDeviceCa ps (HORZRES), hDC.GetDeviceCa ps (VERTRES)
printer_size = hDC.GetDeviceCa ps (PHYSICALWIDTH) , hDC.GetDeviceCa ps (PHYSICALHEIGHT )
printer_margins = hDC.GetDeviceCa ps (PHYSICALOFFSET X), hDC.GetDeviceCa ps (PHYSICALOFFSET Y)
#
# 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 (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 (name)
hDC.StartPage()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleO utput(), (x1, y1, x2, y2))
hDC.EndPage()
hDC.EndDoc()
hDC.DeleteDC()
Thanks again.
Dennis
I've written and used other's python scripts (You've probably seen it) to print a bunch of photos in *.jpg format and seem to be having an Memory Error problem. After the first image is printed the script loads the next and I get the Memory Error. Each .jpg is between 40 and 75 mb. This script work with smaller (5 to 10mb) .jpg's.
Here's the script. If you have suggestions for better image handling, I'm all ears.
Thanks.
import win32print
import win32ui
import sys, os
import glob, time
import Image, ImageDraw, ImageFont
from PIL import Image, ImageWin
os.chdir ('R:/library/ninebynine/scanned/jpeg/ait/y2007')
img_list = glob.glob('*.jp g')
##def take90():
## time.sleep(90)
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 10
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.GetD efaultPrinter()
##os.char('D:/Ortho/jpg_outputs/sem/quarters/complete')
##img_list = glob.glob('*.jp g')
##file_name = "R:/library/ninebynine/scanned/jpeg/ita/y1995/ita01003.jpg"
##file_name = "D:/Ortho/jpg_outputs/sem/quarters/complete/fil12001_b.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.
for img in img_list:
print(img)
print "Sleeping for 5 minutes"
time.sleep(300)
print "Done sleeping attempting to print photo"
name = img
hDC = win32ui.CreateD C()
hDC.CreatePrint erDC (printer_name)
printable_area = hDC.GetDeviceCa ps (HORZRES), hDC.GetDeviceCa ps (VERTRES)
printer_size = hDC.GetDeviceCa ps (PHYSICALWIDTH) , hDC.GetDeviceCa ps (PHYSICALHEIGHT )
printer_margins = hDC.GetDeviceCa ps (PHYSICALOFFSET X), hDC.GetDeviceCa ps (PHYSICALOFFSET Y)
#
# 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 (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 (name)
hDC.StartPage()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleO utput(), (x1, y1, x2, y2))
hDC.EndPage()
hDC.EndDoc()
hDC.DeleteDC()
Thanks again.
Dennis