鱼C论坛

 找回密码
 立即注册
查看: 1382|回复: 3

[技术交流] Python的图片处理

[复制链接]
发表于 2020-2-20 17:54:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 符明东233 于 2020-2-21 02:39 编辑

做ESRGAN复现的时候 将图片预处理为更小单元的时候 发现progressbar的进度条不动 如图所示
希望大佬们不吝赐教
TIM截图20200216010138.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-20 17:55:49 | 显示全部楼层
  1. import os
  2. import torch
  3. import os.path as osp
  4. import sys
  5. from torch.multiprocessing import Pool
  6. import numpy as np
  7. import cv2
  8. try:
  9.     sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
  10.     from utils.util import ProgressBar
  11. except ImportError:
  12.     pass


  13. def main():
  14.     """A multi-thread tool to crop sub imags."""
  15.     input_folder = '/Users/Administrator/Desktop/milai/BasicSR-master/BasicSR-master/codes/scripts/DIV2K_train_LR_x8'
  16.     save_folder = '/Users/Administrator/Desktop/milai/BasicSR-master/BasicSR-master/codes/scripts/DIV2K_train_LR_x8_sub'
  17.     n_thread = 20
  18.     crop_sz = 480
  19.     step = 240
  20.     thres_sz = 48
  21.     compression_level = 3  # 3 is the default value in cv2
  22.     # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer
  23.     # compression time. If read raw images during training, use 0 for faster IO speed.

  24.     if not os.path.exists(save_folder):
  25.         os.makedirs(save_folder)
  26.         print('mkdir [{:s}] ...'.format(save_folder))
  27.     else:
  28.         print('Folder [{:s}] already exists. Exit...'.format(save_folder))
  29.         sys.exit(1)

  30.     img_list = []
  31.     for root, _, file_list in sorted(os.walk(input_folder)):
  32.         path = [os.path.join(root, x) for x in file_list]  # assume only images in the input_folder
  33.         img_list.extend(path)

  34.     def update(arg):
  35.         pbar.update(arg)

  36.     pbar = ProgressBar(len(img_list))

  37.     pool = Pool(n_thread)
  38.     for path in img_list:
  39.         pool.apply_async(worker,
  40.                          args=(path, save_folder, crop_sz, step, thres_sz, compression_level),
  41.                          callback=update)
  42.     pool.close()
  43.     pool.join()
  44.     print('All subprocesses done.')


  45. def worker(path, save_folder, crop_sz, step, thres_sz, compression_level):
  46.     img_name = os.path.basename(path)
  47.     img = cv2.imread(path, cv2.IMREAD_UNCHANGED)

  48.     n_channels = len(img.shape)
  49.     if n_channels == 2:
  50.         h, w = img.shape
  51.     elif n_channels == 3:
  52.         h, w, c = img.shape
  53.     else:
  54.         raise ValueError('Wrong image shape - {}'.format(n_channels))

  55.     h_space = np.arange(0, h - crop_sz + 1, step)
  56.     if h - (h_space[-1] + crop_sz) > thres_sz:
  57.         h_space = np.append(h_space, h - crop_sz)
  58.     w_space = np.arange(0, w - crop_sz + 1, step)
  59.     if w - (w_space[-1] + crop_sz) > thres_sz:
  60.         w_space = np.append(w_space, w - crop_sz)

  61.     index = 0
  62.     for x in h_space:
  63.         for y in w_space:
  64.             index += 1
  65.             if n_channels == 2:
  66.                 crop_img = img[x:x + crop_sz, y:y + crop_sz]
  67.             else:
  68.                 crop_img = img[x:x + crop_sz, y:y + crop_sz, :]
  69.             crop_img = np.ascontiguousarray(crop_img)
  70.             # var = np.var(crop_img / 255)
  71.             # if var > 0.008:
  72.             #     print(img_name, index_str, var)
  73.             cv2.imwrite(
  74.                 os.path.join(save_folder, img_name.replace('.png', '_s{:03d}.png'.format(index))),
  75.                 crop_img, [cv2.IMWRITE_PNG_COMPRESSION, compression_level])
  76.     return 'Processing {:s} ...'.format(img_name)


  77. if __name__ == '__main__':
  78.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-21 02:40:15 | 显示全部楼层
已解决
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-21 06:34:52 | 显示全部楼层
路过学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 16:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表