AppleScript C Perl Shell Xcode Other

Creating a Pin for Pinterest using database of products

Post Reply
coding / perl     Views: 507Prev .. Next
Creating a Pin for Pinterest using database of productsPosted: Tuesday, October 15, 2019 [02:50:20] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 357
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.
View Code## THIS IS AN EXAMPLE PERL FILE, AND IT MAY NOT WORK ON YOUR SYSTEM WITHOUT ADJUSTMENTS
## FILE MADE FOR EDUCATION PURPOSES ONLY! USE AT YOUR OWN RISK!
## MAKING PIN WITH BACKGROUND AND GRADIENTS
my %delete=();## DELETE HASH TO DELETE TEMPORARY FILES AT THE END OF THE PROGRAM
my %datain=();## DECLARE DATA HASH

## 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`;my @datalines = split(/\n/,$dta);$dta='';
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";exit(0);} ## OPTIONAL LINE TO MAKE SURE DATA IS PRESENT
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}}};eval {randadv( \@bgcolors )};
if($@) {print "Dir: $datain{dir1}\nColr Ar: @bgcolors\nError color: $@\n";exit(0);}
my $mainbkg = shift(@bgcolors);@bgcolors=();
## SELECT BACKGROUND IMAGE FROM A FOLDER WITH IMAGES
my @imgs = glob("$installdir/img.all/$datain{dir1}/*");eval {randadv( \@imgs )};
if($@) {print "Error dir: $@\n";exit(0);}
my $img = shift(@imgs);@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`;my($w,$h) = &pnmsize($image);$image='';
## GET A RANDOM PORTION OF AN IMAGE
my $ow=$w;$w -= 735;my @imsz = (0..$w);eval{randadv( \@imsz )};
if($@) {print "\nERROR:\nW: $ow\tH: $h\nError pixel: $@\n";exit(0);}
my $stpos = shift(@imsz);@imsz=();

## CREATE AN RANDOME BACKGROUND IMAGE WIDTH 735 HEIGHT 1102
`convert $tmp/resized.jpg -crop 735x1102+$stpos+0 $tmp/composite0.jpg`;$delete{"$tmp/composite0.jpg"}=1;

## 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;$delete{"$tmp/gradient2.jpg"}=1;$delete{"$tmp/gradient3.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;$delete{"$tmp/pin.save.w.png"}=1;
$image = `anytopnm $tmp/pin.save.png`;my($w,$h) = &pnmsize($image);$image='';
if($w && $h) {my $xpos = 735 / 2;$w /= 2;$xpos -= $w;my $xposz=$xpos;
`composite -geometry +$xposz+975 $tmp/pin.save.w.png $tmp/gradient3.jpg $tmp/pin.w1.year.jpg`;$delete{"$tmp/pin.w1.year.jpg"}=1;
`composite -geometry +$xpos+975 $tmp/pin.save.png $tmp/pin.w1.year.jpg $tmp/pin.w.year.jpg`;$delete{"$tmp/pin.w.year.jpg"}=1;
$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`;my($w,$h) = &pnmsize($image);$image='';
if($w && $h) {my $xpos = 735 / 2;$w /= 2;$xpos -= $w;
`composite -geometry +$xpos+60 $tmp/pin.brnd.png $tmp/pin.w.year.jpg $tmp/pin.w.brand.jpg`;$delete{"$tmp/pin.w.brand.jpg"}=1;
$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;my $stypos=680;
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`;my($w,$h) = &pnmsize($image);$image='';
if($w && $h) {my $ypos = $stypos - $h;$ypos += 6;
`composite -geometry +52+$ypos $tmp/pin.brand.k.png $nextpr $tmp/pin.w.brand1.jpg`;$ypos -= 6;$delete{"$tmp/pin.w.brand1.jpg"}=1;
`composite -geometry +45+$ypos $tmp/pin.brand.png $tmp/pin.w.brand1.jpg $tmp/pin.w.brand.jpg`;$delete{"$tmp/pin.w.brand.jpg"}=1;
$nextpr="$tmp/pin.w.brand.jpg";
}

## PRINT SOME TEXT
my $prtext = $datain{pgtext};
## CLEAN TEXT (OPTIONAL)
$prtext =~ s/^\s+//;$prtext =~ s/^\W+//;$prtext =~ s/^\s+//;$prtext =~ s/\s+$//;$prtext =~ s/\W+$//;$prtext =~ s/\s+$//;$prtext =~ tr/ //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($w,$h) = &pnmsize($image);$image='';
my $vertpos=220;
if($w && $h) {my $xpos='';$vertpos += $h + 60;
if($w < 380) {$xpos=160;}
else {$xpos = 735 / 2;$w /= 2;$xpos -= $w;}
`composite -geometry +$xpos+220 $tmp/pin.text.png $nextpr $tmp/pin.i.jpg`;$delete{"$tmp/pin.i.jpg"}=1;
$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($w,$h) = &pnmsize($image);$image='';
my $readimg="$installdir/$datain{img}";
if($w && $h) {my $ypos=0;my $ssz=$w.'x180';
if($w > $h) {`convert -size $ssz xc:white $tmp/img.tmp.png`;$delete{"$tmp/img.tmp.png"}=1;
if($h < 180) {$ypos = (180-$h)/2;} else {
`convert $readimg -resize x180 $tmp/prevrt.jpg`;$delete{"$tmp/prevrt.jpg"}=1;
$image = `anytopnm $tmp/prevrt.jpg`;my($w1,$h1) = &pnmsize($image);$image='';my $tssz=$w1.'x180';
`convert -size $tssz xc:white $tmp/img.tmp.png`;
}
`composite -geometry +0+$ypos $readimg $tmp/img.tmp.png $tmp/orsmimg.jpg`;$delete{"$tmp/orsmimg.jpg"}=1;
} ## END WIDER THAN HEIGHT
else {
`convert $readimg -resize $ssz $tmp/orsmimg.jpg`;$delete{"$tmp/orsmimg.jpg"}=1;
} ## END IF HIGHER THEN WIDTH
$image = `anytopnm $tmp/orsmimg.jpg`;my($w,$h) = &pnmsize($image);$image='';
my $spaceav = 920 - $vertpos;
if($spaceav > 180) {
my $maxwid=450;my $koeff = $spaceav / $h;my $tryw = $koeff * $w;
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`;print "$tryw <= $maxwid\n";}
else {
my $tryht = (450 / $w) * $h;print "Try Height: $tryht (450 / $w) * $h\n";
`convert $tmp/orsmimg.jpg -resize 450x $tmp/orsmimg.jpg`;
print "$tryw > $maxwid\n";}
$image = `anytopnm $tmp/orsmimg.jpg`;($w,$h) = &pnmsize($image);$image='';
} ## 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;my $i;
for ($i = @$array; --$i; ) {my $j = int rand ($i+1);next if $i == $j;@$array[$i,$j] = @$array[$j,$i];}
} ## 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.There's no place like ~
Data file and image examplePosted: Tuesday, October 15, 2019 [03:25:17] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 357
This is example of data file used to produce an image:
View Codedir1tables
line1Buy Now!
headerChestnut Dining Tables
hd2On Sale Now
pgtextGreat Selection Summer Sale
imgimages/tables/chestnut.jpg

and this is the resulting Pin:

Data file and image example
Pin was actually created by the program code listed aboveThere's no place like ~
Actual screen recordingPosted: Wednesday, November 13, 2019 [22:37:02] - 3
rootPosted by:rootMember Since:
June 16 2010
Posts: 357
Here you can find a screen recording:
www.codemacs.com/coding/a..
Actual posting to PinterestThere's no place like ~
coding / perlPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group