This commit is contained in:
Q
2024-06-03 10:46:35 +03:00
2 changed files with 25 additions and 8 deletions

View File

@@ -9,7 +9,7 @@ from imagelist2.db import DB, sqlite_sqrt, sqlite_square
from imagelist2.image import ImageMeasure, is_image_extension from imagelist2.image import ImageMeasure, is_image_extension
from tqdm import tqdm from tqdm import tqdm
__version__ = "0.0.1" __version__ = "0.0.3"
SQLFILE = "image-list.sqlite" SQLFILE = "image-list.sqlite"
# IMGMATCH = re.compile("|".join([".*\." + x + "$" |.*\.jpeg$|.*\.png$|.*\.gif$|.*\.tif$", re.I) # IMGMATCH = re.compile("|".join([".*\." + x + "$" |.*\.jpeg$|.*\.png$|.*\.gif$|.*\.tif$", re.I)
BADDIRS = ["_tn", "_med", ".tn", ".med"] BADDIRS = ["_tn", "_med", ".tn", ".med"]
@@ -26,6 +26,7 @@ class ImageList:
def recursive_add(self): def recursive_add(self):
dir_count = 0 dir_count = 0
image_count = 0
for path, dirs, files in os.walk(os.path.realpath(self.options.startpath), followlinks=self.options.symlinks): for path, dirs, files in os.walk(os.path.realpath(self.options.startpath), followlinks=self.options.symlinks):
clean_dirs(dirs) clean_dirs(dirs)
dir_count += 1 dir_count += 1
@@ -60,21 +61,25 @@ class ImageList:
if self.db.is_hash_mismatch(image): if self.db.is_hash_mismatch(image):
has_changed = True has_changed = True
if has_changed: if has_changed:
image_count += 1
self.add_single(image, change=True) self.add_single(image, change=True)
else: else:
if not self.options.no_add: if not self.options.no_add:
image_count += 1
self.add_single(image, change=False) self.add_single(image, change=False)
self.db.conn.commit() self.db.conn.commit()
if image_count > 0:
print(f"Added/changed {image_count} images")
return return
def add_single(self, image, change=False): def add_single(self, image, change=False):
if change: if change:
query = "UPDATE list SET hash=?, date=? ,size=? WHERE file=?" query = "UPDATE list SET hash=?, date=? ,size=? WHERE file=?"
error_msg = f"error adding file: {image.filename}" error_msg = f"error adding image: {image.filename}"
else: else:
query = "INSERT INTO list(hash,date,size,file) VALUES (?,?,?,?)" query = "INSERT INTO list(hash,date,size,file) VALUES (?,?,?,?)"
error_msg = f"error changing file: {image.filename}" error_msg = f"error changing image: {image.filename}"
try: try:
self.db.cursor().execute( self.db.cursor().execute(
@@ -119,13 +124,14 @@ class ImageList:
continue continue
image = ImageMeasure(filename) image = ImageMeasure(filename)
cursor.execute( cursor.execute(
"""INSERT INTO data(hash,portrait,width,height) """INSERT INTO data(hash,portrait,width,height,description)
VALUES(?,?,?,?)""", VALUES(?,?,?,?,?)""",
( (
row[0], row[0],
image.get_portrait(), image.get_portrait(),
image.get_width(), image.get_width(),
image.get_height(), image.get_height(),
image.get_description()
), ),
) )
if i % 50 == 0: if i % 50 == 0:
@@ -145,7 +151,7 @@ class ImageList:
cursor.execute("DELETE FROM list where file == ?", (file,)) cursor.execute("DELETE FROM list where file == ?", (file,))
self.db.conn.commit() self.db.conn.commit()
if len(to_delete) > 0: if len(to_delete) > 0:
print(f"Cleaned {len(to_delete)} files") print(f"Cleaned {len(to_delete)} images")
return return
def clean_data(self): def clean_data(self):
@@ -746,11 +752,11 @@ def main():
il = ImageList(options) il = ImageList(options)
if options.command == "db": if options.command == "db":
if not options.no_delete:
il.delete_missing()
if not options.no_add: if not options.no_add:
il.recursive_add() il.recursive_add()
il.base_add() il.base_add()
if not options.no_delete:
il.delete_missing()
if not options.no_delete_data: if not options.no_delete_data:
il.clean_data() il.clean_data()
if options.measure: if options.measure:

View File

@@ -111,6 +111,12 @@ class ImageMeasure:
self.portrait = self.height >= self.width self.portrait = self.height >= self.width
return self.width, self.height, self.portrait return self.width, self.height, self.portrait
def get_description(self):
if self.description is None:
self.description = read_image_comment(self.filename)
return self.description
def get_image(self, image_type="numpy"): def get_image(self, image_type="numpy"):
if self.image is None: if self.image is None:
@@ -227,3 +233,8 @@ def read_image_size(fname):
"""Just reading the size is faster with PIL""" """Just reading the size is faster with PIL"""
im = Image.open(fname) im = Image.open(fname)
return im.width, im.height return im.width, im.height
def read_image_comment(fname):
"""Just reading the comment with PIL"""
im = Image.open(fname)
return im.info.get('comment','')