def agrega_entrada2(codigo, sector, tipo_foto, segmento):
	imagen_ref = ajustar(tipo_foto, alto=160)
	print imagen_ref
	"""
	if segmento not in sectores and segmento not in [u"Galería Everton", u"Galería Visita"]:
		imagen_ref = cort[tipo_foto]
	else:
		imagen_ref = foto[tipo_foto]
	"""
	alto_total = imagen_ref.size[1] + 20

	foto_codigo = Code128b.code128_image(codigo)
	
	foto_codigo = ajustar(foto_codigo, alto=alto_total)
	ancho_datos = foto_codigo.size[0]/2
	alto_recuadro = foto_codigo.size[1] + 30
	ancho_recuadro = foto_codigo.size[0] + 50
	margen_sup = 10

	margen_recuadro = 24
	alto_recuadro = foto_codigo.size[1] + 15
	ancho_recuadro = foto_codigo.size[0] + 50

	inicio_recuadro = (foto_codigo.size[0] + margen_recuadro, margen_recuadro)
	fin_recuadro = (inicio_recuadro[0] + ancho_recuadro, inicio_recuadro[1] + alto_recuadro)

	ancho_datos = 350
	alto_datos = foto_codigo.size[1] + 30

	inicio_datos = (fin_recuadro[0] + 5, inicio_recuadro[1])
	fin_datos = (inicio_datos[0] + ancho_datos, inicio_datos[1] + alto_datos)

	ancho_total = imagen_ref.size[0] + foto_codigo.size[0] + ancho_datos + margen_recuadro*2 + ancho_recuadro


	lienzo = Image.new("RGBA", (ancho_total, alto_total), (255,255,255,255))


	lienzo.paste(foto_codigo, (0, 24))
	lienzo.paste(imagen_ref, (ancho_total - imagen_ref.size[0], 20))

	# agregar texto

	drawer = ImageDraw.Draw(lienzo)
	

	margen_lat = int((ancho_recuadro - fuente.getsize(sector.upper())[0])/2)
	

	COLOR = color_recuadro[sector]
	if segmento == u"Galería Visita":
		COLOR = color_recuadro[segmento]

	drawer.rectangle([inicio_recuadro, fin_recuadro], fill=COLOR)

	drawer.text((inicio_recuadro[0] +  margen_lat, inicio_recuadro[1] + margen_sup), sector.upper(), font=fuente)

	if segmento not in sectores:
		margen_sup = margen_sup + 50
		margen_lat = int((ancho_recuadro - fuente_chica.getsize(segmento.upper())[0]) / 2)
		drawer.text((inicio_recuadro[0] + margen_lat, inicio_recuadro[1] + margen_sup), segmento.upper(), font=fuente_chica, fill=(255,255,255,255))

		if segmento not in [u"Galería Everton", u"Galería Visita"]:
			texto_cortesia = "Prohibida su venta".upper()
			margen_sup = fin_recuadro[1] + 5
			margen_lat = inicio_recuadro[0] + int((ancho_recuadro - fuente_serie.getsize(texto_cortesia)[0]) / 2)
			drawer.text((margen_lat, margen_sup), texto_cortesia, font=fuente_serie, fill=(0,0,0,255))

	# datos evento

	

	datos = descripcion.split("\n")

	margen_izq = inicio_datos[0] + int(ancho_datos - fuentes_datos[0].getsize(datos[0])[0]) / 2
	margen_sup = inicio_datos[1]

	drawer.text((margen_izq, margen_sup), datos[0], font=fuentes_datos[0], fill=COLOR)

	for i in range(1,len(datos)):
		margen_sup += 30
		margen_izq = inicio_datos[0] + int(ancho_datos - fuentes_datos[1].getsize(datos[i])[0]) / 2

		drawer.text((margen_izq, margen_sup), datos[i], font=fuentes_datos[1], fill=color_recuadro[sector])	


	# numero de serie

	global SERIE

	texto_serie = "%04d" % SERIE

	foto_serie = Image.new("RGBA", (120 + 48, 30), (255,255,255,0))

	wr = ImageDraw.Draw(foto_serie)

	marg =  int((117  + 48 - fuente_serie.getsize(texto_serie)[0]) / 2)

	wr.text((marg,0), texto_serie, font=fuente_serie, fill=(0,0,0,255))

	foto_serie = foto_serie.rotate(90, expand=True)

	wr = ImageDraw.Draw(foto_serie)

	wr.rectangle((0, 0, foto_serie.size[0]+1, 3), outline=(255,255,255,255), fill=(255,255,255,255))
	

	lienzo.paste(foto_serie, (0,0))


	SERIE += 1

	achique = 1

	return lienzo.resize((int(lienzo.size[0]*achique), int(lienzo.size[1]*achique)))
alto_letra = 0.4


def ajustar(img,**kwargs):
	w, h = img.size
	factor = 1
	if "alto" in kwargs:
		factor = kwargs["alto"] / h
		return img.resize((int(w * factor), int(h * factor)))
	elif "ancho" in kwargs:
		factor = kwargs["ancho"] / w
		return img.resize((int(w * factor), int(h * factor)))
	else:
		raise Exception("no argument found")

