|
import os, cv2, argparse |
|
import numpy as np |
|
|
|
|
|
def transparence2white(img): |
|
sp=img.shape |
|
width=sp[0] |
|
height=sp[1] |
|
for yh in range(height): |
|
for xw in range(width): |
|
color_d=img[xw,yh] |
|
if(color_d[3]==0): |
|
img[xw,yh]=[255,255,255,255] |
|
return img |
|
|
|
|
|
def transparence2black(img): |
|
sp = img.shape |
|
width = sp[0] |
|
height = sp[1] |
|
for yh in range(height): |
|
for xw in range(width): |
|
color_d = img[xw, yh] |
|
if (color_d[3] == 0): |
|
img[xw, yh] = [0, 0, 0, 255] |
|
return img |
|
|
|
|
|
def center_crop(img, crop_size): |
|
h, w = img.shape[:2] |
|
th, tw = crop_size |
|
i = int(round((h - th) / 2.)) |
|
j = int(round((w - tw) / 2.)) |
|
return img[i:i + th, j:j + tw] |
|
|
|
if __name__ == '__main__': |
|
parser = argparse.ArgumentParser() |
|
|
|
parser.add_argument("origin_image_path", default=None, type=str, help="Path to the images to convert.") |
|
parser.add_argument("output_image_path", default=None, type=str, help="Path to the output images.") |
|
parser.add_argument("--width", default=512, type=int, help="Width of the output images.") |
|
parser.add_argument("--height", default=512, type=int, help="Height of the output images.") |
|
parser.add_argument("--png", action="store_true", help="convert the transparent background to white/black.") |
|
|
|
|
|
args = parser.parse_args() |
|
|
|
path = args.origin_image_path |
|
save_path = args.output_image_path |
|
if not os.path.exists(save_path): |
|
os.makedirs(save_path) |
|
else: |
|
print('The folder already exists, please check the path.') |
|
|
|
|
|
allow_suffix = ['png', 'jpg', 'jpeg', 'bmp', 'webp'] |
|
image_list = os.listdir(path) |
|
image_list = [os.path.join(path, image) for image in image_list if image.split('.')[-1] in allow_suffix] |
|
|
|
for file, i in zip(image_list, range(1, len(image_list)+1)): |
|
print('Processing image: %s' % file) |
|
try: |
|
img = cv2.imread(file, -1) |
|
|
|
|
|
crop_size = min(img.shape[:2]) |
|
img = center_crop(img, (crop_size, crop_size)) |
|
|
|
|
|
width = args.width |
|
height = args.height |
|
img = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA) |
|
|
|
|
|
if args.png: |
|
img = transparence2black(img) |
|
|
|
cv2.imwrite(os.path.join(save_path, str(i).zfill(4) + ".jpg"), img) |
|
except Exception as e: |
|
print(e) |
|
os.remove(path+file) |
|
print("删除无效图片: " + path+file) |