def run(self,mainframe,values=None): BLACK=0 from config import card_folder, a4_size, PRINT_FORMAT from os.path import join,isfile,splitext from math import sqrt, radians from ImageOps import grayscale dst=values['Destination file'] DEPTH=values['Depth(cm)'] LENGTH=values['Length(cm) - optional'] WIDTH=values['Width(cm) - optional'] if not dst: dlg = wx.FileDialog(mainframe, "Choose a destination file for Box Front", mainframe.deckdir, "box.png", "*.*", wx.SAVE| wx.OVERWRITE_PROMPT) try: if dlg.ShowModal() == wx.ID_OK: dst = dlg.GetPath() else: return finally: dlg.Destroy() name,ext=splitext(dst) dstfront='.'.join([name,'front'])+ext dstback='.'.join([name,'back'])+ext front=values['front'] if not isfile(front): front=None back=values['back'] if not isfile(back): back=None sidelr=values['side (left/right)'] if not isfile(sidelr): sidelr=None sidetb=values['side (top/bottom)'] if not isfile(sidetb): sidetb=None #Determine tuckbox size width,length=mainframe.fitting_size from config import left, right, bottom, top, a4_size, DEBUG, PRINT_FORMAT, card_folder, sheet_left, sheet_top, BLACK_FILLING width+=left+right+5 + 10 #10 added to make some place for the card around it in the tuckbox length+=top+bottom+5 length=float(length+10) width=float(width+10) deep=10+float((mainframe.deckgrid.GetDeckSize()))* 2.5 import config if DEPTH: deep=config.cm2p(DEPTH) if LENGTH: length=config.cm2p(LENGTH) if WIDTH: width=config.cm2p(WIDTH) #Adapt length & width to have back insert in to front lengthFront=length+10 widthFront=width+10 #Create Tuckboxes lines import Image, ImageDraw #w,l=a4_size w=int(3*deep+2*width+1) l=int(3*deep+length +1) if sorted((w,l))>sorted(a4_size): print "won't fit in a4 page" _W,_L=a4_size sheet=Image.new('RGB', (_W,_L),(255,255,255)) sheetFront=Image.new('RGB',(_W,_L),(255,255,255)) drawFront=ImageDraw.Draw(sheetFront) draw=ImageDraw.Draw(sheet) #Draw necessary lines rw=7*deep/8 rl=deep/4 for d,w,l in zip((draw,drawFront),(width,widthFront),(length,lengthFront)): d.rectangle((0,deep-rl,rw,deep),outline=BLACK) d.rectangle((0,l+deep,rw,l+deep+rl),outline=BLACK) d.rectangle((deep+w+deep-rw,deep-rl,deep+w+deep,deep),outline=BLACK) d.rectangle((deep+w+deep-rw,l+deep,deep+w+deep,l+deep+rl),outline=BLACK) d.line((rw,deep-rl, deep,deep),fill=BLACK) d.line((rw,deep-rl, deep,deep),fill=BLACK) #Now copy & resize images in the tuckboxes length=int(length) width=int(width) deep=int(deep) if front: front_image=Image.open(front) front_image=front_image.resize((widthFront,lengthFront)) sheetFront.paste(front_image,(deep,deep)) if back: back_image=Image.open(back) back_image=back_image.resize((width,length)) sheet.paste(back_image,(deep,deep)) if sidelr: side=Image.open(sidelr) side=grayscale(side.resize((length,deep))) lside=side.rotate(90,expand=True) rside=side.rotate(270,expand=True) sheet.paste(rside,(0,deep)) sheet.paste(lside,(deep+width,deep)) side=Image.open(sidelr) side=side.resize((lengthFront,deep)) lside=side.rotate(90,expand=True) rside=side.rotate(270,expand=True) sheetFront.paste(rside,(0,deep)) sheetFront.paste(lside,(deep+widthFront,deep)) if sidetb: side=Image.open(sidetb) side=grayscale(side.resize((width,deep))) bside=side.rotate(180,expand=True) sheet.paste(side,(deep,0)) sheet.paste(bside,(deep,length+deep)) side=Image.open(sidetb) side=side.resize((widthFront,deep)) bside=side.rotate(180,expand=True) sheetFront.paste(bside,(deep,0)) sheetFront.paste(side,(deep,lengthFront+deep)) #Save & display sheet.save(dstback) sheetFront.save(dstfront) import os os.startfile(dstfront) return
def pdfcreate_image(deckname, dst, fitting_size, front,back, templates): output=canvas.Canvas(os.path.join(dst,deckname+".pdf"),psize,cropMarks=False) output.setTitle(deckname) output.setStrokeColor(black) output.setFont('Times-Roman',10) output.setPageCallBack(cb) import config if fitting_size is None: fitting_size=(config.px,config.py) else: fitting_size=[p2cm(i) for i in fitting_size] a4_size_x,a4_size_y=(config.a4x,config.a4y) x,y=fitting_size left=p2cm(config.left) right=p2cm(config.right) top=p2cm(config.top) bottom=p2cm(config.bottom) sheet_left=p2cm(config.sheet_left) sheet_top=p2cm(config.sheet_top) x_gap=left+right+x+p2cm(5) y_gap=top+bottom+y+p2cm(5) NUM_ROW=int((a4_size_x-top)/x_gap) NUM_COL=int((a4_size_y-left)/y_gap) PICT_BY_SHEET=NUM_COL*NUM_ROW deckname=deckname.lower().split('.')[0] if DEBUG: print 'Preparing %d picts by sheet (%d by %d)'%(PICT_BY_SHEET, NUM_ROW, NUM_COL), _t="" if dual: _t=" BACK mode" print _t maxlen=max(len(front),len(back)) for i in range(int(ceil((float(maxlen)/PICT_BY_SHEET)))): for dual,imgs in zip((False,True),(front,back)): if not imgs: continue sheet_imgs=imgs[PICT_BY_SHEET*i:PICT_BY_SHEET*(i+1)] if not sheet_imgs: continue sheet_imgs.reverse() #Write the fiiting size info for deck + date from datetime import date text="Printed on the %s. fitting size: %s"%(date.today().strftime('%d/%m/%y'),fitting_size) output.drawString(sheet_left,a4_size_y-sheet_left,text) for col in range(NUM_COL): for row in range(NUM_ROW): try: s,rotated,templateName,values=sheet_imgs.pop() #print s,rotated,templateName,values T=templates.get(templateName,None) if not T: #print templateName,T, templates.keys() T= template_empty #else: #print 'using template',T.name card=T.draw(os.path.join(config.card_folder,s.encode('cp1252')),fitting_size,values) except IndexError:#done ! break if rotated: card=card.rotate(90) #No more resizing: this is being handled by draw image bounding box card=card.resize([config.cm2p(_i) for _i in fitting_size]) #Start point is reverted from right border if dual if not dual: startx=row*x_gap + sheet_left else: startx=a4_size_x - ((row+1)*x_gap + sheet_left) starty=col*y_gap + sheet_top #Line vertical: output.line(startx*cm,0,startx*cm,a4_size_y*cm) output.line(cm*(startx+left+right+x),0,cm*(startx+left+right+x),a4_size_y*cm) #Line horizontal output.line(0,starty*cm,a4_size_x*cm,starty*cm) output.line(0,(starty+top+bottom+y)*cm,a4_size_x*cm,(starty+top+bottom+y)*cm) #~ #black rectangle if config.BLACK_FILLING: output.rect(startx*cm,starty*cm, (left+right+x)*cm, (top+bottom+y)*cm,fill=True) #PAste image c=StringIO() card.save(c, format="PNG") c.seek(0) #Rewind it ir = ImageReader(c) #print 'Pasting Image at cm:',startx+left,starty+top, x,y,fitting_size output.drawImage(ir,(startx+left)*cm,(starty+top)*cm,x*cm,y*cm) output.showPage() #In dual mode, save as back output.save()
def run(self,mainframe,values=None): BLACK=0 from config import card_folder, a4_size, PRINT_FORMAT from os.path import join,isfile from math import sqrt, radians dst=values['Destination file'] DEPTH=values['Depth(cm)'] LENGTH=values['Length(cm) - optional'] WIDTH=values['Width(cm) - optional'] if not dst: dlg = wx.FileDialog(mainframe, "Choose a destination file", mainframe.deckdir, "tuckbox.png", "*.*", wx.SAVE| wx.OVERWRITE_PROMPT) try: if dlg.ShowModal() == wx.ID_OK: dst = dlg.GetPath() else: return finally: dlg.Destroy() front=values['front'] if not isfile(front): front=None back=values['back'] if not isfile(back): back=None sidelr=values['side (left/right)'] if not isfile(sidelr): sidelr=None sidetb=values['side (top/bottom)'] if not isfile(sidetb): sidetb=None #Determine tuckbox size width,length=mainframe.fitting_size from config import left, right, bottom, top, a4_size, DEBUG, PRINT_FORMAT, card_folder, sheet_left, sheet_top, BLACK_FILLING width+=left+right+5 + 10 #10 added to make some place for the card around it in the tuckbox length+=top+bottom+5 length=float(length+10) width=float(width+10) deep=10+float((mainframe.deckgrid.GetDeckSize()))* 2.5 import config if DEPTH: deep=config.cm2p(DEPTH) if LENGTH: length=config.cm2p(LENGTH) if WIDTH: width=config.cm2p(WIDTH) #Create Tuckboxes lines import Image, ImageDraw #w,l=a4_size w=int(3*deep+2*width+1) l=int(3*deep+length +1) if sorted((w,l))>sorted(a4_size): print "won't fit in a4 page" _W,_L=reversed(a4_size) sheet=Image.new('RGB', (_W,_L),(255,255,255)) draw=ImageDraw.Draw(sheet) #Draw necessary lines def ysym(y): return int(3*deep + length - y) iso= lambda x:x step=deep/4 colors=[BLACK]*6 for stopx,color in zip([deep,deep+width, 2*deep+width,2*(deep+width),2.9*deep+2*width],colors): draw.rectangle((0,deep+2*step,stopx,deep+2*step+length),outline=color) for func in (iso,ysym): draw.line((deep/2,func(deep), deep,func(deep)),fill=colors[0]) draw.line((deep+width,func(deep), 3*deep/2+width,func(deep)),fill=colors[1]) draw.line((deep,func(2*step), deep,func(deep+2*step)),fill=colors[2]) draw.line((deep,func(2*step), deep+width,func(2*step)),fill=colors[3]) draw.line((deep+width/4,func(0), deep+3*width/4,func(0)),fill=colors[4]) draw.line((deep+width,func(2*step), deep+width,func(deep+2*step)),fill=colors[5]) draw.arc((0,int(deep+2*step),int(deep/2),int(deep)),180,270,fill=BLACK) #Now for the folding bottom rectangle, only at the bottom draw.rectangle((2*deep+width,length+step+deep,2*(deep+width),length+step+2*deep),outline=BLACK) #Now the arcs draw.arc((int(width+deep),int(deep),int(2*deep+width),int(2*deep)),270,360,fill="#000000") draw.arc((int(width+deep),int(length+deep),int(2*deep+width),int(length+2*deep)),0,90,fill="#000000") draw.arc((0,int(deep),int(deep),int(2*deep)),180,270,fill="#000000") draw.arc((0,int(length+deep),int(deep),int(length+2*deep)),90,180,fill="#000000") ## draw.arc((int(deep),0,int(deep+width/2),int(deep)),180,270,fill="#000000") draw.arc((int(deep),int(2*deep+length),int(deep+width/2),int(3*deep+length)),90,180,fill="#000000") draw.arc((int(width/2+deep),0,int(deep+width),int(deep)),270,360,fill="#000000") draw.arc((int(width/2+deep),int(2*deep+length),int(deep+width),int(3*deep+length)),0,90,fill="#0000ff") #Now copy & resize images in the tuckboxes length=int(length) width=int(width) deep=int(deep) step=int(step) if front: front_image=Image.open(front) front_image=front_image.resize((width,length)) sheet.paste(front_image,(width+2*deep,deep+2*step)) if back: back_image=Image.open(back) back_image=back_image.resize((width,length)) sheet.paste(back_image,(deep,deep+2*step)) if sidelr: side=Image.open(sidelr) side=side.resize((length,deep)) lside=side.rotate(90,expand=True) rside=side.rotate(270,expand=True) sheet.paste(rside,(0,deep+step*2)) sheet.paste(lside,(deep+width,deep+2*step)) if sidetb: side=Image.open(sidetb) side=side.resize((width,deep)) bside=side.rotate(180,expand=True) sheet.paste(side,(deep,2*step)) sheet.paste(bside,(deep,length+deep+2*step)) #Save & display sheet.save(dst) import os os.startfile(dst)