我正在为印刷行业开发软件,我需要转换高质量和高PPI(例如300)上传的图像为低PPI(例如40英寸)与相同的物理尺寸英寸。
例如,将300 PPI和10x10(英寸x英寸)的图像转换为50 PPI和10x10(英寸x英寸)的图像。
此转换很重要,因为我们希望使用其他透明层向用户显示低质量的图像,这是由于预览实际的最终打印结果。
我如何在Java或Kotlin中做到这一点?
发布于 2020-07-13 18:43:37
首先,请耐心听我说,因为我既不懂印刷工艺,也不懂图形术语。这个答案假设(用我自己的话说),我们的目标是拍摄一张图像并对其进行修改,使其细节更少-显示图像所需的像素更少,但保留了大小。如果事实证明我误解了目标,请随时指出。我要么试着修改答案,要么想出一些新的东西。
我提出的解决方案是,将图像按比例缩小,并将其拉伸回相同的大小:
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
保存的像素数仅由您使用的比例因子确定。您将不得不尝试缩放以实现精确的像素数节省。
https://stackoverflow.com/questions/62853809
复制相似问题