我正在为印刷行业开发软件,我需要将高质量和高PPI(例如300)上传的图像转换为具有相同物理尺寸(英寸)的低PPI(例如40)。
例如,将具有300 PPI和10x10(英寸x英寸)的图像转换为具有50 PPI和10x10(英寸x英寸)的图像
此转换很重要,因为由于要预览实际的最终打印结果,我们希望通过其他透明层向用户显示低质量的图像。
this image is another example
如何用Java或Kotlin做到这一点?
答案 0 :(得分:0)
首先,请耐心等待,因为我既不了解打印过程,也不了解图形术语。这个答案假设(用我自己的话说)目标是拍摄图像并对其进行修改,以使细节更少-显示图像所需的像素更少,但尺寸保持不变。如果事实证明我误解了目标,请随时指出。我将尝试编辑答案或提出新的建议。
我提出了一种解决方案,您可以在其中拍摄图像,按比例缩小图像并将其拉伸回相同的大小:
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)
我们完成了。图片original
和destination
:
保存的像素数完全由您使用的比例因子确定。您必须进行缩放才能实现精确的像素数量节省。