这篇文章主要介绍“Python图像基本操作有哪些”,在日常操作中,相信很多人在Python图像基本操作有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python图像基本操作有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
from libtiff import TIFF
tif = TIFF.open('filename.tif', mode='r') #打开tiff文件进行读取
image = tif.read_image() #读取图像并作为numpy数组返回
for image in tif.iter_images() #读取TIFF文件中的所有图像
tif = TIFF.open('filename.tif', mode='w') #打开tiff文件进行写入
tif.write_image(image) #将图像写入tiff文件
from libtiff import TIFFfile, TIFFimage
tif = TIFFfile('filename.tif') #读取图片
samples, sample_names = tiff.get_samples()
tiff = TIFFimage(data, description='')
tiff.write_file('filename.tif', compression='none') # or 'lzw'
del tiff # 刷新(释放缓存)
import cv2
cv2.imread("filename",flags)
=====================其中:flags四种选择如下:==================
IMREAD_UNCHANGED = -1 #不进行转化,比如保存为了16位的图片,读取出来仍然为16位。
IMREAD_GRAYSCALE = 0 #转化为灰度图,比如保存为了16位的图片,读取出来为8位,类型为CV_8UC1。
IMREAD_COLOR = 1 #进行转化为RGB三通道图像,图像深度转为8位
IMREAD_ANYDEPTH = 2 #保持图像深度不变,进行转化为灰度图。
IMREAD_ANYCOLOR = 4 #若通道数小于等于3,则保持不变;若通道数大于3则只取取前三个通道。图像深度转为8位
对于多通道TIFF图像,若要保证图像数据的正常读取,显然要选择IMREAD_UNCHANGED
from PIL import Image
img0 = Image.open("D:/python_script/ffff/11lalala.jpg")
img1 = Image.open("D:/python_script/ffff/42608122.tif")
img2 = Image.open("D:/python_script/ffff/42608122_1.jpg") #这张图片是直接修改上张图的后缀名
print ("图片格式:{0},图片大小:{1},图片模式:{2}".format(img0.format,img0.size,img0.mode))
print ("图片格式:{0},图片大小:{1},图片模式:{2}".format(img1.format,img1.size,img1.mode))
print ("图片格式:{0},图片大小:{1},图片模式:{2}".format(img2.format,img2.size,img2.mode))
输出:#说明直接修改图片后缀名,图片的编码格式并没有改变
图片格式:JPEG,图片大小:(245, 213),图片模式:RGB
图片格式:TIFF,图片大小:(2480, 3508),图片模式:YCbCr
图片格式:TIFF,图片大小:(2480, 3508),图片模式:YCbCr
import PIL.Image
import os
def convert(input_dir,output_dir):
for filename in os.listdir(input_dir):
path = input_dir+"/"+filename
print("doing... ",path)
PIL.Image.open(path).save(output_dir+"/"+filename[:-4]+".jpg")
print ("%s has been changed!"%filename)
if __name__ == '__main__':
input_dir = "D:/classifier_data20181225/img1"
output_dir = "D:/classifier_data20181225/img2"
convert(input_dir,output_dir)
遇到分辨率大,图片文件大小并不大的文件,opencv打不开,此时用到了以下代码用来缩小图片。
若文件宽大于1200,(高度小于1800)以此宽度等比缩放
若文件高大于1800,(宽度小于1200)以此高度等比缩放
import os
from PIL import Image
import shutil
def get_img(input_dir):
img_path_list = []
for (root_path,dirname,filenames) in os.walk(input_dir):
for filename in filenames:
img_path = root_path+"/"+filename
img_path_list.append(img_path)
print("img_path_list",img_path_list)
return img_path_list
def process_image(filename,output_dir, mwidth=1200, mheight=1800):
image = Image.open(filename)
w, h = image.size
if w <= mwidth and h <= mheight:
print(filename, 'is OK.')
shutil.move(filename, output_dir+filename[-15:])
return
if (1.0 * w / mwidth) > (1.0 * h / mheight):
scale = 1.0 * w / mwidth
new_im = image.resize((int(w / scale), int(h / scale)), Image.ANTIALIAS)
else:
scale = 1.0 * h / mheight
new_im = image.resize((int(w / scale), int(h / scale)), Image.ANTIALIAS)
new_im.save(output_dir+filename[-15:])
new_im.close()
if __name__ == '__main__':
input_dir = "D:/classifier_data20181212/lipei_resize_1"
output_dir = "D:/classifier_data20181212/lipei_resize/"
img_path_list = get_img(input_dir)
for filename in img_path_list:
print("filename",filename)
process_image(filename,output_dir)
到此,关于“Python图像基本操作有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。