Python to determine the image format and conversion, the conversion of the image saved to the generated folder

Time:2021-1-14

I don’t want to talk much nonsense. Let’s go straight to the code!

import Image
from datetime import datetime
import os
str = '/home/dltest/caffe/examples/sgg_datas/images/result_test/zutest/' + datetime.now().strftime("%Y%m%d_%H%M%S")
 
while True==os.path.exists(str):
  str = str + datetime.now().strftime("%Y%m%d_%H%M%S")
  
os.makedirs (STR) # create folder
 
imageFile = '/home/dltest/caffe/examples/sgg_ datas/images/result_ Test / zutest / 20form1. BMP '# image path
 
import imghdr
imgType =  imghdr.what (imagefile) # get the image type, return BMP, JPG, etc
Type1 = CMP (imgtype, 'BMP') # judge whether it is BMP, JPG type. If the characters are equal, the return value is 0 
type2 = cmp(imgType,'jpeg') 
type3 = cmp(imgType,'jpg') 
Type = type1 * type2 * type3 # judge whether it is one of the three types
if type != 0 : 
  #Image type conversion, conversion to JPG format
  im = Image.open(imageFile) 
  STR2 = STR + '/' + '1. JPG' #, the original 1. JPG will be replaced automatically
  im.save(str2) 
 
print('1')

The above Python to determine the image format and conversion, the conversion of the image saved to the generated folder is Xiaobian to share with you all the content, I hope to give you a reference, also hope you can support developer.

Recommended Today

Kotlin will support wasm-wasm weekly report 1014 with the combination of 64 bit wasm, wasm FAAS, wasm and AI

Editor’s note: first, webassembly has been making great efforts in the fields of serverless and cloud computing. We have seen the application of 64 bit webassembly, serverless based on webassembly, and webassembly in the field of AI. ​ WebAssembly 64 bit webassembly Memory64 of 64 bit webassembly is being implemented step by step! 64 bit […]