1. 程式人生 > >基於python的超分結果圖的處理

基於python的超分結果圖的處理

做SR任務時,需要擷取一些圖的效果,寫下本博文~

import matplotlib.pyplot as plt
from PIL import Image
import os 


fin='/home/guanwp/zhangwenlong/paperresult/ESRGAN/Set5_ESRGAN/'
fout=fin+'result'+'/'

def is_image_file(filename):
    '''
    Check wheather the file is a image file.
    :param filename: name of the file
    :return: bool value shows that whether it is a image
    '''
    return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg", ".bmp"])


for file in os.listdir(fin):
	if is_image_file(file):
		file_fullname=fin+'/'+file
		img=Image.open(file_fullname)

		a=[201,112,503,188]
		box=(a)
		ROI=img.crop(box)

		out_path=fout+'/'+file
		ROI.save(out_path)

 

import matplotlib.pyplot as plt
from PIL import Image,ImageDraw
import cv2
import os 


fin='/home/guanwp/zhangwenlong/paperresult/SRGANx4_RANK_3style_V1_esrgan_pre_46d7/val_set14/'
fout='result'+'/'

def is_image_file(filename):
    '''
    Check wheather the file is a image file.
    :param filename: name of the file
    :return: bool value shows that whether it is a image
    '''
    return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg", ".bmp"])


for file in os.listdir(fin):
	if is_image_file(file):
		if file=='baboon.png':

			file_fullname=fin+'/'+file
			img=Image.open(file_fullname)

			x=216
			y=266
			w=206
			a=[x,y,x+w,y+w]
			box=(a)
			ROI=img.crop(box)
			#upscale
			
			out_path=fout+file
			ROI.save(out_path)

			im=cv2.imread(file_fullname)
			cv2.rectangle(im,(x,y),(x+w,y+w),(0,255,0),3)
			cv2.imshow("image",im)
			cv2.waitKey (0)
			cv2.destroyAllWindows() 
			out_draw=fout+'abc/'+file
			cv2.imwrite(out_draw,im)

 

from PIL import Image, ImageDraw
import glob, os

dir = 'D:\Documents\截圖/'  # 圖片所在目錄

# 選框左上角座標(x, y),寬度width,高度自動計算得出
x = 0
y = 0
width = 100

# 獲取影象
pyFile = glob.glob(os.path.join(dir, "*.png"))
pyFile += glob.glob(os.path.join(dir, "*.jpg"))
pyFile += glob.glob(os.path.join(dir, "*.bmp"))
result_path = os.path.join(dir,"result")

# 判斷是否存在result子目錄,若不存在則建立
if not os.path.exists(result_path) :
        os.mkdir(result_path)

# 遍歷圖片
for img_path in pyFile:
    im = Image.open(img_path)
    draw = ImageDraw.Draw(im)

    aspect_ratio = im.size[0]/im.size[1] # 長寬比
    # 擷取選區影象
    im_ = im.crop((x, y, x+width, (x+width)//aspect_ratio))
    # 框出選區
    draw.rectangle((x, y, x+width, (x+width)//aspect_ratio), outline='red', width=3) # width是線條的寬度

    im_ = im_.resize(im.size) # 呼叫resize函式將子圖放大到原圖大小

    # 獲取檔名
    _, img_name = os.path.split(img_path)
    img_name, _ = os.path.splitext(img_name)

    # 儲存子圖與含有選框的原圖
    im_.save(os.path.join(result_path , img_name + '_sub_image.png'))
    im.save(os.path.join(result_path , img_name + '_ori_image.png'))

 

from PIL import Image, ImageDraw
import glob, os

dir = '*******************'  # the direction of the result

# the(x, y), and the width
x = 80
y = 100
width = 200
scale=4

# capture an image
pyFile = glob.glob(os.path.join(dir, "*.png"))
pyFile += glob.glob(os.path.join(dir, "*.jpg"))
pyFile += glob.glob(os.path.join(dir, "*.bmp"))
result_path = os.path.join(dir,"result")

# if the in result
if not os.path.exists(result_path) :
        os.mkdir(result_path)

# Traverse the picture
for img_path in pyFile:
    im = Image.open(img_path)
    draw = ImageDraw.Draw(im)

    aspect_ratio = im.size[0]/im.size[1] # Aspect ratio
    # Intercepting a selection image
    im_ = im.crop((x, y, x+width, (x+width)//aspect_ratio))
    # Box out of the selection
    draw.rectangle((x, y, x+width, (x+width)//aspect_ratio), outline='red', width=3) # width是線條的寬度

    #im_ = im_.resize(im.size) # Call the resize function to enlarge the submap to the original image size
    width1=int(im_.size[0]*scale)
    height1=int(im_.size[1]*scale)
    im_=im_.resize((width1, height1), Image.ANTIALIAS)

    # Get the file name
    _, img_name = os.path.split(img_path)
    img_name, _ = os.path.splitext(img_name)

    # Save submap and original image with marquee
    im_.save(os.path.join(result_path , img_name + '_sub_image.png'))
    im.save(os.path.join(result_path , img_name + '_ori_image.png'))