图片读取:Image.open(ImgPath)

from PIL import Image

利用 img = Image.open(ImgPath) 打开的图片是PIL类型的,它自带resize函数。由于pytorch的顺序是(batch,c,h,w),所以需要进行PIL类型到numpy类型转换,tensorflow,numpy的顺序是(batch,h,w,c):

  # Load Image
  img_fn = os.path.join(self.input_path, filenames)
  img = Image.open(img_fn)  # RGB(默认)
  # img.show()

  # resize/crop if needed:[128*128*3]
  if self.input_size != 0:
      height = width = self.input_size
      img = img.resize((height, width), Image.BILINEAR)

  # 将PIL类型转化成numpy类型
  img = np.array(img).uint8()    # H*W*C

 # Load Image
  img_fn = os.path.join(self.input_path, filenames)
  img = Image.open(img_fn)  # RGB(默认)
  # img.show()
  # 将PIL类型转化成numpy类型
  img = np.array(img).uint8()    # H*W*C
 
  # resize/crop if needed:[128*128*3]
  if self.input_size != 0:
      height = width = self.input_size
      img = self.resize(img, height, width)

 def resize(self, img, height, width, centerCrop=True, interp='bilinear'):
     imgh, imgw = img.shape[0:2]
     if centerCrop and imgh != imgw:
         # center crop
         side = np.minimum(imgh, imgw)
         j = (imgh - side) // 2
         i = (imgw - side) // 2
         img = img[j:j + side, i:i + side, ...]
     # 改变图像大小并且隐藏归一化到0-255区间的操作
     img = scipy.misc.imresize(img, [height, width], interp=interp)
     return img

from scipy.misc import imread

利用 img = imread(ImgPath) 打开的图片虽然是numpy类型的,并且也是RGB格式。但是由于 scipy 在版本1.3.0后就取消了imread函数,如果下载低版本的scipy会与tensorflow出现各种不兼容现象,挺麻烦的

  • 38
    点赞
  • 149
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
很抱歉,这段代码是 Python 代码,无法直接在 Node.js 中运行。如果你想在 Node.js 中实现类似的功能,可以考虑使用类似的图片处理库,例如 Jimp 或 GraphicsMagick。以下是使用 Jimp 库实现类似功能的示例代码: ```javascript const Jimp = require('jimp'); async function parseBgCaptcha(imgPath, imShow = false, savePath = null) { // 读取图片 const img = await Jimp.read(imgPath); // 图片还原顺序, 定值 const Ge = [ 39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42, 12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17 ]; const wSep = 10; const hSep = 80; // 还原后的背景图 const newImg = new Jimp(260, 160); for (let idx = 0; idx < Ge.length; idx++) { const x = Ge[idx] % 26 * 12 + 1; const y = Ge[idx] > 25 ? hSep : 0; // 从背景图中裁剪出对应位置的小块 const imgCut = img.clone().crop(x, y, wSep, hSep); console.log(imgCut); // 将小块拼接到新图中 const newX = idx % 26 * 10; const newY = idx > 25 ? hSep : 0; newImg.blit(imgCut, newX, newY); } if (imShow) { newImg.write('output.jpg'); } if (savePath !== null) { await newImg.writeAsync(savePath); } return newImg; } parseBgCaptcha('bg.webp', true, 'bg.jpg'); ``` 在上面的代码中,我们使用 Jimp 库读取了名为 `bg.webp` 的图片,然后使用跟 Python 版本类似的方式还原了图片中的小块位置,并裁剪拼接成一张新图片。如果设置了 `imShow` 为 `true`,则会在当前目录下生成一张新图片 `output.jpg` 并打开。如果设置了 `savePath`,则会将新图片保存到指定路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码小白的成长

计算机网络PPT下载

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值