fromutils.resizeimportresizefromutils.add_watermarkimportadd_watermarkfromutils.get_promptimportget_promptimportargparseimportglobimportosdefresize_and_addwatermark(inputFile,outputFile):image=resize(inputFile)result=add_watermark(image)result.save(outputFile,'webp',quality=80)defimages_handler(inputFolder,outputFolder,image_name='girl'):# get png/jpg files and index themimages=glob.glob(inputFolder+"/*.png")images.extend(glob.glob(inputFolder+"/*.jpg"))images.extend(glob.glob(inputFolder+"/*.jpeg"))name_index=0has_prompt=Falseforimageinimages:ifos.path.isfile(image):has_prompt=generate_prompt(image,has_prompt,outputFolder)print('doing',image)file_name=image_name+str(name_index)+".webp"name_index+=1output_file=os.path.join(outputFolder,file_name)resize_and_addwatermark(image,output_file)print('done',image,'to',output_file)print('done all')defgenerate_prompt(image,has_prompt,outputFolder):ifnothas_prompt:prompt=get_prompt(image)ifprompt:has_prompt=Trueprompt_file=os.path.join(outputFolder,'prompt.txt')withopen(prompt_file,'w',encoding='utf8')asf:f.write(prompt)print('done',prompt_file)returnhas_promptdefget_args():parser=argparse.ArgumentParser(description='Resize image and add watermark')parser.add_argument('-i','--input',help='input folder',required=True)parser.add_argument('-n','--name',help='name of images',default='girl_')args=parser.parse_args()returnargsif__name__=='__main__':args=get_args()inputFolder=args.inputoutputFolder=inputFolder+"\output"name=args.nameifnotos.path.exists(outputFolder):os.makedirs(outputFolder)images_handler(inputFolder,outputFolder,name)
fromwordpress_xmlrpcimportClient,WordPressPostfromwordpress_xmlrpc.methods.postsimportNewPostfromwordpress_xmlrpc.methodsimportmediafromwordpress_xmlrpc.compatimportxmlrpc_clientimportargparseimportos# replace with your own wordpress site url and credentialswp=Client('http://your_wp_site.com/xmlrpc.php','admin','password')defpython_to_wp(folder,title,tags,category,slug=None):print('Creating post: '+title)post=WordPressPost()post.title=titleifslug:post.slug=slugpost.content=''prompt_txt_content=get_prompt_txt_content(folder)post.terms_names={'post_tag':tags.split(','),'category':category.split(',')}post.excerpt=prompt_txt_contentpost.post_status='publish'images=get_folder_images(folder)has_cover=Falseforimageinimages:# upload imageimage_with_path=os.path.join(folder,image)image_uploaded=upload_image(image_with_path)image_url=image_uploaded['url']image_id=image_uploaded['id']image_name=image_uploaded['file']# add image to post contentpost.content+=f'''<figure class="wp-block-image size-full"><img decoding="async" loading="lazy" src="{image_url}" alt="" ></figure>'''ifnothas_cover:post.thumbnail=image_idifimage_name=='cover.webp':has_cover=Truewp.call(NewPost(post))print('Post created: '+title)defget_folder_images(folder):images=[fforfinos.listdir(folder)ifos.path.isfile(os.path.join(folder,f))]images=[fforfinimagesiff.endswith('.webp')]returnimagesdefupload_image(image):# get image nameimage_name=os.path.basename(image)# get image datadata={'name':image_name,'type':'image/webp',}# open imagewithopen(image,'rb')asimg:data['bits']=xmlrpc_client.Binary(img.read())# upload imageresponse=wp.call(media.UploadFile(data))returnresponsedefget_prompt_txt_content(folder):prompt_file=os.path.join(folder,'prompt.txt')ifos.path.isfile(prompt_file):withopen(prompt_file,'r',encoding='utf8')asf:content=f.read()returncontentelse:return''defget_args():parser=argparse.ArgumentParser()parser.add_argument('-f','--folder',help='Choose Folder',required=True)parser.add_argument('-t','--title',help='Post Title',required=True)parser.add_argument('-s','--slug',help='Post Slug')parser.add_argument('-tags','--tags',help='Post Tags',required=True)parser.add_argument('-cat','--category',default='妹子图',help='Post Category')args=parser.parse_args()returnargsdefmain():args=get_args()python_to_wp(**vars(args))if__name__=='__main__':main()