read images at add, to figure out broken images

This commit is contained in:
q
2025-06-12 19:25:43 +03:00
parent 319e8584c8
commit 4fe95c765f
3 changed files with 25 additions and 30 deletions

View File

@@ -10,7 +10,7 @@ from imagelist2.db import DB, DBCachedWriter, sqlite_sqrt, sqlite_square
from imagelist2.image import ImageBrokenError, ImageMeasure, is_image_extension from imagelist2.image import ImageBrokenError, ImageMeasure, is_image_extension
from tqdm import tqdm from tqdm import tqdm
__version__ = "0.0.9" __version__ = "0.0.10"
SQLFILE = "image-list.sqlite" SQLFILE = "image-list.sqlite"
BADDIRS = ["_tn", "_med", ".tn", ".med"] BADDIRS = ["_tn", "_med", ".tn", ".med"]
MINSIZE = 0 MINSIZE = 0
@@ -199,10 +199,9 @@ class ImageList:
def measure(self): def measure(self):
duplicates = set() duplicates = set()
missing_measurements = ( cursor = self.db.cursor()
self.db.cursor() missing_measurements = cursor.execute(
.execute( """
"""
SELECT SELECT
list.file, list.file,
data.hash, data.hash,
@@ -227,11 +226,8 @@ class ImageList:
data.broken IS FALSE OR data.broken IS NULL data.broken IS FALSE OR data.broken IS NULL
) )
""" """
)
.fetchall()
) )
if len(missing_measurements) == 0:
return
for i, row in enumerate(tqdm(missing_measurements, desc="Measure", delay=1, smoothing=0.01)): for i, row in enumerate(tqdm(missing_measurements, desc="Measure", delay=1, smoothing=0.01)):
filename = row[0] filename = row[0]
if filename == None: if filename == None:
@@ -261,9 +257,9 @@ class ImageList:
raise ImageBrokenError() raise ImageBrokenError()
except ImageBrokenError: except ImageBrokenError:
self.db_writer.execute( self.db_writer.execute(
"""UPDATE data SET broken = ? """ UPDATE data SET broken = ?
WHERE hash = ? WHERE hash = ?
""", """,
( (
image.broken, image.broken,
image.hash, image.hash,
@@ -272,7 +268,8 @@ class ImageList:
continue continue
self.db_writer.execute( self.db_writer.execute(
"""UPDATE data SET """
UPDATE data SET
p_hash = ?, p_hash = ?,
sharpness = ?, sharpness = ?,
R = ?, R = ?,
@@ -282,8 +279,8 @@ class ImageList:
BG = ?, BG = ?,
BB = ?, BB = ?,
broken = ? broken = ?
WHERE hash = ? WHERE hash = ?
""", """,
( (
image.p_hash, image.p_hash,
image.sharpness, image.sharpness,

View File

@@ -96,21 +96,12 @@ class DB:
cursor.execute("UPDATE data SET broken = ?;", (False,)) cursor.execute("UPDATE data SET broken = ?;", (False,))
db.commit() db.commit()
if config_version == "0.0.7": # => 0.0.8 with sqlite3.connect(self.sqlfile, timeout=30) as db:
with sqlite3.connect(self.sqlfile, timeout=30) as db: cursor = db.cursor()
cursor = db.cursor() cursor.execute("UPDATE config SET value = ? WHERE key = ?;", (running_version, "version"))
config_version = "0.0.8" db.commit()
cursor.execute("UPDATE config SET value = ? WHERE key = ?;", (config_version, "version"))
db.commit()
if config_version == "0.0.8": # => 0.0.9 print(f"Migrated to {running_version}. Restart", file=sys.stderr)
with sqlite3.connect(self.sqlfile, timeout=30) as db:
cursor = db.cursor()
config_version = "0.0.9"
cursor.execute("UPDATE config SET value = ? WHERE key = ?;", (config_version, "version"))
db.commit()
print(f"Migrated to {config_version}. Restart", file=sys.stderr)
def connect(self): def connect(self):
conn = sqlite3.connect(self.sqlfile, timeout=30) conn = sqlite3.connect(self.sqlfile, timeout=30)

View File

@@ -68,12 +68,19 @@ class ImageMeasure:
self.filename = os.path.realpath(self.filename) self.filename = os.path.realpath(self.filename)
def is_broken(self): def is_broken(self):
"""Note: Size reading does not necessarily mean file is okay for reading"""
if self.broken is None: if self.broken is None:
self.broken = False
try: try:
read_image_size(self.filename) read_image_size(self.filename)
self.broken = False
except Exception: except Exception:
self.broken = True self.broken = True
if not self.broken:
try:
self.get_image(image_type="numpy")
self.get_image(image_type="PIL")
except Exception:
self.broken = True
return self.broken return self.broken
def get_hash(self): def get_hash(self):