qr扫描枪不再适用于GIMP Pythonfu自动化

2024-09-27 21:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在和动物学家合作,他们有成千上万的昆虫照片。每张照片都有一些标签和一个二维码,可以唯一地识别样本。在将这些照片上传到数据库之前,必须根据图像中包含的二维码对每个文件进行处理和重命名。这是通过使用霍尼韦尔的手持式QR扫描枪完成的。你知道吗

我为GIMP编写了一个Python插件,允许用户选择要处理的图像文件夹。该插件执行它可以自动完成的所有处理步骤,然后一次显示一个图像。对于每个图像,插件都会生成一个对话框,提示用户输入新的文件名。用户可以手工编辑照片,然后点击文本输入框,用扫描枪扫描二维码。然后插件用新的文件名保存图像并移动到下一个文件。你知道吗

问题是,手持式扫描枪不再适用于这项服务,我也说不出有什么变化。用户仍然可以通过手动输入文本框来重命名每个图像,甚至可以将光标放在其他地方,扫描二维码,然后将扫描仪生成的文本复制粘贴到文本框中。但是只要点击文本输入框并扫描二维码,就会返回一个空字符串作为新的文件名,当图像不能像那样保存时,就会产生错误。你知道吗

我已经在下面列出了我的所有代码,因为动物学家声称这个问题是从更新之间开始的。我的代码有明显的缺陷吗?有没有人以这种方式使用QR扫描仪,并且需要克服类似的问题?有人能想出解决这个问题的办法吗?感谢您的帮助;StackExchange对这个项目非常有价值。你知道吗

#!/usr/bin/env python
from gimpfu import *    # For interacting with the GIMP
import os       # For pulling files from folders

import gtk      # The gtk code is adapted from Ben Duffield's Ardonis Wordpress. Thanks, Ben!
            # It's used for generating dialog boxes to prompt users for new filenames.
            # It comes with a checkbox marked "Flag bad image?" If checked, "bad_pic_" appended to filename start 

##### 

def responseToDialog(entry, dialog, response):
    dialog.response(response)
def getText(file):
    #base this on a message dialog
    dialog = gtk.MessageDialog(
        None,
        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
        gtk.MESSAGE_QUESTION,
        gtk.BUTTONS_OK,None)
    dialog.set_markup('Edit the photo now. Then enter new file name. Use the scanner-gun on the QR if possible. Leave the space blank to use old file name.')
    checkBlurry = gtk.CheckButton()     # Add checkmark box
    entry = gtk.Entry()         # Add text-entry box                
    entry.connect("activate", responseToDialog, dialog, gtk.RESPONSE_OK)
    vbox = gtk.VBox(TRUE)                       # Vertical box
    hbox1 = gtk.HBox(TRUE)                      # First horizontal box has text-entry
    hbox1.pack_start(gtk.Label("New Filename:"), True, True, 0)
    hbox1.pack_end(entry)
    vbox.pack_start(hbox1)                      # Second horizontal box has checkbox
    hbox2 = gtk.HBox(TRUE)
    hbox2.pack_start(gtk.Label("Flag Bad Image:"), True, True, 0)
    hbox2.pack_end(checkBlurry, True, True, 0)
    vbox.pack_end(hbox2)
    vbox.pack_end(gtk.Label(), True, True, 0)               
    dialog.vbox.pack_end(vbox, True, True, 0)               # Done packing
    dialog.show_all()

    dialog.run()
    text = entry.get_text() + ".jpg"

    if(text == ".jpg"):         # If the dialog is returned empty, use old file name
        text = file
    if(checkBlurry.get_active()):   # If flagged, start filename with "bad_pic_"
        text = "bad_pic_" + text
    dialog.destroy()            # Destroy and return filename
    return text

#####

def BugFlipper(OldDir,NewDir,renameMe,rotateMe,correctMe,deleteOld,imageQuality):
    for file in os.listdir(OldDir):                     # Checks every file in first folder
        if ((file.endswith(".jpg") or file.endswith(".JPG")) and file.startswith("DSC_")):  # Selects JPEGS beginning DSC_

PrepareImage(file,OldDir,NewDir,renameMe,rotateMe,correctMe,imageQuality)   
                                    # Method for image processing
        if (deleteOld):
            os.remove(OldDir+"/"+file)              # Delete old photos, if selected
    return()

#####   

def PrepareImage(file,OldDir,NewDir,renameMe,rotateMe,correctMe,imageQuality):          # Processes photos
    image = pdb.gimp_file_load(OldDir+"/"+file,OldDir+"/"+file)
    drawable = pdb.gimp_image_get_active_layer(image)           
    if(rotateMe):
        pdb.gimp_drawable_transform_rotate_simple(drawable,ROTATE_180, 1,0,0,1)     # Rotate 180 degrees, if selected
    if(correctMe):
        drawable = pdb.python_fu_WhiteBalanceStretch(image,drawable)            # White/Color balance by Diego Nassetti, if selected
    newFileName = file
    if(renameMe):
        display = pdb.gimp_display_new(image)                       # Displays the photo
        newFileName = getText(file)                             # Asks user for new filename
    pdb.file_jpeg_save(image, drawable,NewDir+"/"+newFileName,NewDir+"/"+newFileName,
    imageQuality, 0,0,0,"newFileName",0,1,0,0)                  # Save in second folder with new name and quality
    if(renameMe):   
        pdb.gimp_display_delete(display)                        # Remove display

#####

register(
    "Bug-Flipper",                      # Name
    "Made by Ted Tinker for Cheadle Center's zoologists",   # Blurb
    "Made by Ted Tinker for Cheadle Center's zoologists for human-assisted-image-processing of standardized insect photos.",    # Help
    "Ted Tinker",                       # Author
    "Ted Tinker, freeware",                 # Copywrite
    "2017",                         # Date
    "Bug-Flipper",                      # Display Name
    "",                             # No picture required
[
(PF_DIRNAME, "OldDir", "Folder with photos:","/Users/herbarium/Desktop/insect-images-for-processing"),  # Method Parameters
(PF_DIRNAME, "NewDir", "Folder to save photos:", "/Users/katjaseltmann/Public/insect-images-processing/flipped"),
(PF_BOOL, "renameMe", "Rename/Edit photos?", 1),
(PF_BOOL, "rotateMe", "Rotate photos 180 degrees?", 1),
(PF_BOOL, "correctMe", "Run white-balance/color-correction?", 1),
(PF_BOOL, "deleteOld", "Delete old photos after processing?", 1),
(PF_SLIDER, "imageQuality", "Saved Image Quality:", .3,(.01,1,.01))
],
[],                             # Method Return
BugFlipper, menu="<Toolbox>/Filters")

main()

Tags: thetext图像imagetruegtkforif

热门问题