文章目录

  • 一、图片 读、写、显示、属性查看
    • libtiff 包装器
    • Python 模块
    • opencv 模块
    • PIL 模块
    • 直接修改图片格式
    • 大(分辨率大)图片缩小
    • 与上面代码同效
  • 二、PSD图像读取与保存

关于图像处理的模块常用的有 PIL,openCV等,不过应为要处理 tif 格式的图片,故特来写下这篇博客。
关于安装模块 libtiff
直接 pip install libtiff 安装模块,发现无法导入,显示“No module named libtiff” ,打开anaconda prompt 执行 conda list显示模块确实已经安装。尝试了把libtiff移除再重装还是没解决。

直接下载模块安装:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 注意下载适配的python版本,与win系统

一、图片 读、写、显示、属性查看

libtiff 包装器
from libtiff import TIFFtif = 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文件
Python 模块
from libtiff import TIFFfile, TIFFimagetif = TIFFfile('filename.tif')              #读取图片
samples, sample_names = tiff.get_samples()
tiff = TIFFimage(data, description='')tiff.write_file('filename.tif', compression='none') # or 'lzw'
del tiff                                            # 刷新(释放缓存)
opencv 模块
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
PIL 模块
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 osdef convert(input_dir,output_dir):for filename in os.listdir(input_dir):path = input_dir+"/"+filenameprint("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 shutildef 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+"/"+filenameimg_path_list.append(img_path)print("img_path_list",img_path_list)return  img_path_listdef process_image(filename,output_dir, mwidth=1200, mheight=1800):image = Image.open(filename)w, h = image.sizeif w <= mwidth and h <= mheight:print(filename, 'is OK.')shutil.move(filename, output_dir+filename[-15:])returnif (1.0 * w / mwidth) > (1.0 * h / mheight):scale = 1.0 * w / mwidthnew_im = image.resize((int(w / scale), int(h / scale)), Image.ANTIALIAS)else:scale = 1.0 * h / mheightnew_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)
与上面代码同效
# -*- coding: utf-8 -*-
import os
from PIL import Imageclass image_aspect():def __init__(self, image_file, aspect_width, aspect_height):self.img = Image.open(image_file)self.aspect_width = aspect_widthself.aspect_height = aspect_heightself.result_image = Nonedef change_aspect_rate(self):img_width = self.img.size[0]img_height = self.img.size[1]if (img_width / img_height) > (self.aspect_width / self.aspect_height):rate = self.aspect_width / img_widthelse:rate = self.aspect_height / img_heightrate = round(rate, 1)print(rate)self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))return selfdef past_background(self):self.result_image = Image.new("RGB", [self.aspect_width, self.aspect_height], (0, 0, 0, 255))self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((self.aspect_height - self.img.size[1]) / 2)))return selfdef save_result(self, file_name):self.result_image.save(file_name)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+"/"+filenameimg_path_list.append(img_path)print("img_path_list",img_path_list)return  img_path_listif __name__ == '__main__':input_dir = "D:/classifier_data20181212/img"output_dir = "D:/classifier_data20181212/img2/"img_path_list = get_img(input_dir)for filename in img_path_list:print("filename",filename)image_aspect(filename, 1200, 1600)\.change_aspect_rate()\.past_background()\.save_result(output_dir+filename[-13:])

二、PSD图像读取与保存

from psd_tools import PSDImagepsd1 = PSDImage.load('200x800.ai.psd')
psd1.as_PIL().save('psd_image_to_detect1.png')

