纸拍尺

今天看到别人家产品拍照时下面垫的网格纸心动了,但是在网上搜了搜却发现这个东西很小众,没找到现成现的,有的卖的说要定制,有的配色不好看,遂决定自己做一个。

1.jpg

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
# -*- coding: utf-8 -*-

from PIL import Image
from PIL import ImageDraw
from PIL import Image, ImageDraw, ImageFont
import os
outputfile="ruler.pdf"

#72像素/英寸 72像素=2.54cm

dpi_scale=5# 1:A4 at 72dpi
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)) # White

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)

运行后会产生一个 pdf,将其打印到 A4 纸后,由于打印的缩放,刻度可能不太准确 ,用尺子测量后修改print_scale,校正每 mm 的像素点个数即可。