codigo_nino  = ajustar(Code128b.code128_image("0019701"), ancho=ancho_codigo*dpi)
codigo_socio = ajustar(Code128b.code128_image("0019801"), ancho=ancho_codigo*dpi)
codigo_cred  = ajustar(Code128b.code128_image("0019901"), ancho=ancho_codigo*dpi)

logo = ajustar(Image.open("logo.jpg"),ancho=ancho_logo*dpi )

lienzo = Image.new("RGB", (int(ancho_pag * dpi), int(alto_pag * dpi)), (255,255,255))

lienzo.paste(logo, (lienzo.size[0] - logo.size[0], 0))

fuente = ImageFont.truetype("arial.ttf", int(alto_letra * dpi))

drawer = ImageDraw.Draw(lienzo)

margen_izq = int((ancho_pag*dpi - fuente.getsize(u"Niño")[0]) / 2)
margen_sup = int(0.2 * dpi)
Example #3
0
    def generaImagen(self,
                     barcode,
                     segmento,
                     fechaventa,
                     debug=False,
                     nombreEvento=None):
        # TODO: Ordenar esta cosa
        barcode_img = Code128b.code128_image(str(barcode), self.bheight,
                                             self.bthickness)

        lienzo = Image.new("1", (1710 + 780, 300), 1)

        imgweb = Image.new("1", (300, 30), 1)

        drawer = ImageDraw.Draw(imgweb)

        margen = int((300 - self.font_web.getsize("www.handband.cl")[0]) / 2)

        drawer.text((margen, 0), "www.handband.cl", font=self.font_web)

        imgweb2 = imgweb.rotate(90, expand=True).crop((1, 0, 31, 300))
        imgweb2.load()
        imgweb = imgweb2

        lienzo.paste(imgweb, (0, 0, 30, 300))

        w, h = barcode_img.size
        b = barcode_img.convert("1")
        achique = 0.66
        fact = (300.0 * achique) / h
        b = b.resize((int(w * fact), int(h * fact)))
        w, h = b.size
        topm = (lienzo.size[1] - h) / 2
        # pegar codigo de barras

        lienzo.paste(b, (30, topm, w + 30, h + topm))
        # pegar asignacion (normal-pref-vip)
        asignacion = Image.new("1", (500, 300), 1)
        drawer = ImageDraw.Draw(asignacion)

        if nombreEvento is None:
            tipo_asignacion = self.segmentos[segmento]
        else:
            tipo_asignacion = nombreEvento

        alt = int((300 - 80) / 2)
        drawer.text((0, alt), tipo_asignacion, font=self.font)

        lienzo.paste(asignacion, (30 + w + 10, 0, 30 + w + 510, 300))

        #pegar logo

        lienzo.paste(self.logo_img,
                     (30 + w + 310 + 10, 0, 30 + w + 310 + 10 + self.logo['w'],
                      self.logo['h']))

        #pegar fechahora

        fechahora = Image.new("1", (400, 300), 1)
        drawer = ImageDraw.Draw(fechahora)
        #drawer.text((0,20), "Emitido el", font=self.font)
        fechaventap = fechaventa.split(" ")
        alt = int((300 - 80 * 3) / 2)
        drawer.text((0, alt), fechaventap[0], font=self.font)
        drawer.text((0, alt + 80), fechaventap[1], font=self.font)

        # si es un socio, imprimir su nombre
        nombresocio = self.get_nombre_socio(barcode)
        if nombresocio != None:
            drawer.text((0, alt + 160), nombresocio, font=self.font)

        lienzo.paste(fechahora,
                     (30 + w + 310 + 10 + self.logo['w'] + 10, 0,
                      30 + w + 310 + 10 + self.logo['w'] + 10 + 400, 300))

        if debug != False:
            drawer = ImageDraw.Draw(lienzo)
            tam_w, tam_h = self.font_debug.getsize(debug)
            margen_izq = int((b.size[0] - tam_w) / 2) + 30
            margen_sup = int((b.size[1] + 10)) + topm
            drawer.text((margen_izq, margen_sup), debug, font=self.font_debug)

        return lienzo
	if len(sys.argv) != 2:
		quit()
	archivo = sys.argv[1]
	root = ET.parse(archivo).getroot()
	for localidad in root.findall('localidad'):
		cod_seg = localidad.find("idUsr").text
		cod_loc = localidad.find("codLoc").text
		maximo = int(localidad.find("entradas").text)
		nombre_sec = localidad.find("nombreLoc").text
		nombre_seg = localidad.find("nombreUsr").text

		numero = random.randint(1,int(maximo*1.4))
		codigo = "%04d%s%s" % (numero, cod_seg, cod_loc)

		codes[(cod_seg, cod_loc)] = {
			"img": Code128b.code128_image(codigo, 50),
			"label": "%s/%s" % (nombre_sec, nombre_seg),
			"code": "%s (max: %d)" % (codigo, maximo),
			"valid": True if numero <= maximo else False
		}
	print "Cantidad de localidades: %d" % (len(codes))
	posx = 0
	posy = 0
	dpi = 100
	ancho_carta_p = 8.5
	alto_carta_p = 11
	ancho_carta = int(ancho_carta_p * dpi)
	alto_carta = int(alto_carta_p * dpi)
	lienzo = Image.new("RGB", (ancho_carta, alto_carta), (255, 255, 255))
	ancho_casilla = int(ancho_carta / 4)
	alto_casilla = int(alto_carta / 10)