OpenCV—Python PyLibTiff_psd 图像基本操作以及图像格式转换相关推荐

  1. python读取图片文件显示_Python—图像基本操作以及图像格式转换

    关于图像处理的模块常用的有 PIL,openCV等,不过应为要处理 tif 格式的图片,故特来写下这篇博客. 关于安装模块 libtiff 直接pip install libtiff 安装模块,发现无 ...

  2. python基础教程:Python图像处理库PIL中图像格式转换的实现

    这篇文章主要介绍了Python图像处理库PIL中图像格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 在数字图像处理 ...

  3. Python图像处理库PIL中图像格式转换(一)

    参考:https://blog.csdn.net/icamera0/article/details/50843172 在数字图像处理中,针对不同的图像格式有其特定的处理算法.所以,在做图像处理之前,我 ...

  4. openCV—Python(6)—— 图像算数与逻辑运算

    openCV-Python(6)-- 图像算数与逻辑运算 一.函数简介 1.add-图像矩阵相加 函数原型:add(src1, src2, dst=None, mask=None, dtype=Non ...

  5. OpenCV python 提取图像内的三色

    OpenCV python 提取图像内的三色 原图 [opencv.jpg] import cv2 import numpy as npdef main():# 1.导入图片img_src = cv2 ...

  6. python opencv 图像切割_【OpenCV+Python】图像的基本操作与算术运算

    图像的基本操作 在上个教程中,我们介绍了使用鼠标画笔的功能.本次教程,我们将要谈及OpenCV图像处理的基本操作. 本次教程的所有操作基本上都和Numpy相关,而不是与OpenCV相关.要使用Open ...

  7. opencv PIL读取图像得到的图像格式

    文章目录 opencv读取图像 rbg格式和gbr HWC和CHW opencv读取图像 cv2.imread() # Load an color image in grayscale img = c ...

  8. Opencv学习笔记——图像基本操作

    文章目录 前言 一.数据读取-图像 1.读取图像 2.读取部分图像 二.数据读取-视频 三.颜色通道提取 四.边界填充 五.数值计算 六.图像融合 前言 先说一些图像的基本知识: (1)图像由像素构成 ...

  9. Python图像处理库PIL中图像格式转换

    在数字图像处理中,针对不同的图像格式有其特定的处理算法.所以,在做图像处理之前,我们需要考虑清楚自己要基于哪种格式的图像进行算法设计及其实现.本文基于这个需求,使用python中的图像处理库PIL来实 ...

最新文章

  1. C#-DataSet和DataTable详解
  2. linux中截断日志
  3. 判断一个数是否是2的幂
  4. 使用Gradle构建和应用AST转换
  5. 背景位置 background-position 0916
  6. 电容触摸按键实验(STM32F407)
  7. 游戏素材制作篇(一)——使用PR将视频或者动图转化为图片
  8. 甘特图制定项目计划,提高项目管理效率
  9. java kdj macd_MACD、BOLL、KDJ 三大组合精准把握趋势与买卖!
  10. 行为识别:让机器学会“察言观色”第一步
  11. java基本数据类型长度
  12. 手动埋点转无痕埋点,如何做到代码“零”入侵
  13. TOEFL 托福综合写作模板【高级版+低级版】
  14. 嵌入式Linux下LCD应用编程: 调用giflib库解码显示GIF动态图
  15. 【加装硬盘如何迁移系统,简单高效安全,一步搞定】
  16. dns dnsmasq容器化部署
  17. 局域网内的攻击—Arp欺骗
  18. 软件工程教程:第4章总体设计 课后习题
  19. F3arRa1n更新4.4.3版本,支持IOS12.0~14.8免费绕过
  20. 顶级学霸的武汉大学经历

热门文章

  1. IDEA中出现module not specified异常如何jiejue
  2. 名悦集团:暴雨过后车辆如何保养?
  3. Google I/O 2021 发布 Flutter 2.2
  4. 手机技巧:手机只剩20%电量?有了这几招,多用2小时
  5. 如何开放防火墙端口?
  6. Grafana 教程 - 构建你的第一个仪表盘
  7. 全国计算机等级考试二级教程Python(2019)编程题参考答案
  8. windows系统下MySQL中遇到1045问题
  9. 为单身狗推荐一个相亲公众号,靠谱!真实
  10. This application has requested the Runtime to terminate it in an unusual way.