Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic watermarking and uploading of pictures by connecting Python with PicGo

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Python docking PicGo how to automatically add watermarks and upload pictures". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

1. The world has been carrying hard for a long time.

two。 Current drawing bed management tools

3. The assumption of the plan

4. Complete code parsing

4.1 define hotkeys and monitor keyboards

Step 1: define your hotkeys first

Step 2: monitor all keyboard movements

4.2 read images from memory

4.3 add a watermark to generate a new image

4.4 put the new image back on the clipboard

4.5 Analog trigger PicGo

4.6Notification Mac notification desk

5. Other setup work

5.1 set program permissions

5.2 set Boot self

6. Running effect

1. The world has been carrying hard for a long time.

For technical self-media like me, which often needs to write some articles, I often launch the original article on the official account in the morning, and at noon someone synchronizes to external platforms such as Zhihu and Jinri Toutiao, and gets the first release of the article on these platforms.

In order to solve the problem of the initial launch, I paid to use the OpenWrite platform, which costs 20 yuan a month and can be distributed to the major platforms at the click of a button, which is very easy.

The starting line-up is important, but sometimes it's not that important.

Because there are special training institutions (don't name them here, avoid reverse marketing) is to use your article to pile up practical information for your account to attract attention, they don't care about originality or originality, as long as the article can be published.

This kind of person can live very well in Zhihu, which has no original detection mechanism.

Before that, an unscrupulous training institution produced more than 10 numbers on Zhihu to carry the original articles of me and some friends in bulk. At that time, I could report a lot every day.

Gradually, I am tired. I have written hundreds of articles by myself. If I check one by one, I can hardly do anything on this day. The cost of violation of rights is too high.

Considering that there are so many pictures in my articles, in order to make these people visit my articles for nothing, they can also bring me some income (of course, it is impossible for people to pay, but at least it is very nice to advertise for my official account).

So I thought, ah, can I write my own tool and add my own watermark to each of my pictures to see if they can steal or not? no, no, no.

two。 Current drawing bed management tools

Before I start talking about how to use Python to implement my requirements, I need to introduce my graph bed management tool.

When I write an article, I mainly use three tools:

Typora: editor of Markdown copy Snipaste: a very useful screenshot tool PicGo: a very humane tool for picture bed management

Among them, today's protagonist is PicGo.

It provides good support for the current mainstream picture bed platforms.

When I use the Snipaste screenshot, and then hold down the shortcut key (⌘ ⇧ P), you can immediately upload your picture bed to the designated picture bed, and copy the uploaded link to the clipboard in markdown image format, you can paste it directly.

3. The assumption of the plan

Because I have been using this whole tool for three years, I am very familiar with all kinds of operations, and I have become extremely dependent on them, so now I want to achieve the function of automatic watermarking. It must be based on this set of tools.

Neither Snipaste nor PicGo itself supports custom watermarks, nor does they provide a development portal for third-party plug-ins.

Snipaste and PicGo can work together because there is a bridge of clipboard, so if you want to achieve your needs, you can only find a breakthrough from the clipboard.

I won't say any more nonsense. I'll just talk about my plan:

After Snipaste puts the image on the clipboard, I type the custom hotkey to trigger the Python script to read the image from the clipboard, then use PIL to watermark the image, put it back into the clipboard, and then use the Python script to automatically trigger PicGo. After the shortcut key PicGo is activated, the watermarked image can be uploaded to the picture bed.

In order to give you an intuitive understanding of this scheme, I specially drew a flow chart, in which the dotted line is the function that I have to implement by myself.

4. Code complete parsing 4.1 define hotkeys and monitor keyboards

There is a pynput library in Python that allows you to listen to the keyboard of the system.

In its official documentation, a solution for customizing hotkey combinations was quickly found.

From pynput import keyboarddef on_activate (): print ('Global hotkey activated') def for_canonical (f): return lambda k: F (l.canonical (k)) hotkey = keyboard.HotKey (keyboard.HotKey.parse ('+ + h'), on_activate) with keyboard.Listener (on_press=for_canonical (hotkey.press), on_release=for_canonical (hotkey.release)) as l: l.join ()

But unfortunately, this function currently has BUG, and it doesn't work when I test it on Mac, and some people in github's issue also responded to the problem in August 2020, but it hasn't been solved yet.

Although its own key combination monitoring mode can not be used, but the ordinary monitoring mode can still be used, as long as it is based on this, it is not difficult for me to build my own wheel to achieve the function of combining hotkeys.

Step 1: define your hotkey upload_pic_set = {keyboard.Key.ctrl.value.vk, keyboard.Key.cmd.value.vk, keyboard.Key.alt.value.vk, keyboard.KeyCode (35) .vk} step 2: monitor all keyboard actions with keyboard.Listener (on_press=on_press, on_release=on_release) as listener: listener.join ()

As long as a key is in the state of press, store the key in the list

Key_list = [] def on_press (key): if isinstance (key, keyboard.KeyCode): key_list.append (key.vk) elif isinstance (key Keyboard.Key): key_list.append (key.value.vk) if set (key_list) = = upload_pic_set: image = get_image_from_clipboard () new_image = make_watermark (image) put_image_to_clip (new_image) upload_image_via_picgo () notify_to_mac ("successfully add watermark and upload to picture bed")

But once a key is released, clear the list

Def on_release (key): key_list.clear ()

Every time you press the key, you will check whether key_list is equal to the defined shortcut key, and if it happens to be equal, you can start the processing logic of the image.

If set (key_list) = = upload_pic_set: pass4.2 reads images from memory

PIL has an ImageGrab module, in which there is a grabclipboard function that reads images from the clipboard, but reads the rgb format, because we have to use rgba format when we add watermarks, so use convert to turn around.

From PIL import ImageGrab img_rgb = ImageGrab.grabclipboard () image = img_rgb.convert ("RGBA") 4.3 add a watermark to generate a new image

The following is the code to add a watermark. In fact, it may be important to note that if your text contains Chinese, you must choose a Chinese font, otherwise square characters will appear.

Def make_watermark (image): txt = Image.new ('RGBA', image.size, (0,0,0,0) fnt = ImageFont.truetype ("/ System/Library/Fonts/STHeiti Medium.ttc", 20) draw = ImageDraw.Draw (txt) draw.text (txt.size [0]-300) / / 2, txt.size [1]-40), "Wechat official account: Python programming time", font=fnt, fill= (240,49,48) Draw.text (txt.size [0]-300) / / 2, txt.size [1]-70), "do not reprint without authorization", font=fnt, fill= (240,49,48,255) out = Image.alpha_composite (image, txt) return out4.4 put the new image back into the clipboard

