Python script to help with Tease AI tagging

Webteases are great, but what if you're in the mood for a slightly more immersive experience? Chat about Tease AI and other offline tease software.

Moderator: 1885

Post Reply
Rar1197
Explorer
Explorer
Posts: 46
Joined: Tue Aug 22, 2023 6:02 am

Python script to help with Tease AI tagging

Post by Rar1197 »

I imagine other people have come up with their own solutions to make tagging less tedious , but I thought I would post mine in case it was helpful.

When tagging images one of the main problems I have is working out which files I haven't done yet , especially when returning to a large folder that has been partially done previously.

This python script helps with that by finding a small number of files that haven't been tagged yet.

Basically the flow of the script is :
1) It finds up to 40 pictures that aren't included in LocalImageTags.txt yet and copies them to a temporary directory.
2) It launches Tease AI so you can tag those pictures , and waits for Tease AI to end
3) Once you close Tease AI it opens up LocalImageTags.txt and remaps the paths for the temporary files back to the original locations
4) It then deletes all the files that it copied to the temporary directory.

To use this you will need to modify the first few values in the script based on the directories you want to use.
szTeaseAIDir needs to be the directory that contains 'Tease AI.exe'
szTaggingDir should be a suitable empty temporary directory that already exists. I chose a directory on my desktop since it is less hassle to find it within Tease AI
aszSourceDirs should be one or more directories that should searched for images that may need tagging.

You should use \\ rather than / when defining the paths , since otherwise the filename comparisons probably won't work correctly.

I would also highly recommend backing up your LocalImageTags.txt before using this , in case things go pear-shaped and it nukes your previous tagging work.

If I was less lazy there would be some friendly error messages for the things that go wrong , but apparently I am not.

Code: Select all

#Path to directory containing Tease AI.exe
szTeaseAIDir = 'D:\\Tease_AI'

#Temporary directory that pictures will be copied to for tagging
szTaggingDir = 'C:\\Users\\raric\\OneDrive\\Desktop\\Tagging'

#List of directories to check for untagged pictures (subdirectories are also checked)
aszSourceDirs =[
'D:\\Tease AI Common\\Images\\Genre',
#'D:\\Blah\\SomeOtherDirectory',
]
#Maximum number of files to copy each time
uMaxFiles = 40

###############################################################
szTeaseAIExe = szTeaseAIDir + '\\Tease AI.exe'
szLocalImageTags = szTeaseAIDir + '\\Images\\System\\LocalImageTags.txt'
szLocalImageTagsTemp = szTeaseAIDir + '\\Images\\System\\LocalImageTagsTemp.txt'

###############################################################

#File extensions to check
aszImageExt=[
'.bmp',
'.jpg',
'.jpeg',
'.png',
'.gif',
]


import os
import glob
import shutil

#Find the list of currently tagged files
LocalImageTagsFile = open(szLocalImageTags,'rt')
TaggedFiles = set()
for szLine in LocalImageTagsFile:
	szLine = szLine.rstrip(' \n')
	if(len(szLine) == 0):
		continue
	iFirstTag = szLine.find(' Tag')
	if(iFirstTag != -1):
		#Strip of tags to get filename
		szFilename = szLine[:iFirstTag]
	else:
		szFilename = szLine
	TaggedFiles.add(szFilename.lower())
LocalImageTagsFile.close()	

#Find all the images in the source directories
ImageFilenames = []
for szDir in aszSourceDirs:
	ImageFilenames += glob.glob(szDir + '/**/*.*', recursive=True)


Mapping = {}
uNumFile = 0
for szImage in ImageFilenames:
	#Skip files that are already in LocalImageTags.txt
	if szImage.lower() in  TaggedFiles:
		continue
	#Ignore non-image files
	szExtension = os.path.splitext(szImage)[1]
	if not szExtension in aszImageExt:
		continue

	#Copy the file to the temporary directory
	szDestImage = szTaggingDir + '\\Image{}'.format(uNumFile) + szExtension
	Mapping[szDestImage.lower()] = szImage
	shutil.copy(szImage, szDestImage)
	uNumFile += 1
	if uNumFile >= uMaxFiles:
		break
		


#Start Tease AI and wait for it to exit
os.system('\"{}\"'.format(szTeaseAIExe))

#Create temporary tags file
TempLocalImageTagsFile = open(szLocalImageTagsTemp,'wt')

#read the tag file again
LocalImageTagsFile = open(szLocalImageTags,'rt')

for szLine in LocalImageTagsFile:
	szLine = szLine.rstrip(' \n')
	if(len(szLine) == 0):
		continue
	iFirstTag = szLine.find(' Tag')
	if(iFirstTag != -1):
		#Strip of tags to get filename
		szFilename = szLine[:iFirstTag]
	else:
		szFilename = szLine

	szSourceImage = Mapping.get(szFilename.lower());
	if szSourceImage is None:
		#Re-write the line as is
		TempLocalImageTagsFile.write(szLine)
	else:
		#Write out the original name of the file with the new tags
		TempLocalImageTagsFile.write(szSourceImage)
		if(iFirstTag != -1):
			TempLocalImageTagsFile.write(szLine[iFirstTag:])
	TempLocalImageTagsFile.write('\n')
	
LocalImageTagsFile.close()
TempLocalImageTagsFile.close()

#Delete LocalImageTags.txt
os.remove(szLocalImageTags)
#Rename the temp file
os.rename(szLocalImageTagsTemp,szLocalImageTags)

#Delete temp files
for szFile in Mapping:
	os.remove(szFile)
Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests