PPI/DPI到真实世界测量刻度校准

2024-05-16 18:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从照片中提取真实世界的度量值,在本例中是厘米(cm)。我写了一小段代码,让我知道计算机在屏幕上是如何以像素到厘米为单位测量的:

# When pixels(px) are equal to PPI, then 1 PPI = 1 inch or 2.54cm.
# 1cm = 96pixels/2.54, or 1cm = 37.7952756 pixels at 96ppi
# As PPI increases, and number of pixels stay the same, cm decreases.
# As number of pixels increases, and PPI stays the same, cm increases.
# PPI * x = 96 and pixel height * y = 37.7952756

from PIL import Image
img = Image.open("test.png")
#
px_width = img.size[0]
px_height = img.size[1]
dpi = img.info['dpi']
#
print('Pixel width of the image is:', px_width)
print('Pixel height of the image is:', px_height)
print('DPI of the image is:', dpi)
px_convert_cm = 1/(2.54/dpi[1])
#
print(f"{px_convert_cm} pixels at {dpi[1]} DPI is equal to 1 cm.")
img_cm = px_height/px_convert_cm
print(f"Based on scanning resolution, the imported image appears as {img_cm} centimeters in height on the screen.")

然而,cm中的最终度量不是真实世界的度量。我有一个我扫描过的秤。我如何使用刻度(在本例中为600 DPI的10厘米刻度)从图像中提取真实世界的测量值?附上图片和比例。谢谢你的帮助/建议

item to be measured

10cm scale sized to 10cm in photoshop


Tags: ofthetoimageimg度量iscm
1条回答
网友
1楼 · 发布于 2024-05-16 18:32:23
# When pixels(px) are equal to PPI, then 1 PPI = 1 inch or 2.54cm.
# 1cm = 96pixels/2.54, or 1cm = 37.7952756 pixels at 96ppi
# As PPI increases, and number of pixels stay the same, cm decreases.
# As number of pixels increases, and PPI stays the same, cm increases.
# PPI * x = 96 and pixel height * y = 37.7952756

# Formulas
# 1cm = 96px / 2.54
# 1px = 2.54 cm / 96
# 1px = 2.54 cm / PPI
# cm = pixels * ( 2.54 / PPI )

from PIL import Image

scale_size = int(input("Enter scale size in cm: "))
scale_res = int(input("Enter scale resolution in PPI: "))
scale_px = scale_size/(2.54/scale_res)  # pixels number based on the equation: cm = pixels * ( 2.54 / PPI )
scale_unit = scale_px/scale_size  # what 1 cm of the scale will be in pixels
# print(scale_px)
# print(scale_unit)
# Import the image
img = Image.open("scale_real.png")

# dimension information in pixels
px_width = img.size[0]
px_height = img.size[1]
dpi = img.info['dpi']

# Return information about the scanned image
print('Pixel width of the image is:', px_width)
print('Pixel height of the image is:', px_height)
print('DPI of the image is:', dpi)
print('PPI of the image is:', dpi[1])

px_cm = 1/(2.54/dpi[1])  # how many pixels of the scanned image equal 1 cm
scanned_cm = px_height*(2.54/dpi[1])  # cm of scanned image based on image resolution an pixel count
multiplier = scale_unit/px_cm  # to equalize the pixels in 1cm of the scale and 1cm of the scanned image
new_cm = multiplier*px_height

print(f"For the scanned image, {px_cm} pixels at {dpi[1]} PPI is equal to 1 cm.")
print(f"Based on scanning resolution, the imported image appears as {scanned_cm} centimeters in height on the screen.")
print(f"Based on the provided scale of {scale_size} cm, at a DPI of {scale_res}, 1 cm will equate to {scale_unit} pixels.")

相关问题 更多 >