1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
from PIL import Image from PIL import ImageDraw from PIL import Image, ImageDraw, ImageFont import os outputfile="ruler.pdf"
dpi_scale=5 print_scale=25.0/22.7 weight=595*dpi_scale height=842*dpi_scale PixelPermm=int(72*dpi_scale/2.54/10*print_scale)
img = Image.new('RGB', (weight,height), (255, 255, 255))
img_d = ImageDraw.Draw(img)
x_len, y_len = img.size x_step = PixelPermm y_step = PixelPermm print(x_len) print(y_len)
edge_lineToMarginleft=15*PixelPermm edge_lineToMarginright=15*PixelPermm edge_lineToMargintop=15*PixelPermm edge_lineToMarginbottom=15*PixelPermm
edge_lineHeadToMargin_ver=12*PixelPermm edge_lineHeadToMargin_hor=12*PixelPermm
font = ImageFont.truetype('LiberationSans-Regular.ttf', 2*PixelPermm) font_cm = ImageFont.truetype('LiberationSans-Regular.ttf', 1*PixelPermm)
color_margin=(71,89,201) color_text=(0,0,0) color_bigline=(255,0,0) color_line=(0,0,0)
img_d.rectangle([0,0,weight,height] , fill =None, outline =color_margin,width =8*PixelPermm)
i=0 for x in range(edge_lineToMarginleft, x_len-edge_lineToMarginright, x_step): if (i % 10 ==0) : img_d.line(((x, edge_lineHeadToMargin_ver), (x, y_len-edge_lineHeadToMargin_ver)), color_bigline,width=3) img_d.text((x, y_len-edge_lineHeadToMargin_hor+0.3*PixelPermm), str(i/10),font=font, fill=color_text) img_d.text((x+1*len(str(i/10))*PixelPermm, y_len-edge_lineHeadToMargin_hor+1*PixelPermm), " cm",font=font_cm, fill=color_text)
else: img_d.line(((x, edge_lineHeadToMargin_ver), (x, y_len-edge_lineHeadToMargin_ver)), color_line,width=2) i=i+1
img = img.rotate(90, expand=True) img_d = ImageDraw.Draw(img)
i=0 for y in range( y_len-edge_lineToMarginbottom,edge_lineToMargintop, -y_step): if (i % 10 ==0) : img_d.line(((y, edge_lineHeadToMargin_hor), (y, x_len-edge_lineHeadToMargin_hor)), color_bigline,width=3) img_d.text((y, x_len-edge_lineHeadToMargin_ver+0.3*PixelPermm), str(i/10),font=font, fill=color_text) img_d.text((y+1*len(str(i/10))*PixelPermm, x_len-edge_lineHeadToMargin_ver+1*PixelPermm), " cm",font=font_cm, fill=color_text)
else: img_d.line(((y, edge_lineHeadToMargin_hor), (y, x_len-edge_lineHeadToMargin_hor)), color_line,width=2) i=i+1 print(i) img = img.rotate(-90, expand=True)
img.save(outputfile, 'PDF', quality=100)
|