A simple negative script based on python+opencv

Time:2022-6-30

Export & the use of this script

Recently, I found that the dark color theme can relieve eye fatigue to a great extent when the computer uses the dark color theme. Therefore, I changed the dark color theme for the markdown editor, which is most commonly used at ordinary times
However, it is very easy to change the color for the text content, but the picture is more troublesome and inconvenient to change the color
Untitled
Such a big white picture is forced into the interface with white characters on a black background. It’s almost obsessive-compulsive disorder!

So I plan to write a small script to realize that the source image can be automatically converted to black system by dragging and dropping it onto a handler.

However, it seems to be a bit troublesome and the effect is not very good. For example, if the RGB value of a pixel is within the white range, it will be changed to (0,0,0)? Using this violence threshold method to process the image contour edges of low resolution images will be very bad.

Therefore, it is better to use the effect of “negative film”, just change the color without losing information.

realization

Since Python scripts do not support “dragging files to scripts as execution parameters” in windows, but bat scripts do, you can drag the images to be processed to the bat script to read the image path, then the bat script calls the python script, and takes the image path as the execution parameter of the python script to achieve the “indirect drag” effect

Drag the file onto me to process as negative bat

:: write this instruction, otherwise each of the following instructions will be printed on the public screen when executing bat
@echo off
::% * represents the collection of all files dragged to the bat script. It is better to save this value in a variable, or use% * when calling the function. It seems that the value is different?
set dragged_items=%*
::%0 represents the address of the file itself, such as c:\users\csd\documents\enote_ database\__ batch_ processor__\ Picture what what what bat
set LocalPath=%0
:: This is to call the following execute_ python_ Script function, the parameter is the localpath just set
call :execute_python_script %LocalPath%
:: displays a line "press any button to continue..."
:: pause


:execute_python_script
:: write the name of the python script in the peer directory after the following equal sign
set python_script=invertPICS.py
:: This is to delete the self name (picture.Bat) in the file address of the batch script, leaving a path. Then, the path of the python script is spliced with its path to become the path of the python script
:: (of course, the premise is that you must put the python script and bat script in the same directory = \u=)
set file_path=%~dp1%python_script%
:: finally, the python script can be executed. The parameters are the files just dragged onto the bat
python %file_path% %dragged_items%

invertPICS.py

import sys
import cv2 as cv

pic_extensions = [".png", ".jpg"]

file_paths = sys.argv[1:]
print(file_paths)
for Pic in file_paths:
    for pic_extension in pic_extensions:
        if str(Pic).endswith(pic_extension):
            Pic_data = cv.imread(Pic, 1)
            # cv.imshow("preview", Pic_data)
            # cv.waitKey(3000)
            try:
                test = Pic_data.shape[0]
                Pic_data = 255 - Pic_data
                cv.imwrite(Pic, Pic_data)
            Except attributeerror: \opencv imread does not support Chinese paths
                print(">>Error! you'd gotta use a pure English filename!")
            break