I'm trying to print a (or many) large aerial photos converted from tiff's to jpeg's and cannot seem to get it done without a MemoryError message.
These files are between 6 and 15 mb is size.
These images are 9x9 photos with these dimensions:
Width = 11705
Height = 11712
Block Width = 11705

I've been able to print 2 and 3 mb file size images with these dimensions:
Width, Height and block Width about = 7500

I'm not sure if it's the file size or the dimensions that's giving me trouble.

I'm using the code from Tim's Golden Python scripts with a few alterations.

import win32print
import win32ui
import glob, shutil, datetime, time
import sys, os
import Image, ImageDraw, ImageFont
from PIL import Image, ImageWin
import ImageOps
from path import path

# 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.GetDefaultPrinter()

d = path(print_jpeg_dir)
for f in d.walkfiles('*.jpg'):
scan = f.splitpath()[-1]
name = f
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 (name)
## bmp.show()
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.GetHandleOutput(), (x1, y1, x2, y2))

hDC.EndPage()
hDC.EndDoc()
hDC.DeleteDC()

This should be an easy script but, I just can't get it to work.
Any ideas?
Thanks.
-Dennis

Recommended Answers

All 7 Replies

These files are between 6 and 15 mb is size.

That's not very large by today's standards so I doubt that "MemoryError message" means running out of memory. Try running the program with a try/except and maybe you will get error messages that show what and where the error is, as not much can be done with what was presented.

Have you steped through the script to see at what point the error is being thrown? Try adding

import sys

and the line

print ("Unexpected error:", sys.exc_info()[0])

for your 'except' section. It should give you a better answer that just 'Memory Error'

Here's the Traceback:
The python script in <module>
dib = ImageWin.Dib (bmp)
File "D:\Python25\lib\site-packages\PIL\ImageWin.py", line70, in init
self.image = Image.core.display(mode, size)
MemoryError

Sounds funny, but did you try running it with a couple of small images?, just to verify your code operates properly? Maybe a couple pics from the web under 512k?

Just printed a 3mb image and it worked fine.

It looks like it has something to do with the bmp portion of the script. Of course I'm new to python and don't quite know what this bmp part of the script is doing.

It is hanging up on this line of the code:
dib = ImageWin.Dib (bmp)

Which opens the ImageWin.py and the self.image = Image.core.display(mode, size) error occures.

Added the print("unexpected error:", sys.exc_info()[0])
Just tried to print a file 6.5mb and recieved this error:
('Unexpected error:', <type 'exceptions.MemoryError'>)

Thanks.

Looks to me at least that according to the error spec, that really is an out of memory condition.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.