gif to bitmap in python

Steve Holden sholden at holdenweb.com
Fri Sep 1 01:52:54 CEST 2000


Jason Toy wrote:
> 
> That's all to convert gif to bitmap???Cool..  I suppose you can use pil to
> convert jpegs too?  Can pil make thumbnails?
> 
Below find a Piddle-based (with pil image handling) program which I
use to produce PDF printouts of the photos from my digital camera.  It's
all of 46 lines long, without much code optimization.

I found that scaling in Piddle was much slower than in pil, but that
resolution of the thumbnails was too granular if I just scaled in pil.

pil is great.

regards
 Steve

from piddle import *
from piddlePDF import *
import Image, os.path, sys, glob

MAXLINE = 8
MAXCOL = 4
LINEHEIGHT = 100
COLWIDTH = 140
LEFTMARGIN = 45
TOPMARGIN = 45

fntLarge = Font(face="helvetica", size=16, bold=1)
fntMedium = Font(face="helvetica", size=12, bold=1)
fntSmall = Font(face="helvetica", size=8, bold=0)

imagename = []
for filepat in sys.argv[1:]:
	for infile in glob.glob(filepat):
		imagename.append(infile)

imagename.sort()
lineno = MAXLINE+1
colno = MAXCOL+1
pageno = 0

out = PDFCanvas(name = "PhotoClips.pdf")

for i in range(len(imagename)):
	if colno >= MAXCOL:
		lineno = lineno+1
		colno = 0
	if lineno >= MAXLINE:
		lineno = 0
		colno = 0
		if pageno > 0: out.clear()
		pageno = pageno +1
	print imagename[i]; sys.stdout.flush()
	im = Image.open(imagename[i])
	imx = colno*COLWIDTH+LEFTMARGIN
	imy = lineno*LINEHEIGHT+TOPMARGIN
	out.drawImage(im.resize((220,146)), imx, imy, imx+109, imy+72)
	out.drawString(os.path.basename(imagename[i]),
		imx, imy+80, fntSmall)
	colno = colno +1

out.flush()


> Greg Landrum wrote:
> 
> > In article <8oghku$a9t$1 at nnrp1.deja.com>,
> >   tiddlerdeja at my-deja.com wrote:
> > > Can anyone tell me how, or give an example of howto convert a gif
> > image
> > > to a bitmap representation of it?
> > >
> >
> > I'm not completely sure what you mean by "bitmap representation", but
> > PIL probably does what you want.  :-)
> >
> > To convert to a BMP (windows bitmap), you would do:
> > >>> from Pil import Image
> > >>> img = Image.open('foo.gif')
> > >>> img.save('foo.bmp')
> >
> > If you would rather get a Numeric array with the values from the image:
> > >>> from Pil import Image
> > >>> from Numeric import *
> > >>> img = Image.open('foo.gif')
> > >>> arr = resize(fromstring(img.tostring(),Int8),img.size)
> >
> > I hope this helps.
> > -greg
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/



More information about the Python-list mailing list