基于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'))

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值