繁体   English   中英

通过保留物理尺寸降低图像 PPI(每英寸像素数)

[英]Reduce image PPI (Pixels Per Inch) with physical dimensions preservation

我正在为印刷行业开发软件,我需要将高质量和高 PPI(例如 300)上传的图像转换为具有相同物理尺寸(以英寸为单位)的低 PPI(例如 40)。
例如,将 300 PPI 和 10x10(英寸 x 英寸)的图像转换为 50 PPI 和 10x10(英寸 x 英寸)的图像
这种转换很重要,因为我们希望向用户显示带有其他透明层的低质量图像,因为要预览实际的最终打印结果。

这张图片是另一个例子

如何在 Java 或 Kotlin 中做到这一点?

首先,请耐心等待,因为我既不了解打印过程,也不了解图形术语。 这个答案假设(用我自己的话说)目标是拍摄图像并对其进行修改,以减少细节 - 显示图像所需的像素更少,但尺寸保持不变。 如果事实证明我误解了目标,请随时指出。 我要么尝试编辑答案,要么想出一些新的东西。


我建议您拍摄图像,缩小并将其拉伸回相同大小的解决方案:

val path = "Y:\\our\\path\\\\to\\directory\\"

val sourceImage = ImageIO.read(File("${path}original.png"))

val smallerImage = BufferedImage(
        sourceImage.width / 2,
        sourceImage.height / 2,
        sourceImage.type
)

var graphics2D = smallerImage.createGraphics()
graphics2D.drawImage(
        sourceImage,
        0,
        0,
        sourceImage.width / 2,
        sourceImage.height / 2,
        null
)
graphics2D.dispose()

在这里, smallerImage被缩小了——我们现在使用的像素比原来使用的少 4 倍(因为我们将宽度和高度都缩放了 2 倍)。

这实现了使用更少像素的目标,但没有保留大小 - 它被缩小了。 我们现在需要将其拉伸回原始大小,但现在我们将使用较少数量的像素:

val stretched = smallerImage.getScaledInstance(
        sourceImage.width,
        sourceImage.height,
        Image.SCALE_DEFAULT
)

val destination = BufferedImage(
        sourceImage.width,
        sourceImage.height,
        sourceImage.type
)

graphics2D = destination.createGraphics()
graphics2D.drawImage(stretched, 0, 0, null)
graphics2D.dispose()

最后,我们将图像保存到文件中:

val destinationImageFile = File("${path}destination.png")
ImageIO.write(destination, "png", destinationImageFile)

我们完成了。 图片originaldestination

原始图像 (16x16) 目标(已更改)图像 (16x16)


保存的像素数仅由您使用的比例因子决定。 您必须使用缩放来实现精确的像素数量节省。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM