Image Processing: Automatic Certificate Generation using R

Alok Pratap Singh

Research Scholar, Department of Psychology, University of Allahabad. Date: 04 July, 2021

Somewhere I read the article “Automatic Certificate Generation Using Python”. The thought came, can the same be made using “R Programming”. After a lot of scrambling, I got the way.

Installing EBImage

EBImage provides general purpose functionality for image processing and analysis. In the context of (high-throughput) microscopy-based cellular assays, EBImage offers tools to segment cells and extract quantitative cellular descriptors. This allows the automation of such tasks using the R programming language and facilitates the use of other tools in the R environment for signal processing, statistical modeling, machine learning and visualization with image data.

for more please visit Introduction to EBImage

if(!require(BiocManager)) {install.packages("BiocManager",
                                            dependencies = T)}
## Loading required package: BiocManager
if(!require(EBImage)) {BiocManager::install("EBImage")}
## Loading required package: EBImage

Sample Certificate

Select any certificate and download it. After that, using Windows’ Paint, replace the information given in it with it’s background color.

Increasing contrast (^2) of the Image

# Reading template certificate file (sample_2.jpeg) form local machine to R environment
readImage("C:/Users/Asus/Downloads/Sample/sample_2.jpeg") -> sample_certificate    # Reading the Image 
sample_certificate^2 -> sample_certificate   # increasing Contrast
display(sample_certificate)   # View the Sample Template

Creating Sample Data

(You Can import .csv data using readr::read_csv(“path/your_filename”) -> participants)

set.seed(2021)   # For Replicability of Random values
data.frame(      # Creating a Dataframe
    id= 1:6,
    prefix = c("Mr.", "Dr.", "Mr.", "Mis.", "Mr.", "Dr."),
    first_n =  c("Rahul", "Arihant", "Ritesh", "Rita", "Karan", "Arjuna"),
    last_n=  c("Sakti", "Vanse", "Karan", "Kumari", "Sena", "Bhuma"),
    instiitute= c("XYX Deemed to be University", "Internatiol XYZ of Technology",
                  "National Study Hub", "BrainX XYZ of Research",
                  "ABC Learning Certer", "Sahara XYZ Group"),
    certificateId= sample(50:55, size = 6, replace = T)
           ) -> participants
# Converting seriesId to Roman Numbers
participants$certificateId <- as.roman(participants$certificateId)

set.seed(2021)
# Adding some Random Numbers to certificateId
participants$certificateId <- paste0(
    participants$certificateId,
    sample(500:1000, size = 6, replace = F))

participants$seritesId <- c(1998:2003)

Data for Certificate

participants
##   id prefix first_n last_n                    instiitute certificateId
## 1  1    Mr.   Rahul  Sakti   XYX Deemed to be University         LV890
## 2  2    Dr. Arihant  Vanse Internatiol XYZ of Technology         LV665
## 3  3    Mr.  Ritesh  Karan            National Study Hub         LI929
## 4  4   Mis.    Rita Kumari        BrainX XYZ of Research       LIII941
## 5  5    Mr.   Karan   Sena           ABC Learning Certer       LIII730
## 6  6    Dr.  Arjuna  Bhuma              Sahara XYZ Group         LV895
##   seritesId
## 1      1998
## 2      1999
## 3      2000
## 4      2001
## 5      2002
## 6      2003

Fonts family available (top 40) in Windows.

library(xkcd)     # xkcd help to pull the required fonts.
## Loading required package: ggplot2
## Loading required package: extrafont
## Registering fonts with R
fonts()[1:40]  # top 40 fonts available in windows
##  [1] "Titillium Web"               "Titillium Web Light"        
##  [3] "Public Sans Black"           "Public Sans ExtraBold"      
##  [5] "Public Sans ExtraLight"      "Public Sans Light"          
##  [7] "Public Sans Medium"          "Public Sans"                
##  [9] "Public Sans SemiBold"        "Public Sans Thin"           
## [11] "EconSansCndBol"              "EconSansCndBolIta"          
## [13] "EconSansCndLig"              "EconSansCndLigIta"          
## [15] "EconSansCndMed"              "EconSansCndMedIta"          
## [17] "EconSansCndReg"              "EconSansCndRegIta"          
## [19] "Agency FB"                   "Aparajita"                  
## [21] "Arial Black"                 "Arial"                      
## [23] "Arial Narrow"                "Arial Rounded MT Bold"      
## [25] "Bahnschrift"                 "Baskerville Old Face"       
## [27] "Bauhaus 93"                  "Bell MT"                    
## [29] "Berlin Sans FB"              "Berlin Sans FB Demi"        
## [31] "Bernard MT Condensed"        "Blackadder ITC"             
## [33] "Bodoni MT"                   "Bodoni MT Black"            
## [35] "Bodoni MT Condensed"         "Bodoni MT Poster Compressed"
## [37] "Book Antiqua"                "Bookman Old Style"          
## [39] "Bookshelf Symbol 7"          "Bradley Hand ITC"

