// https://novita.ai/playground#remove-watermark import * as fs from 'fs' import * as path from 'path' import * as bluebird from 'bluebird' import { Promise as BPromise } from 'bluebird' import * as sharp from 'sharp' import { OSR_CACHE } from '@plastichub/osr-commons' import { sync as exists } from "@plastichub/fs/exists" import { async as move } from "@plastichub/fs/move" import { async as write } from "@plastichub/fs/write" import { sync as dir } from "@plastichub/fs/dir" import { createItem as toNode } from "@plastichub/fs/inspect" import { logger } from '../../../index' import { IOptions, IResizeOptions } from '../../../types' import { imageToBase64, base64ToBuffer } from './lib' import { get_cached } from '@plastichub/osr-cache/lib' import { NovitaSDK } from "novita-sdk" import { targets, targetsNext } from '../../' const removeWatermark = async (file, target, onNode: (data: any) => void = () => { }, options: IResizeOptions) => { const novitaClient = new NovitaSDK(options.key) const params = { image_file: await imageToBase64(file) //"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD", } try { const wMark = await novitaClient.removeWatermark(params) logger.info(`Watermark removed: ${file} to ${target}`) write(target,base64ToBuffer(wMark.image_file)) } catch (error) { logger.error(`Failed to remove watermark: ${error.msg}`) } } const _watermark = async (file, targets: string[], onNode: (data: any) => void = () => { }, options: IOptions) => { return BPromise.resolve(targets).map((target) => { options.verbose && logger.debug(`Removing Watermark ${file} to ${target}`) if (options.dry) { return bluebird.resolve() } return removeWatermark(file, target, onNode, options); }, { concurrency: 1 }) } export const watermark = async (options: IOptions) => { // reporting, stub let reports: any = [] const onNode = (data: any) => reports.push(data) if (options.srcInfo) { options.verbose && logger.info(`Convert ${options.srcInfo.FILES.length} files`) return await BPromise.resolve(options.srcInfo.FILES).map((f) => { const outputs = targets(f, options) options.verbose && logger.info(`Convert ${f} to `, outputs) return _watermark(f, outputs, onNode, options) }, { concurrency: 1 }) } else { options.debug && logger.error(`Invalid source info`) } }