Jump to content
3 posts in this topic

Recommended Posts

I am needing a script that will take a text file that has an image URL and 2 lines of text and create one image out of the URL image with the 2 lines of text underneath it all in one image.

 

So the text file would contain:

 

http://www.url.com/to/image

Text Line 1

Text Line 2

 

-----

 

I need the final image to look like the file I have attached.

post-1029734-0-17816400-1340409937_thumb.png

Link to comment
https://www.insanelymac.com/forum/topic/279991-combine-text-file-and-image/
Share on other sites

  • 3 weeks later...

You could do this using Python, but first you'll need to install the pil module.

 

Below is a bash script to install the module and libs that you need (you'll need Xcode/Xcode Command Line Tools installed):

 

#!/bin/bash
JPEG_LION="http://rudix.googlecode.com/files/libjpeg-8d-0.pkg"
JPEG_SNOW="http://rudix-snowleopard.googlecode.com/files/libjpeg-8d-0.pkg"

echo "Select MacOS Version:"
echo "[1] Lion"
echo "[2] Snow Leopard"
read selection

case $selection in
1) OPTION=$JPEG_LION;;
2) OPTION=$JPEG_SNOW;;
*) Echo "No Selection Made"
exit 0
;;
esac
curl -O $OPTION

sudo installer -pkg libjpeg-8d-0.pkg -target /
sudo easy_install pip
sudo pip install pil
sudo pip install pillow

 

Then you could try something like this in Python:

 

#!/usr/bin/env python
import os,sys,urllib2,Image,ImageDraw,ImageFont
fname = raw_input("Enter Filename: ")
txt = open(fname).readlines()
asset,text1,text2,blank = txt
savedFilename = os.path.basename(asset).strip()
imgfile = urllib2.urlopen(asset)
savedfile = open(savedFilename,'wb')
savedfile.write(imgfile.read())
savedfile.close()

im = Image.open(savedFilename)
font = ImageFont.truetype('/Library/Fonts/Verdana Bold.ttf',14)
box = im.getbbox()
im2=im.crop((box[0], box[1], box[2], box[3]+50))
draw=ImageDraw.Draw(im2)
draw.text((box[1]+10,box[3]+10), text1.strip(),font=font)
draw.text((box[1]+10,box[3]+30), text2.strip(), font=font)
im2.save('output.jpg','JPEG')

 

The Python script reads a single text file that has the 3 entries you listed, terminated with a blank line.

 

HTH,

 

AIR.

×
×
  • Create New...