增加图像尺寸而不损失质量-JS

时间:2020-08-04 10:29:14

标签: javascript html electron

我正在使用电子制作一个具有应用程序图标的应用程序。 所以我用app.getFileIcon()来获取图标,并使用icon.toDataURL()并将带有事件的图像URL传递给index.html

它可以正常工作,但是当我增加图像尺寸时,图像变得模糊,因为app.getFileIcon()的图标最大尺寸只能是32px(在MacOs或Linux中为48px)

这是它的样子(高度增加之前)

enter image description here

身高增加后

enter image description here

Main.js

const { app, BrowserWindow, ipcMain, shell } = require('electron')
const path = require('path');
const { writeFileSync } = require('fs');

let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    }
  })

  win.loadFile('public/index.html')
}

app.whenReady().then(createWindow)

// Get File Icons ------------------------------------------------

const filePath = path.normalize("C:\\Users\\hamid\\Desktop\\DualTouch\\NordVPN.lnk");
const realPath = shell.readShortcutLink(filePath).target

app.getFileIcon(realPath).then(icon => {

  const ImgUrl = icon.toDataURL()
  ipcMain.on('ImgUrl-request', (event)=>{
    event.sender.send('ImgUrl-reply', ImgUrl)
  })
  writeFileSync('icon.png', icon.toPNG())

}).catch(err => console.log(err))

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>DualTouch</title>
</head>
<body>
  <div id="app"></div>

  <script src="assets/js/app.js"></script>
  <script>
  const { ipcRenderer } = require('electron')

  ipcRenderer.send('ImgUrl-request');

  ipcRenderer.on('ImgUrl-reply', function (event, args) {
    document.write(`<img src='${args}' />`)
  });
  </script>
</body>
</html>

Icon Url

2 个答案:

答案 0 :(得分:1)

不幸的是,图像尺寸与质量成反比,即图像像素的曝光。因此,为了获得更高的质量,我建议您获得具有更多像素和更高分辨率的图像。

答案 1 :(得分:0)

您不能将信息添加到不存在的图像中,但是这些日子确实有很好的升频器,特别是那些基于AI的日子。

例如,访问该网站-> https://bigjpg.com/,我可以将您的图片放大为这样的外观->

Upscaled Image

快速搜索用于AI升级的工具会引起https://github.com/topics/upscaling的出现,因此这些选项之一可能有用。