IMAGE PROCESSING + BARCODES
import sys
import time
from PIL import Image, ImageDraw, ImageFont
file_in = sys.argv[1]
#file_in = "BARCODE.RF85907738000300013390001.1.jpg.fixed.jpg.resized.jpg"
text_in = sys.argv[2]
image = Image.open(file_in)
#image = Image.open("BARCODE.RF85907738000300013390001.1.jpg.fixed.jpg.resized.jpg")
font_type = ImageFont.truetype('arial.ttf', 14)
draw = ImageDraw.Draw(image)
draw.text(xy=(20,50), text=text_in, fill=(0,0,0), font=font_type)
#image.show()
image.save(file_in + ".ok.jpg")
# image = Image.open(file_in)
# font_type = ImageFont.truetype('arial.ttf', 18)
# draw = ImageDraw.Draw(image)
# draw.text(xy=(60,50), text="HELLO2", fill=(255,69,0), font=font_type)
# image.show()
# image.save(str(file_in) + ".ok.jpg")
1. stringtobarcode : Converts given string to barcode
2. pngtojpeg : Converts .png to .jpeg or .jpg
3. jpegtopng : Converts .jpg or .jpeg to .png
4. resizejpg : Converts size of jpg but keeps ratio
5. addtexttojpg : Adds specific text to jpg
def resizejpg(basewidth, inputname, outputname):
import PIL
#from PIL import Image
#basewidth = 300
img = PIL.Image.open(str(inputname))
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save(str(outputname))
def stringtobarcode(string_input, file_out):
import code128
string_in = str(string_input)
file_outp = str(file_out)
code128.image(string_in).save(file_outp)
def resize_pad(input_image):
from PIL import Image, ImageOps
# original size =500
desired_size = 500
im_pth = str(input_image)
output_image_fixed = str(input_image) + ".resized.jpg"
im = Image.open(im_pth)
old_size = im.size
ratio = float(desired_size)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
im = im.resize(new_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (desired_size, desired_size), "white")
new_im.paste(im, ((desired_size-new_size[0])//2, (desired_size-new_size[1])//2))
new_im.save(output_image_fixed)
return output_image_fixed
def addtext(input_image, input_text):
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
#input_tuple = ((0,0), str(input_text), (255,255,255))
output_image = str(input_image) + ".addtext.jpg"
img = Image.open(input_image)
#img - img.convert("RGBA")
font_type = ImageFont.truetype("arial.ttf", 14)
draw = ImageDraw.Draw(img)
draw.text(xy=(10, 10), text="HELLO WORLD", fill=(255,69,0), font=font_type)
# This works before resize
#draw.text((0, 0), str(input_text), font=font)
#draw.text((0, 0), str(input_text), (255,255,255), font=font)
#draw.text(input_tuple, font=font)
#draw.text(str(rf_in), font=font)
img.save(output_image)
def addtext2(input_image, input_text):
from PIL import Image, ImageDraw, ImageFont
output_image = str(input_image) + ".addtext2.jpg"
image = Image.open(input_image)
font_type = ImageFont.truetype('arial.ttf', 18)
draw = ImageDraw.Draw(image)
draw.text(xy=(60,50), text="HELLO", fill=(255,69,0), font=font_type)
image.show()
image.save(output_image)
##############################################################
# RESIZE JPG OR PNG
def resizejpg(basewidth, inputname, outputname):
import PIL
#from PIL import Image
#basewidth = 300
img = PIL.Image.open(str(inputname))
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save(str(outputname))
resizejpg(100, "RFTEST2.png", "RFTESTRESIZED.png")
resizejpg(500, "RFTEST2.png", "RFTESTRESIZED2.png")
resizejpg(1500, "RFTEST2.png", "RFTESTRESIZED3.png")
resizejpg(1500, "RFTEST2.jpg", "RFTESTRESIZED4.jpg")
resizejpg(1500, "RFTEST2.jpg", "RFTESTRESIZED5.jpeg")
resizejpg(1500, "RFTEST2.jpg", "RFTESTRESIZED6.png")
####################################################
#def convertToJpeg(im):
# with BytesIO() as f:
# im.save(f, format='JPEG')
# return f.getvalue()
#imag = Image.open("RFTEST2.png")
#im = Image.open("infile.png")
#imag.save("outfile.jpg")
def pngtojpeg(im):
'''
Name: pngtojpeg
Description: Converts png to jpeg
Input: string - path name
Output: None, converts the image and stores in same folder
Usage: pngtojpeg("/path/to/png")
Notes: Full path names require to be raw strings
prepend r, like: r'C:\...."
'''
from PIL import Image
name = str(im)
outname = name.replace(".png", ".jpg")
imag = Image.open(str(im))
imag.save(outname)
def jpegtopng(im):
'''
Name: pngtojpeg
Description: Converts png to jpeg
Input: string - path name
Output: None, converts the image and stores in same folder
Usage: pngtojpeg("/path/to/png")
Notes: Full path names require to be raw strings
prepend r, like: r'C:\...."
'''
from PIL import Image
name = str(im)
outname = name.replace(".jpeg", ".png")
imag = Image.open(str(im))
imag.save(outname)
pngtojpeg("RFTEST2.png")
jpegtopng("TESTBARCODEOUT2.jpeg")
#imag.save(str(convertToJpeg(imag)))
####################################################
#def convertToJpeg(im):
# with BytesIO() as f:
# im.save(f, format='JPEG')
# return f.getvalue()
#imag = Image.open("RFTEST2.png")
#im = Image.open("infile.png")
#imag.save("outfile.jpg")
def pngtojpeg(im):
'''
Name: pngtojpeg
Description: Converts png to jpeg
Input: string - path name
Output: None, converts the image and stores in same folder
Usage: pngtojpeg("/path/to/png")
Notes: Full path names require to be raw strings
prepend r, like: r'C:\...."
'''
from PIL import Image
name = str(im)
outname = name.replace(".png", ".jpg")
imag = Image.open(str(im))
imag.save(outname)
def jpegtopng(im):
'''
Name: pngtojpeg
Description: Converts png to jpeg
Input: string - path name
Output: None, converts the image and stores in same folder
Usage: pngtojpeg("/path/to/png")
Notes: Full path names require to be raw strings
prepend r, like: r'C:\...."
'''
from PIL import Image
name = str(im)
outname = name.replace(".jpeg", ".png")
imag = Image.open(str(im))
imag.save(outname)
pngtojpeg("RFTEST2.png")
jpegtopng("TESTBARCODEOUT2.jpeg")
#imag.save(str(convertToJpeg(imag)))
##### Manually
import code128
#from PIL import Image
# CONVERT STRING TO BARCODE
code128.image("Hello World").save("Hello World.png") # with PIL present
code128.image("RFTEST1").save("RFTEST.png")
code128.image("RF32905738000001000000070").save("RFTEST2.png")
code128.image("RF32905738000001000000070").save("RFTESTXXX.jpeg")
code128.image("RF32905738000001000000070").save("RFTESTXX1.jpg")
##### Function
def stringtobarcode(string_input, file_out):
import code128
string_in = str(string_input)
file_outp = str(file_out)
code128.image(string_in).save(file_outp)
stringtobarcode("RF32905738000001000000070", "TESTBARCODEOUT.png")
stringtobarcode("RF32905738000001000000070", "TESTBARCODEOUT2.jpeg")
###### Reading from file
def filetoliststrip(file):
'''
Function: filetoliststrip
Description: Reads a file, stores in list (stripped)
Input: File
Output: List
Usage: print (filetoliststrip("C:\\Users\\p.doulgeridis\\Desktop\\testpy.txt"))
Notes: Path needs double \\ or reverse /
'''
file_in = str(file)
lines = list(open(file_in, 'r'))
content = [x.strip() for x in lines]
return content
fileinlist = filetoliststrip("JUSTCODES.TXT")
counter = 0
for j in fileinlist:
counter += 1
outname = "BARCODE" + str(counter) + ".jpg"
stringtobarcode(str(j),str(outname))
#########################################################
# Write to svg file
#with open("Hello World2.svg", "w") as f:
# f.write(code128.svg("Hello World"))
#with open("Hello World2.svg", "a") as f:
# f.write(code128.svg("pan"))