Tag images based on gallery/studio/performer

Is there any way for tags that I give to a gallery (or studio or performer) to trickle down to the images itself?

Not with Stash graphical interface, I don’t see any plugin for it either, but I might be blind.

There is a scraper that will do this: CommunityScrapers/scrapers/CopyToImages at master · stashapp/CommunityScrapers · GitHub

It’s a bit of a hack and maybe better suited to a plugin but it does work. Just install this scraper, and then open your Gallery, go to the edit tab, and scrape with “Copy to Images”.

Note that you can edit the CopyToImages.py file and change the flags at the top to change what is copied over to the Images.

3 Likes

Not used to how scrapers work but it seems that I already had that installed. Is there anything else I need to do to get it working?

I posted this on Discord in response to someone making a similar request…

If you don’t mind dropping down to SQL, database triggers are ideal for this use case.

The example below is a trigger that executes a bit of SQL when an update is made to the galleries table. When updating a gallery’s date, studio or photographer - the changes will automatically propagate to all images associated with the gallery.

CREATE TRIGGER update_image_from_gallery 
AFTER UPDATE OF studio_id, date, photographer ON galleries
  BEGIN
    UPDATE images
    SET studio_id = NEW.studio_id,
        date = NEW.date,
        photographer = NEW.photographer,
        updated_at = NEW.updated_at
    FROM images AS i
    WHERE i.id = images.id
      AND i.id IN (SELECT image_id FROM galleries_images WHERE gallery_id = NEW.id);
  END;

Doing the same for tags is a little trickier, but totally doable.

Update! I’ve included two SQL triggers below, they do the following:

  1. Adds the same tag to all images in a gallery when a tag is added.
  2. Removes a tag from all images in a gallery if the tag is removed.

Also, because it’s just plain ol’ SQL, there’s no plugins or third-party dependencies needed!


I’ve been testing it on my own Stash instance and it works really well. Tags are instantly visible on images after updating a gallery and removing a tag also works as you’d expect.

stash-recording

Trigger 1: Adding tags

CREATE TRIGGER trg_gallery_tags_add_to_images AFTER INSERT ON galleries_tags
BEGIN
  INSERT INTO images_tags (tag_id, image_id)
    SELECT NEW.tag_id, img.image_id FROM galleries_images AS img
    WHERE img.gallery_id = NEW.gallery_id;
END;

Trigger 2: Removing tags

CREATE TRIGGER trg_gallery_tags_remove_from_images AFTER DELETE ON galleries_tags
BEGIN
  DELETE FROM images_tags
  WHERE tag_id = OLD.tag_id
    AND image_id IN (SELECT img.image_id FROM galleries_images AS img WHERE img.gallery_id = OLD.gallery_id);
END;

If you want to remove/disable the triggers:

DROP TRIGGER trg_gallery_tags_add_to_images;
DROP TRIGGER trg_gallery_tags_remove_from_images;
3 Likes