img.resize()函数的作用和用法-单张图像变换大小

52 篇文章 1 订阅
38 篇文章 1 订阅

https://blog.csdn.net/qq_32801595/article/details/80461084

这个是一段学过的简单程序,可以改变图像的大小,jpg,png都可以的:

#encoding=utf-8
#author: walker
#date: 2014-05-15
#function: 更改图片尺寸大小

from PIL import Image
'''
filein: 输入图片
fileout: 输出图片
width: 输出图片宽度
height:输出图片高度
type:输出图片类型(png, gif, jpeg...)
'''
def ResizeImage(filein, fileout, width, height, type):
  img = Image.open(filein)
  out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
  out.save(fileout, type)
if __name__ == "__main__":
  filein = r'0.jpg'
  fileout = r'testout.png'
  width = 6000
  height = 6000
  type = 'png'
  ResizeImage(filein, fileout, width, height, type)

这个函数img.resize((width, height),Image.ANTIALIAS)
第二个参数:
Image.NEAREST :低质量
Image.BILINEAR:双线性
Image.BICUBIC :三次样条插值
Image.ANTIALIAS:高质量

 

上面是单张图片尺寸的改变,针对大量数据集图片,如何批量操作,记录一下,为以后数据集预处理提供一点参考:

from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=1280,height=720):
    img=Image.open(jpgfile)   
    new_img=img.resize((width,height),Image.BILINEAR)   
    new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
for jpgfile in glob.glob("E:/test/picture/12/*.jpg"):
    convertjpg(jpgfile,"E:/test/picture/111/")

涉及到的一个重要函数glob.glob()
例如:

glob.glob(“E:/test/picture/12/*.jpg”)

返回12文件夹下所有的jpg路径

glob.glob(“E:/test/picture/111//“)

返回的是111文件夹下下个文件的所有路径

 

### 回答1: 好的,以下是一个示例代码,使用Python和OpenCV库实现单张图片的字符分割和图像预处理: ```python import cv2 def preprocess_image(image_path): # 读取图片 img = cv2.imread(image_path) # 灰度化处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 膨胀处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dilate = cv2.dilate(thresh, kernel, iterations=1) # 查找轮廓 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 对每个轮廓进行处理 for i in range(len(contours)): # 获取轮廓的坐标和大小 x, y, w, h = cv2.boundingRect(contours[i]) # 根据轮廓的大小,判断是否为字符 if w < 5 or h < 5: continue # 提取字符并保存 digit_img = thresh[y:y+h, x:x+w] cv2.imwrite("digit_{}.png".format(i), digit_img) def choose_digit(image_path, digit_index): # 读取图片 img = cv2.imread(image_path) # 灰度化处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 膨胀处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dilate = cv2.dilate(thresh, kernel, iterations=1) # 查找轮廓 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 找到指定的数字轮廓 digit_contour = None for i in range(len(contours)): if i == digit_index: digit_contour = contours[i] break # 如果找到了,提取字符并返回 if digit_contour is not None: x, y, w, h = cv2.boundingRect(digit_contour) digit_img = thresh[y:y+h, x:x+w] return digit_img # 示例使用 preprocess_image("test_image.png") # 对单张图片进行处理 digit_img = choose_digit("test_image.png", 0) # 提取第一个数字 cv2.imshow("Digit Image", digit_img) # 显示提取的数字 cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段代码包含两个函数: - `preprocess_image(image_path)`:输入一张图片的路径,对该图片进行字符分割和图像预处理,将每个字符分别保存到一个单独的文件中。 - `choose_digit(image_path, digit_index)`:输入一张图片的路径和数字的索引,提取该图片中指定索引的数字,返回提取出来的数字图像。 你可以根据自己的需求进一步修改代码,比如更改预处理参数、修改提取数字的方式等等。 ### 回答2: 要定义一个单张图片的字符分割和图像预处理的Python代码,可以使用OpenCV和PIL库来实现。 首先,导入所需的库: ```python import cv2 from PIL import Image ``` 然后,定义一个函数来完成字符的分割和图像预处理: ```python def image_processing(image_path): image = cv2.imread(image_path) # 读取图片 # 对图片进行字符分割 # ... # 对每个字符进行图像预处理 # ... return processed_image ``` 在该函数中,首先使用cv2.imread()函数读取输入的图片。然后,你可以使用OpenCV中的字符分割算法来对图片进行字符分割。具体的字符分割算法可以根据实际需求来选择和实现。 完成字符分割后,对于每个字符,你可以使用各种图像预处理技术进行处理,例如裁剪、调整大小、灰度化、二值化等。具体的图像预处理方法可以根据需要选择和实现。 最后,你可以将处理后的图片返回。 除了以上功能外,你还可以定义一个函数来选择单个数字或多个数字进行处理: ```python def process_single_number(image_path): return image_processing(image_path) def process_multiple_numbers(image_path_list): processed_images = [] for image_path in image_path_list: processed_images.append(image_processing(image_path)) return processed_images ``` 在这两个函数中,你可以调用image_processing()函数来处理单个或多个数字。 这样,你就可以根据需要选择单个数字或多个数字进行处理,并获取相应的处理结果。 ### 回答3: 以下是一个使用Python代码定义的单张图片的字符分割和图像预处理的函数: ```python import cv2 import pytesseract def preprocess_image(image_path): # 加载图像 image = cv2.imread(image_path) # 将彩色图像转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 使用阈值将图像转换为二值图像 _, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 执行字符分割 contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 为每个字符创建一个新的图像文件 digits = [] for contour in contours: # 计算轮廓的边界框 x, y, w, h = cv2.boundingRect(contour) # 调整边界框的大小以适应字符的大小 roi = threshold[y:y+h, x:x+w] resized_roi = cv2.resize(roi, (28, 28)) # 将字符保存到列表中 digits.append(resized_roi) return digits def recognize_digits(digits): # 初始化Tesseract OCR引擎 pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" recognized_digits = [] for digit in digits: # 使用Tesseract识别数字 digit_text = pytesseract.image_to_string(digit, config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789') # 将识别结果保存到列表中 recognized_digits.append(digit_text) return recognized_digits # 输入需要处理的图像路径 image_path = 'path_to_image.jpg' # 图像预处理和字符分割 digits = preprocess_image(image_path) # 单个数字处理 single_digit = digits[0] recognized_single_digit = recognize_digits([single_digit]) print("单个数字识别结果:", recognized_single_digit) # 多个数字处理 recognized_multiple_digits = recognize_digits(digits) print("多个数字识别结果:", recognized_multiple_digits) ``` 这个函数中,`preprocess_image`函数接收一个图片路径作为参数并返回图像处理后的字符分割结果。`recognize_digits`函数接收一个字符图像列表作为参数,并使用Tesseract OCR引擎识别每个字符,并将识别结果返回为一个列表。之后,在使用这两个函数时,可以将图像路径传递给`preprocess_image`函数进行图像预处理和字符分割。然后,可以选择是否处理单个数字或多个数字,将相应的字符图像列表传递给`recognize_digits`函数进行识别。最后,输出识别结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值