python_apps/crwlers/imagemagick.py
2023-11-03 14:49:12 +09:00

49 lines
1.8 KiB
Python

import subprocess, re, os
#11666_0.JPG
file = "/home/maddiekorea/imgs/crawl/_GLOBALNET_PRODUCT_new_test/"
#converted = "./convered.JPG"
files = os.listdir(file)
for i in range(len(files)):
filename = file + files[i]
sizeInfo = os.stat(filename).st_size
#print(filename + "\t" + str(sizeInfo))
if sizeInfo == 0:
removeCmd = "rm -f " + filename
#excuteRemove = subprocess.Popen(removeCmd,stdout=subprocess.PIPE,shell=True)
#print(removeCmd)
logsize = "echo \"Not found : " + filename + "\" >> ./errors.log"
excutelog = subprocess.Popen(logsize,stdout=subprocess.PIPE,shell=True)
else:
extension = filename.split(".")
extension = extension[len(extension)-1]
if extension == "GIF":
cmd = "GIF FUCK"
loggif = "echo \"GIF FUCK : " + filename + "\" >> ./errors.log"
excutelog = subprocess.Popen(loggif,stdout=subprocess.PIPE,shell=True)
else:
try:
identify = subprocess.check_output("identify " + filename, shell=True)
identify = identify.decode()
identify = re.sub('^[\.\/0-9_A-Za-z\-\ ]+\s.[A-Z]+\s',"",identify)
identify = re.sub('\ .+\n',"",identify)
identify = identify.split("x")
width = int(identify[0])
height = int(identify[1])
maxValue = max(identify)
converted = filename + ".JPG"
cmd = "convert " + filename + " -background white -gravity center -extent " + maxValue + "x" + maxValue +" -quality 90 -resize \"1000x>\" " + converted + ";"
moveCmd = "mv " + converted + " " + filename + ";"
cmd = cmd + moveCmd
except subprocess.CalledProcessError:
cmd = "error : malformat"
logfile = "echo \"GIF FUCK : " + filename + "\" >> ./errors.log"
excutelog = subprocess.Popen(logfile,stdout=subprocess.PIPE,shell=True)
#convertCrawl = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
#print(cmd)
#
#print(str(width) + " " + str(height) + " " + str(maxValue))
#print(cmd)