python放大图形中部分内容_python opencv对图像主体部分放大

这篇博客介绍了如何使用Python和OpenCV库来读取图像,并针对图像中的白色部分进行处理。首先,读取图像并获取特定像素的颜色值,然后遍历图像计算白色像素的比例。当白色像素比例小于某个阈值时,不断调整裁剪区域,直到满足条件。最后,对裁剪后的图像进行放大并保存。
摘要由CSDN通过智能技术生成

# encoding:utf-8

import cv2

image = cv2.imread("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674.png") # 读取图像

image_original = cv2.imread("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674.jpg") # 读取图像

(b, g, r) = image[200, 20] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的

print "位置(0,0)处的像素 - 红:%d,绿:%d,蓝:%d" % (r, g, b) # 显示像素值

def access_pixels(frame):

print(frame.shape) # shape内包含三个元素:按顺序为高、宽、通道数

height = frame.shape[0]

weight = frame.shape[1]

channels = frame.shape[2]

print("weight :%s, height :%s, channel :%s" % (weight, height, channels))

white_count = 0

for row in range(height): # 遍历高

for col in range(weight): # 遍历宽

for c in range(channels): # 便利通道

(b, g, r) = frame[row, col] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的

if r==255: #白色部分

white_count=white_count+1

# pv = frame[row, col, c]

# frame[row, col, c] = 255 - pv # 全部像素取反,实现一个反向效果

print("white point is :%s" % (white_count))

size = float(white_count)/518400

print("size is :%s" % (size))

#cv2.imshow("fanxiang", frame)

#cv2.waitKey(0)

cropped = image

cropped_original = image_original

height_start_original = 0

height_end_original = 720

weight_start_original = 0

weight_end_original = 720

while size < 0.03:

height_start = (height_end_original-height_start_original)/6

height_end = (height_end_original-height_start_original)*5/6

weight_start = (weight_end_original-weight_start_original) / 6

weight_end = (weight_end_original-weight_start_original)* 5 / 6

height_start_original = height_start

height_end_original = height_end

weight_start_original = weight_start

weight_end_original = weight_end

print("height_start:%s" % height_start)

print("height_end:%s" % height_end)

print("weight_start:%s" % weight_start)

print("weight_end:%s" % weight_end)

cropped = cropped[height_start:height_end, weight_start:weight_end] # 裁剪坐标为[y0:y1, x0:x1]

cropped_original = cropped_original[height_start:height_end, weight_start:weight_end]

cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new1.png", cropped)

cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new1.jpg", cropped_original)

for row in range(height): # 遍历高

for col in range(weight): # 遍历宽

for c in range(channels): # 便利通道

(b, g, r) = frame[row, col] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的

if r == 255: # 白色部分

white_count = white_count + 1

# pv = frame[row, col, c]

# frame[row, col, c] = 255 - pv # 全部像素取反,实现一个反向效果

print("white point is :%s" % (white_count))

size = float(white_count) / (height*weight)

print("size is :%s" % (size))

cropped_changesize = cropped

cropped_save = cv2.resize(cropped_changesize,(720, 720))

cropped_changesize2 = cropped_original

cropped_save2 = cv2.resize(cropped_changesize2, (720, 720))

cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new2.png", cropped_save)

cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new2.jpg", cropped_save2)

access_pixels(image)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值