# Buggy code
def remove_excluded_image(self, img, input_folder):
    # Remove excluded images
    # img is something like input_folder/demo/tesla2_lg.jpg so it's the *source* path
    # and we should remove both the large and thumbnail *destination* paths

    output_folder = os.path.dirname(
        os.path.join(
            self.kw["output_folder"], self.site.path("gallery", os.path.dirname(img))
        )
    )
    img = os.path.relpath(img, input_folder)
    img_path = os.path.join(output_folder, os.path.basename(img))
    fname, ext = os.path.splitext(img_path)
    thumb_path = fname + ".thumbnail" + ext

    yield utils.apply_filters(
        {
            "basename": "_render_galleries_clean",
            "name": thumb_path,
            "actions": [(utils.remove_file, (thumb_path,))],
            "clean": True,
            "uptodate": [
                utils.config_changed(
                    self.kw, "nikola.plugins.task.galleries:clean_thumb"
                )
            ],
        },
        self.kw["filters"],
    )

    yield utils.apply_filters(
        {
            "basename": "_render_galleries_clean",
            "name": img_path,
            "actions": [(utils.remove_file, (img_path,))],
            "clean": True,
            "uptodate": [
                utils.config_changed(
                    self.kw, "nikola.plugins.task.galleries:clean_file"
                )
            ],
        },
        self.kw["filters"],
    )


# Traceback (IOError)
nikola | /h/@/P/g/msx.github.io ‹master*›> nikola build; nikola deploy; nikola github_deploy
Scanning posts....done!
.  render_galleries:output/en/galleries/demo/index.html
########################################
TaskError - taskid:render_galleries:output/en/galleries/demo/index.html
PythonAction Error
Traceback (most recent call last):
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/doit/action.py", line 372, in execute
returned_value = self.py_callable(*self.args, **kwargs)
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/nikola/plugins/task/galleries.py", line 533, in render_gallery_index
im = Image.open(thumb)
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/PIL/Image.py", line 2248, in open
fp = builtins.open(fp, "rb")
IOError: [Errno 2] No such file or directory: u'output/galleries/demo/tesla4_lg.thumbnail.jpg'



Scanning posts....done!
[2015-05-16T07:25:21Z] INFO: deploy: => preset 'default'
[2015-05-16T07:25:21Z] INFO: deploy: Successful deployment
Scanning posts....done!
.  render_galleries:output/en/galleries/demo/index.html
########################################
TaskError - taskid:render_galleries:output/en/galleries/demo/index.html
PythonAction Error
Traceback (most recent call last):
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/doit/action.py", line 372, in execute
returned_value = self.py_callable(*self.args, **kwargs)
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/nikola/plugins/task/galleries.py", line 533, in render_gallery_index
im = Image.open(thumb)
File "/home/@msx/.virtualenvs/nikola/lib/python2.7/site-packages/PIL/Image.py", line 2248, in open
fp = builtins.open(fp, "rb")
IOError: [Errno 2] No such file or directory: u'output/galleries/demo/tesla4_lg.thumbnail.jpg'



[2015-05-16T07:25:23Z] ERROR: github_deploy: Build failed, not deploying to GitHub

# Fixed code
def remove_excluded_image(self, img, input_folder):
    # Remove excluded images
    # img is something like input_folder/demo/tesla2_lg.jpg so it's the *source* path
    # and we should remove both the large and thumbnail *destination* paths

    output_folder = os.path.dirname(
        os.path.join(
            self.kw["output_folder"],
            self.site.path("gallery_global", os.path.dirname(img)),
        )
    )
    img = os.path.relpath(img, input_folder)
    img_path = os.path.join(output_folder, os.path.basename(img))
    fname, ext = os.path.splitext(img_path)
    thumb_path = fname + ".thumbnail" + ext

    yield utils.apply_filters(
        {
            "basename": "_render_galleries_clean",
            "name": thumb_path,
            "actions": [(utils.remove_file, (thumb_path,))],
            "clean": True,
            "uptodate": [
                utils.config_changed(
                    self.kw, "nikola.plugins.task.galleries:clean_thumb"
                )
            ],
        },
        self.kw["filters"],
    )

    yield utils.apply_filters(
        {
            "basename": "_render_galleries_clean",
            "name": img_path,
            "actions": [(utils.remove_file, (img_path,))],
            "clean": True,
            "uptodate": [
                utils.config_changed(
                    self.kw, "nikola.plugins.task.galleries:clean_file"
                )
            ],
        },
        self.kw["filters"],
    )