Creating a Pin for Pinterest using database of products
Post ReplyCreating a Pin for Pinterest using database of productsPosted: Tuesday, October 15, 2019 [02:50:20] - 1
Creating a business campaign on Pinterest requires a lot of work choosing proper keywords, designing Pins and much much more. When Pin design was finalized and ready, job of creating pins for each category of products can be automated. Perl program below creates a Pin based on product category and pre-set colors. ## FILE MADE FOR EDUCATION PURPOSES ONLY! USE AT YOUR OWN RISK! ## MAKING PIN WITH BACKGROUND AND GRADIENTS my %delete=(); my %datain=(); ## PATH TO FILE IF PROGRAM IS CALLED WITH FULL PATH ON POSIX OS $0 =~ m/(.+)[\/\\](.+)$/; my $fpath = $1; my $installdir = $fpath.'/installed'; my $tmp = $fpath.'/temp'; ## COLLECT DATA if(-f "$fpath/data.file.txt") {my $dta = `cat $fpath/data.file.txt`; foreach my $l (@datalines) { my($key,$value) = split(/\t/,$l); if($value) {$datain{$key}=$value;} } ## FOREACH LINE END } ## END IF DATA FILE PRESENT else {print "No data found - exiting\n"; my %bkgrounds = ( ## ARRAYS OF COLORS FOR DIFFERENT PRODUCTS, PLEASE CHOOSE YOUR OWN COLORS 'tables'=>['ffbc4f','8ffa54','56c4ff'], 'dishes'=>['ffe96a','6cfce1','ee9eff'], 'shelves'=>['ff578a','ffd058','bffa6d'], 'tiles'=>['1639ff','ff56f5','ff2b10'] ); ## CHOOSE RANDOM BACKGROUND COLOR my @bgcolors = @{$bkgrounds{$datain{dir1}}}; if($@) {print "Dir: $datain{dir1}\nColr Ar: @bgcolors\nError color: $@\n"; my $mainbkg = shift(@bgcolors); ## SELECT BACKGROUND IMAGE FROM A FOLDER WITH IMAGES my @imgs = glob("$installdir/img.all/$datain{dir1}/*"); if($@) {print "Error dir: $@\n"; my $img = shift(@imgs); ## CREATE INITIAL IMAGE TO CHOOSE PORTION OF IT - BLUR IT A BIT `convert $img -resize x1102 -blur 0x3 $tmp/resized.jpg`; ## READ IMAGE SIZE my $image = `anytopnm $tmp/resized.jpg`; ## GET A RANDOM PORTION OF AN IMAGE my $ow=$w; if($@) {print "\nERROR:\nW: $ow\tH: $h\nError pixel: $@\n"; my $stpos = shift(@imsz); ## CREATE AN RANDOME BACKGROUND IMAGE WIDTH 735 HEIGHT 1102 `convert $tmp/resized.jpg -crop 735x1102+$stpos+0 $tmp/composite0.jpg`; ## ADD GRADIENTS TO THE IMAGE `convert $tmp/composite0.jpg -alpha set -channel A -sparse-color Barycentric '%w,%[fx:h-800] opaque %w,%[fx:h+30] transparent' -background '#000000' -flatten $tmp/gradient.jpg`; `convert $tmp/gradient.jpg -alpha set -channel A -sparse-color Barycentric '%w,%[fx:1050] opaque %w,%[fx:-500] transparent' -background '#$mainbkg' -flatten $tmp/gradient2.jpg`; $delete{"$tmp/gradient.jpg"}=1; ## DRAW A TRANSPARENT RECTANGLE `convert $tmp/gradient2.jpg -strokewidth 0 -fill "rgba( 0, 0, 0, 0.3 )" -draw "rectangle 95,95 640,1007" $tmp/gradient3.jpg`; ## WRITE A LINE MAKING A FONT BOLDER my $savewords=$datain{line1}; `convert -background none -fill "#ffffff" -pointsize 100 -kerning 6 -font "StencilStd" -size 690x120 -gravity Center caption:"$savewords" -trim $tmp/pin.save.png`; `convert -background none -fill "#ffffff" -pointsize 100 -kerning 6 -font "StencilStd" -size 690x120 -gravity Center caption:"$savewords" -trim $tmp/pin.save.w.png`; my $nextpr="$tmp/pin.save.png"; ## READ NEXT FILE $delete{"$tmp/pin.save.png"}=1; $image = `anytopnm $tmp/pin.save.png`; if($w && $h) {my $xpos = 735 / 2; `composite -geometry +$xposz+975 $tmp/pin.save.w.png $tmp/gradient3.jpg $tmp/pin.w1.year.jpg`; `composite -geometry +$xpos+975 $tmp/pin.save.png $tmp/pin.w1.year.jpg $tmp/pin.w.year.jpg`; $nextpr="$tmp/pin.w.year.jpg"; ## CHANGING NEXT IMAGE TO READ FROM } ## WRITE SOME HEADER my $piontsize=100; if(length($datain{header}) > 7) {$piontsize=90;} if(length($datain{header}) > 9) {$piontsize=75;} `convert -background none -fill "#ffffff" -pointsize $piontsize -kerning 6 -font "StencilStd" -size 690x120 -gravity Center caption:"$datain{header}" -trim $tmp/pin.brnd.png`; $delete{"$tmp/pin.brnd.png"}=1; $image = `anytopnm $tmp/pin.brnd.png`; if($w && $h) {my $xpos = 735 / 2; `composite -geometry +$xpos+60 $tmp/pin.brnd.png $tmp/pin.w.year.jpg $tmp/pin.w.brand.jpg`; $nextpr="$tmp/pin.w.brand.jpg"; } ## WRITE A VERTICAL HEADER ## SELECT APPROPRIATE FONT SIZE AND STARTING POSITION FOR THE SECOND HEADER DEPENDING ON A LENGTH OF THE HEADER my $bpnts=90; if(length($datain{hd2}) > 6) {$stypos=880;} if(length($datain{hd2}) > 12) {$stypos=980;} if(length($datain{hd2}) > 15) {$bpnts=80;} `convert -background none -fill "#ffffff" -pointsize $bpnts -kerning 3 -font "OpenSansBI" -size 900x120 -gravity Center caption:'$datain{hd2}' -trim -rotate "-90" $tmp/pin.brand.png`; `convert -background none -fill "#000000" -pointsize $bpnts -kerning 3 -font "OpenSansBI" -size 900x120 -gravity Center caption:'$datain{hd2}' -trim -rotate "-90" $tmp/pin.brand.k.png`; $delete{"$tmp/pin.brand.k.png"}=1; $delete{"$tmp/pin.brand.png"}=1; $image = `anytopnm $tmp/pin.brand.k.png`; if($w && $h) {my $ypos = $stypos - $h; `composite -geometry +52+$ypos $tmp/pin.brand.k.png $nextpr $tmp/pin.w.brand1.jpg`; `composite -geometry +45+$ypos $tmp/pin.brand.png $tmp/pin.w.brand1.jpg $tmp/pin.w.brand.jpg`; $nextpr="$tmp/pin.w.brand.jpg"; } ## PRINT SOME TEXT my $prtext = $datain{pgtext}; ## CLEAN TEXT (OPTIONAL) $prtext =~ s/^\s+//; `convert -background none -fill "#ffffff" -pointsize 54 -kerning 2 -font "OpenSans" -size 480x -gravity NorthWest caption:'$prtext' -trim $tmp/pin.text.png`; $delete{"$tmp/pin.text.png"}=1; $image = `anytopnm $tmp/pin.text.png`; my $vertpos=220; if($w && $h) {my $xpos=''; if($w < 380) {$xpos=160;} else {$xpos = 735 / 2; `composite -geometry +$xpos+220 $tmp/pin.text.png $nextpr $tmp/pin.i.jpg`; $nextpr="$tmp/pin.i.jpg"; } ## PUT SOME PRODUCT IMAGE AS WELL AND PROPERLY RESIZE AND PLACE IT IN A CENTER AND BETWEEN TEXT if(-f "$installdir/$datain{img}") { $image = `anytopnm $installdir/$datain{img}`; my $readimg="$installdir/$datain{img}"; if($w && $h) {my $ypos=0; if($w > $h) {`convert -size $ssz xc:white $tmp/img.tmp.png`; if($h < 180) {$ypos = (180-$h)/2;} else { `convert $readimg -resize x180 $tmp/prevrt.jpg`; $image = `anytopnm $tmp/prevrt.jpg`; `convert -size $tssz xc:white $tmp/img.tmp.png`; } `composite -geometry +0+$ypos $readimg $tmp/img.tmp.png $tmp/orsmimg.jpg`; } ## END WIDER THAN HEIGHT else { `convert $readimg -resize $ssz $tmp/orsmimg.jpg`; } ## END IF HIGHER THEN WIDTH $image = `anytopnm $tmp/orsmimg.jpg`; my $spaceav = 920 - $vertpos; if($spaceav > 180) { my $maxwid=450; print "Space av: $spaceav\nCoeff: $koeff ($spaceav / $h)\tTry width: $tryw\n"; if($tryw <= $maxwid) {`convert $tmp/orsmimg.jpg -resize x$spaceav $tmp/orsmimg.jpg`; else { my $tryht = (450 / $w) * $h; `convert $tmp/orsmimg.jpg -resize 450x $tmp/orsmimg.jpg`; print "$tryw > $maxwid\n";} $image = `anytopnm $tmp/orsmimg.jpg`; } ## END MORE THAN 180 PIXELS HIGH AVAILABLE my $xprnt = (735 - $w) / 2; my $yprnt = $vertpos; `composite -geometry +$xprnt+$yprnt $tmp/orsmimg.jpg $nextpr $tmp/final.pin.png`; } ## END W AND H PRESENT } ## END IMAGE PRESENT ## NOW DELETE ALL HELPER IMAGES THAT WERE CREATED foreach my $fdel (keys(%delete)) {unlink $fdel;} 1; ## OPTIONAL IF YOU CALL THIS PROGRAM FROM ANOTHER PROGRAM BE MEANS OF require ## SUBS ######################################################################################################### sub randadv {my $array = shift; for ($i = @$array; --$i; ) {my $j = int rand ($i+1); } ## RAND ADV SUB END sub pnmsize { my($pnm) = @_; my ($width,$height); ($width,$height)=$pnm=~m#^P\d+\s+(\d+)\s+(\d+)\s+\d+\s#ois; return ($width,$height) if ($width && $height); } ## END SUB PNM SIZE ## To post created pins may take a long time. Automated posting can be achieved by submitting pins to online submission services. In-house automatic Pins submission can be done as well. We can help setting it up, please contact us of you need any help. |
Data file and image examplePosted: Tuesday, October 15, 2019 [03:25:17] - 2
This is example of data file used to produce an image: line1 header hd2 pgtext img and this is the resulting Pin: Pin was actually created by the program code listed above |
Actual screen recordingPosted: Wednesday, November 13, 2019 [22:37:02] - 3
Here you can find a screen recording: www.codemacs.com/coding/a.. Actual posting to Pinterest |