The built-in io module supports reading and writing bytes in memory. As long as the image object of PIL is saved in the BytesIO object when save, and then load the data from the BytesIO object through the pasteboard module, you can put the image into the clipboard.

Def put_image_to_clip (image): img_byte_arr = io.BytesIO () pb = pasteboard.Pasteboard () image.save (img_byte_arr, format='PNG') img_byte_arr = img_byte_arr.getvalue () pb.set_contents (img_byte_arr, pasteboard.PNG)

Images loaded by pasteboard only support PNG format, so be sure to specify PNG when saving.

In addition, pasteboard also supports data in more formats, such as PDF, audio data, HTML, color data, etc.

More formats are available at: https://developer.apple.com/documentation/appkit/nspasteboardtypestring

4.5 Analog trigger PicGo

Normally, we use shortcut keys to trigger PicGo to upload images from the clipboard, so if we want to activate PicGo in the program, we only need to simulate the keyboard action in the Python script.

The specific code is as follows:

From pykeyboard import PyKeyboarddef upload_image_via_picgo (): K = PyKeyboard () k.press_keys (['Command',' shift','p']) 4.6 notify the Mac notification desk

The above whole process is run silently by the script in the background. If there is no notification, it is difficult for users to know whether our images have been handled properly and uploaded successfully, so it is recommended to add a notification function.

Import osdef notify_to_mac (message): os.system ("osascript-e'display notification\" {}\ "\" .format (message))

But in fact, after PicGo uploads the picture, it will notify itself, so this notice is not necessary, depending on the personal needs.

5. Other settings work 5.1 set program permissions

If you find that some keys cannot be captured when using the above script, it must be that the system does not give permission and requires you to open it manually.

5.2 set Boot self

Add a boot entry here, and this init.sh is a Shell script.

The content of this script is as follows, pay attention to the last one & it must not be omitted.

6. Running effect

After all the code has been parsed, do you really want to see what effect this program can achieve after running?

I have recorded a GIF dynamic map. You can have a look. It's really convenient.

This is the end of the content of "Python docking PicGo how to automatically add watermarks and upload pictures". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report