-
Notifications
You must be signed in to change notification settings - Fork 0
/
memes.cpp
98 lines (84 loc) · 2.68 KB
/
memes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* meme.cpp
*
* This file contains `meme` program to generate custom memes.
* Part of CS70 Homework 1. This file may NOT be
* shared with anyone other than the author(s) and
* the current semester's CS70 staff without
* explicit written permission from one of the
* CS70 instructors.
*
*/
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace std;
using namespace cv;
/**
* \brief Converts a string to spongebob mocking capitalization.
* \returns the modified string.
*/
string spongebobMocking(string input) {
// Makes every other character capitalized
for (size_t i = 0; i < input.length(); ++i) {
if (i%2 == 0) {
input[i] = tolower(input[i]);
} else {
input[i] = toupper(input[i]);
}
}
return input;
}
/**
* \brief Creates a meme with an image and a string of text.
* \input Three strings, representing the name of the source image file,
* the text to add to the image, and the name of the destination
* image file.
* \returns 0 if everything is successful, -1 otherwise.
*/
int makeMeme(string inputFilename, string text, string outputFilename) {
// Open the image file
cout << "Opening file: " << inputFilename << endl;
Mat image = imread(inputFilename);
if (image.empty()) {
cerr << "Could not open or find the image " << inputFilename << endl;
return -1;
}
// Add text to the image
// position for your text.
int font = FONT_HERSHEY_PLAIN;
float textScaling = 2.0;
Scalar textColor = Scalar(39, 68, 136);
Point textPosition = Point(30, 30);
text = spongebobMocking(text);
cout << "Adding text: " << text << endl;
putText(image, text, textPosition, font, textScaling, textColor);
// Write out the modified image
cout << "Writing file: " << outputFilename << endl;
bool isSuccess = imwrite(outputFilename, image);
if (!isSuccess) {
cerr << "Could not write the image " << outputFilename << endl;
return -1;
}
return 0;
}
/**
* \brief Gets input from the user, then uses that input to call makeMeme.
* \input None, but asks for three input strings from the user with getline().
* \returns 0 if everything is successful, -1 otherwise.
*/
int main() {
// asks user for file name
string inputFilename;
cout << "Enter image file name: ";
getline(cin, inputFilename);
// asks user for meme text
string text;
cout << "Enter meme text: ";
getline(cin, text);
// asks user for output file name
string outputFilename;
cout << "Enter output file name: ";
getline(cin, outputFilename);
return makeMeme(inputFilename, text, outputFilename);
}