python 读取jpg图片

本文介绍了如何使用Pillow、matplotlib和OpenCV库在Python中读取和显示图片,以及它们在Jupyter环境中的适用性。Pillow用于基本操作,matplotlib适合在Jupyter中显示,而OpenCV可能导致显示问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pillow读取图片

from PIL import Image
import numpy as np 

img_path = './Training/meningioma/M546.jpg'
# 读取图片
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))

print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
#显示图片
image.show()
# 

在这里插入图片描述

matplotlib读取图片

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img_path = './Training/meningioma/M546.jpg'
# 打开PNG图片文件
png_image = mpimg.imread(img_path)
plt.imshow(png_image)

在这里插入图片描述

CV2读取图片

import cv2 

import numpy as np 

img_path = './Training/meningioma/M546.jpg'
# 读取图片
img = cv2.imread(img_path)
print(img.shape)

# mac显示图片有问题,会卡死,图片无法关闭,

在这里插入图片描述

注意点

如果在jupyter中,最好使用matplotlib就可以
因为如果使用PIL库,图片是以窗口的形式展示的
而cv2的结果是不显示,所以最好使用matploblib可以在jupyter中显示图片

我又发现了一个问题,就是原始的jpg图片和在程序中显示的维度是反过来的,比如下面的例子

原始图片

在这里插入图片描述

pillow读取图片

from PIL import Image
import numpy as np 

img_path = './pixel_test.jpg'
# 读取图片
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))

print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
#显示图片
image.show()
# 

可以看到,图片的数组大小是(1946,2921,3),而原始的图片的大小是2921*1946,这个有点奇怪的哈,反正记住是倒过来的就行了

matplotlib显示图片

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img_path = './pixel_test.jpg'
# 打开PNG图片文件
png_image = mpimg.imread(img_path)
plt.imshow(png_image)
print(png_image.shape)

结果如下
在这里插入图片描述

cv2显示图片

import cv2 

import numpy as np 

img_path = './pixel_test.jpg'
# 读取图片
img = cv2.imread(img_path)
print(img.shape)
# mac显示图片有问题,会卡死,图片无法关闭,

在这里插入图片描述
所以无论你使用哪种方法读取,这个图片的大小就是会翻转过来的,为什么会造成这种差异呢?因为图片默认的坐标轴和我们印象中的坐标轴是不一样的,图片处理的坐标轴是从左上角开始的,而不是数学中的左下角开始的,这个需要注意的,可以看下面的测试例子

测试

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img_path = './pixel_test.jpg'
# 打开PNG图片文件
png_image = mpimg.imread(img_path)
img_arr = np.array(png_image)
img_arr[0:100,100:200,:]=[255,0,0]
plt.imshow(img_arr)
#print(png_image.shape)

在这里插入图片描述
但是这里注意一个问题,直接使用imread()读取的png_image是不能赋值的,需要要注意哈
在这里插入图片描述
但是这个数据经过复制后就可以进行赋值了,原因在于连个数据的flags是不一样的
在这里插入图片描述

load_image函数

from PIL import Image
import numpy as np

def load_image(filename, verbose=True,show=True):
    img = Image.open(filename)
    if show:
        img.show()
    img = np.array(img)
    if img.ndim == 3 and img.shape[-1] == 4:
        img = img[..., :3]  # remove alpha channel
    if verbose:
        print(f'Image loaded from {filename}')
    return img

img = load_image("../RN49-1/he.jpg")
print(img.shape)
lending club 贷款数据 2018年第二季度的贷款数据 "id","member_id","loan_amnt","funded_amnt","funded_amnt_inv","term","int_rate","installment","grade","sub_grade","emp_title","emp_length","home_ownership","annual_inc","verification_status","issue_d","loan_status","pymnt_plan","url","desc","purpose","title","zip_code","addr_state","dti","delinq_2yrs","earliest_cr_line","inq_last_6mths","mths_since_last_delinq","mths_since_last_record","open_acc","pub_rec","revol_bal","revol_util","total_acc","initial_list_status","out_prncp","out_prncp_inv","total_pymnt","total_pymnt_inv","total_rec_prncp","total_rec_int","total_rec_late_fee","recoveries","collection_recovery_fee","last_pymnt_d","last_pymnt_amnt","next_pymnt_d","last_credit_pull_d","collections_12_mths_ex_med","mths_since_last_major_derog","policy_code","application_type","annual_inc_joint","dti_joint","verification_status_joint","acc_now_delinq","tot_coll_amt","tot_cur_bal","open_acc_6m","open_act_il","open_il_12m","open_il_24m","mths_since_rcnt_il","total_bal_il","il_util","open_rv_12m","open_rv_24m","max_bal_bc","all_util","total_rev_hi_lim","inq_fi","total_cu_tl","inq_last_12m","acc_open_past_24mths","avg_cur_bal","bc_open_to_buy","bc_util","chargeoff_within_12_mths","delinq_amnt","mo_sin_old_il_acct","mo_sin_old_rev_tl_op","mo_sin_rcnt_rev_tl_op","mo_sin_rcnt_tl","mort_acc","mths_since_recent_bc","mths_since_recent_bc_dlq","mths_since_recent_inq","mths_since_recent_revol_delinq","num_accts_ever_120_pd","num_actv_bc_tl","num_actv_rev_tl","num_bc_sats","num_bc_tl","num_il_tl","num_op_rev_tl","num_rev_accts","num_rev_tl_bal_gt_0","num_sats","num_tl_120dpd_2m","num_tl_30dpd","num_tl_90g_dpd_24m","num_tl_op_past_12m","pct_tl_nvr_dlq","percent_bc_gt_75","pub_rec_bankruptcies","tax_liens","tot_hi_cred_lim","total_bal_ex_mort","total_bc_limit","total_il_high_credit_limit","revol_bal_joint","sec_app_earliest_cr_line","sec_app_inq_last_6mths","sec_app_mort_acc","sec_app_open_acc","sec_app_revol_util","sec_app_open_act_il","sec_app_num_rev
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值