Crunch icon indicating copy to clipboard operation
Crunch copied to clipboard

Add option to overwrite original

Open ajimix opened this issue 6 years ago • 17 comments

Normally I want just to compress the original exported assets so would be nice to have an "overwrite original" option

Thanks for the great work 👍

ajimix avatar Apr 08 '18 08:04 ajimix

This is a lossy optimization so I think that many/most will want to hang on to their original images for comparison. Let me see if we can work an option for overwrites into the new command line executable that is in development. We should be able to support this.

chrissimpkins avatar Apr 08 '18 13:04 chrissimpkins

👍 Excellent results with this tool : it can further optimize image that PNGenie had already compressed ! But i must add my +1 to this request : I use PNGenie to do exactly that : drag the PNG file and optimize them in situ. So that i can routinely download user's uploaded images from several servers i'm an administrator, optimize them in one drag and drop, and reupload them easily. If i want to compare the images when i'm not sure, i can make a duplicate of my directory before compression.. So let's make it an option in a Preferences screen ! :)

zipang avatar Apr 19 '18 10:04 zipang

Thanks for your feedback! Will look into it.

chrissimpkins avatar Apr 19 '18 12:04 chrissimpkins

I have a new command line executable that is in development in #19. It supports parallel image processing on any *nix system (this will get to Linux and *nix on Win support) and the scripting will be the basis of the next version of the GUI tool. It will be simple to add this request to the command line executable. How important is it to you that this is implemented in the GUI tool as well? I am not a macOS objC/Swift developer and the GUI approach that I am using would likely involve a workaround like an environment variable or dotfile in the $HOME directory to support optional overwrites from the GUI tooling.

chrissimpkins avatar May 01 '18 16:05 chrissimpkins

Well the GUI is great to have as it makes the app easier to use. But if it's too complex then I'll switch from using GUI to command line which is slower at the end than just grabbing and dragging a bunch of files to a window

ajimix avatar May 01 '18 21:05 ajimix

We should be able to make it happen in the GUI. Will let you know when it is available. Thanks for the feedback.

chrissimpkins avatar May 01 '18 21:05 chrissimpkins

Hi @chrissimpkins,

any progress on that?

BTW great tool. Love it! ❤️❤️❤️❤️❤️

marcinkrzeminski avatar Jan 11 '20 14:01 marcinkrzeminski

Thanks for the note Marcin! I am working on native macOS application chops as we speak. I am considering a rebuild of the desktop application from scratch. I will definitely look into it for both GUI and command line as soon as I circle back to this project. And it is always open for PR's! If anyone else out there wants to tackle it as an optional feature that users call explicitly, I would be happy to implement it.

chrissimpkins avatar Jan 11 '20 15:01 chrissimpkins

Thanks for letting me know. I'd be happy to help, but sadly, I'm not familiar with Python 😔

marcinkrzeminski avatar Jan 11 '20 15:01 marcinkrzeminski

No worries! I'll do my best to get back to this soon. Time...

chrissimpkins avatar Jan 11 '20 16:01 chrissimpkins

If you want to modify the default behavior yourself, I believe that you should be able to do it by modifying this block beginning at line 400:

https://github.com/chrissimpkins/Crunch/blob/dee3cba5f1bfcf73263333c078373c298c3bba31/src/crunch.py#L400-L410

to something like the following:

    # report percent original file size / post file path / size (bytes) to log file (macOS GUI + right-click service)
    if is_gui(sys.argv):
        log_info(
            "[ "
            + percent_string
            + " ] "
            + img.post_filepath
            + " ("
            + str(img.post_size)
            + " bytes)"
        )
    
    try:
        os.remove(img.pre_filepath)
        shutil.move(img.post_filepath, img.pre_filepath)
        stdstream_lock.acquire()
        print("Moved {} to {}".format(img.post_filepath, img.pre_filepath))
        stdstream_lock.release()
    except Exception as e:
        stdstream_lock.acquire()
        sys.stderr.write("Error: Unable to overwrite pre image file {}.  Error message: {}".format(img.pre_filepath, e))
        stdstream_lock.release()

You can then rename the crunch.py script and place it in a directory on your system PATH (e.g., /usr/local/bin) with a file name crunch instead of crunch.py. Then call it on the command line with crunch [arguments - see README page]. This change will always overwrite the in path file.

Please (please, please) try this on copies of files before you attempt on anything that you can't afford to lose. This is 100% untested but I think that it should work™ and is intended as a way to help anyone out there who is interested get started on a derivative that addresses this issue until I can do it upstream.

chrissimpkins avatar Jan 11 '20 16:01 chrissimpkins

@chrissimpkins thanks for the pointers. Following your instructions, I did some changes and made a PR https://github.com/chrissimpkins/Crunch/pull/76

marcinkrzeminski avatar Jan 11 '20 17:01 marcinkrzeminski

The changes from Marcin are now in the dev branch and we will plan to include these in the next release.

chrissimpkins avatar Jan 20 '20 17:01 chrissimpkins

Just want to let you know, that this is the primary way I'd use it. I'm going to install the dev branch right now.

neochief avatar May 19 '20 18:05 neochief

Just looking at the comments on @marcinkrzeminski's PR (#76), perhaps instead of a checkbox for overwriting, some kind of contextual popup could be used? E.g. dragging files to GUI, delay for a few seconds, background of GUI changes, and when you release the mouse button, you receive an 'Are you sure?' prompt. Please excuse the rough mock-up, but something like:

crunch-mockup-crunch (Edit: Replaced the image with the crunched version, because, why not.)

I'm not sure if that's any easier than modifying the GUI, though.

If the information helps: I'm going to try to use this in an image processing workflow with the -f flag (I can regenerate from the original image when I need), so GUI option less critical (but nice to have).

Anyway, just an idea!

mattlongman avatar Mar 28 '21 22:03 mattlongman

E.g. dragging files to GUI, delay for a few seconds, background of GUI changes, and when you release the mouse button, you receive an 'Are you sure?' prompt.

Thanks for the suggestion! I really like it but it isn't possible with the current approach. The GUI is built around our scripts with an automated tool and the addition of dialogs/toasts/OK + Cancel buttons and the like are not possible.

I've been giving some thought to moving this over to PyQt5 so that we have the ability to support cross-platform GUI, custom settings (including an option to always write files in place), and user-defined zopflipng/pngquant paths. Let me see when time opens up to look into this. If we keep it relatively simple, it may not be a terribly large amount of work.

chrissimpkins avatar Mar 29 '21 20:03 chrissimpkins

Understood - I thought that might be the case based on previous notes.

For automated workflows, -f will be great, but I was also wondering whether the python core can be published to pypi? (Edit: not to drag things off topic, issue #87 created.)

mattlongman avatar Mar 30 '21 14:03 mattlongman