Creating Certificates

for(i in 1:nrow(participants)) {
    display(sample_certificate, method= "raster")
    text(x=  540, y= 312,
         label= paste(participants$prefix[i],
                      participants$first_n[i],
                      participants$last_n[i],
                      "\n",
                      participants$instiitute[i]),
         cex=  1.4,
         col= "steelblue",
         font= 2,
         family= "Times New Roman")
    
    
    # Series ID
    text(x= 146, y= 684,
         label= paste0("Series ID: ",
                       participants$seritesId[i]),
         cex= .46,
         family= "Ubuntu",
         font= 1)
    
    
    # Certificate ID
    text(x= 146, y= 684+17,
         label= paste0("Certificate ID: ",
                       participants$certificateId[i]),
         cex= .46,
         family= "Ubuntu",
         font= 1)
    
    # Issue Date
    text(x= 146, y= 684+17+17,
         label= paste0("Issue Date: ",
                       "01 July 2021"),
         cex=.46,
         font= 1,
         family= "Ubuntu")
    # Expiration Date
    text(x= 146, y= 684+17+17+17,
         label= paste0("Expiration Date: ",
                       "01 July 2022"),
         cex= .46,
         font= 1,
         family= "Ubuntu")
    # Certified as
    text(x= 146, y= 684+17+17+17+17,
         label= paste("Certified As: ",
                      participants$prefix[i],
                      participants$first_n[i],
                      participants$last_n[i],
                      sep = " "),
         cex= .46,
         font= 1,
         family= "Ubuntu")
}

Saving the Certificates

for(i in 1:nrow(participants)) {
    
    png(paste0("C:/Users/Asus/Downloads/Sample/",     # Path to save the Certificates
           i, ".", participants$last_n[i], ".png"),
        width = 1020, height = 710, res= 160)
    display(sample_certificate, method= "raster") 
    
    
    text(x=  540, y= 312,
         label= paste(participants$prefix[i],
                      participants$first_n[i],
                      participants$last_n[i],
                      "\n",
                      participants$instiitute[i]),
         cex=  1.4,
         col= "steelblue",
         font= 2,
         family= "Times New Roman")
    
    
    # Series ID
    text(x= 146, y= 684,
         label= paste0("Series ID: ",
                       participants$seritesId[i]),
         cex= .46,
         family= "Ubuntu",
         font= 1)
    
    
    # Certificate ID
    text(x= 146, y= 684+17,
         label= paste0("Certificate ID: ",
                       participants$certificateId[i]),
         cex= .46,
         family= "Ubuntu",
         font= 1)
    
    # Issue Date
    text(x= 146, y= 684+17+17,
         label= paste0("Issue Date: ",
                       "01 July 2021"),
         cex=.46,
         font= 1,
         family= "Ubuntu")
    # Expiration Date
    text(x= 146, y= 684+17+17+17,
         label= paste0("Expiration Date: ",
                       "01 July 2022"),
         cex= .46,
         font= 1,
         family= "Ubuntu")
    # Certified as
    text(x= 146, y= 684+17+17+17+17,
         label= paste("Certified As: ",
                      participants$prefix[i],
                      participants$first_n[i],
                      participants$last_n[i],
                      sep = " "),
         cex= .46,
         font= 1,
         family= "Ubuntu")
    
    dev.off()
}

Done

Check your folder

display(readImage("C:/Users/Asus/Downloads/Sample/screenshot.JPG")^1.8,
        method= "raster")

Regards

Please visit my profile

Alok Pratap Singh (Research Scholar)

Linkedin (Open in New TAB)

Department of Psychology

University of Allahabad

Without data you’re just another person with an opinion