程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

(available for personal testing) Python performs batch conversion of multiple formats of images: tiff,. jpg,. png

編輯:Python

here A Format conversion B Format .

Why can't you use PIL Read tiff What about the image .

PIL Support single channel and multi-channel Uint8 TIFF Image reading , Read single channel Uint16 TIFF The image changes to Uint8 Handle , Direct reading Uint16 TIFF The image will report an error .

Use here python Version of opencv Read images , And keep .

Code up tif--- turn ---》bmp.    You can briefly modify the format according to your own needs

# With tiff turn jpg For example , Other formats are the same ,
# Change the path in the code to your own image storage path
import os
import cv2 as cv2
imagesDirectory = r"C:\Users\***\voc_make\test" # tiff The path of the folder where the picture is located
distDirectory = os.path.dirname(imagesDirectory)#
distDirectory = os.path.join(distDirectory, "bmpImages")# To store bmp Format folder path
print(distDirectory)
for imageName in os.listdir(imagesDirectory):
print("imageName", imageName)
imagePath = os.path.join(imagesDirectory, imageName)
print("imagePath", imagePath)
img = cv2.imread(imagePath)
try:
img.shape
except:
print(' Failed to read picture ')
break
print("imageName.split('.')[0]", imageName.split('.')[0])
distImagePath = os.path.join(distDirectory, imageName.split('.')[0]+'.bmp')# Change the image suffix to .jpg, And ensure the same name as the original image
print("distImagePath", distImagePath)
cv2.imwrite(distImagePath, img)

Remember to be in tif File in the same path bmp Folder

Make the path correct .

Here's the picture


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved