我试图从照片中提取真实世界的度量值,在本例中是厘米(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厘米刻度)从图像中提取真实世界的测量值?附上图片和比例。谢谢你的帮助/建议
相关问题 更多 >
编程相关推荐