Skip to content

Commit

Permalink
Add support for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-mous committed Sep 3, 2020
1 parent d8dc88b commit 1c1979e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 81 deletions.
8 changes: 7 additions & 1 deletion CoinPictureManager/ChromaKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ void chromaKey(Mat *img) {
*/
void updateDisplay() {
Mat img_show;
Size newsize(900 * bgra.cols / bgra.rows, 900); //Resize image to a reasonable size
#ifdef _WIN32
Size newsize(GetSystemMetrics(SM_CYSCREEN) - 200, (GetSystemMetrics(SM_CYSCREEN) - 200) * img_show.rows / img_show.cols); //Resize image to a reasonable size for display
#elif __linux__
Display* d = XOpenDisplay(NULL);
Screen* s = DefaultScreenOfDisplay(d);
Size newsize(s->width - 200, (s->height - 200) * img_show.rows / img_show.cols); //Resize image to a reasonable size for display
#endif
resize(bgra, img_show, newsize);

//std::chrono::time_point<std::chrono::system_clock> start_time = std::chrono::system_clock::now();
Expand Down
30 changes: 15 additions & 15 deletions CoinPictureManager/CoinPictureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ std::string verify_str = "Files MUST be organized as follows : \n\
* @param root_dir Top directory (to search below)
* @param verbose Verbose
*/
int renameFiles(fs::path root_dir, boolean verbose) {
int renameFiles(fs::path root_dir, bool verbose) {
std::cout << "Renaming files in subdirectories" << std::endl;
for (auto &d : fs::directory_iterator(root_dir)) { //Each sub-directory
for (auto &d: fs::directory_iterator(root_dir)) { //Each sub-directory
if (fs::is_directory(d)) {
int f_no = 0;
if (verbose) std::cout << "\tDirectory: " << d.path().filename() << std::endl;
for (auto &f : fs::directory_iterator(d)) { //Each image file
for (auto &f: fs::directory_iterator(d)) { //Each image file
if (isImage(f.path().extension().string())) {
std::string name = std::to_string(f_no);
name.insert(name.begin(), 4 - name.length(), '0');
Expand All @@ -105,13 +105,13 @@ int renameFiles(fs::path root_dir, boolean verbose) {
* @param max_imgs Maximum number of images to use (-1 for all)
* @param verbose Verbose
*/
int createThumbnail(fs::path root_dir, int max_imgs, boolean verbose) {
int createThumbnail(fs::path root_dir, int max_imgs, bool verbose) {
if (max_imgs > 0) {
std::cout << "Creating thumbnail files in subdirectories with a maximum number of pictures " << max_imgs << "..." << std::endl;
} else {
std::cout << "Creating thumbnail files in subdirectories..." << std::endl;
}
for (auto &d : fs::directory_iterator(root_dir)) { //Each sub-directory
for (auto &d: fs::directory_iterator(root_dir)) { //Each sub-directory
if (fs::is_directory(d)) {
if (verbose) std::cout << "\tDirectory: " << d.path().filename() << std::endl;
createThumbnail(d.path(), 250, max_imgs);
Expand All @@ -126,7 +126,7 @@ int createThumbnail(fs::path root_dir, int max_imgs, boolean verbose) {
* @param root_dir Top directory (to search below)
* @param verbose Verbose
*/
int createWebp(fs::path root_dir, boolean verbose) {
int createWebp(fs::path root_dir, bool verbose) {
std::cout << "Creating WebP images..." << std::endl;
for (auto &d : fs::directory_iterator(root_dir)) { //Each sub-directory
if (fs::is_directory(d)) {
Expand All @@ -143,9 +143,9 @@ int createWebp(fs::path root_dir, boolean verbose) {
* @param root_dir Top directory (to search below)
* @param verbose Verbose
*/
int chromaKey(fs::path root_dir, boolean verbose) {
int chromaKey(fs::path root_dir, bool verbose) {
std::cout << "Running chroma keying..." << std::endl;
for (auto &d : fs::directory_iterator(root_dir)) { //Each sub-directory
for (auto &d: fs::directory_iterator(root_dir)) { //Each sub-directory
if (fs::is_directory(d)) {
if (verbose) std::cout << "\tDirectory: " << d.path().filename() << std::endl;
for (auto &f : fs::directory_iterator(d)) { //Each image file
Expand All @@ -165,9 +165,9 @@ int chromaKey(fs::path root_dir, boolean verbose) {
* @param root_dir Top directory (to search below)
* @param verbose Verbose
*/
int cropImages(fs::path root_dir, boolean verbose) {
int cropImages(fs::path root_dir, bool verbose) {
std::cout << "Cropping images..." << std::endl;
for (auto &d : fs::directory_iterator(root_dir)) { //Each sub-directory
for (auto &d: fs::directory_iterator(root_dir)) { //Each sub-directory
if (fs::is_directory(d)) {
if (verbose) std::cout << "\tDirectory: " << d.path().filename() << std::endl;
for (auto &f : fs::directory_iterator(d)) { //Each image file
Expand All @@ -187,7 +187,7 @@ int cropImages(fs::path root_dir, boolean verbose) {
* @param command Integer command as defined in getSelection
* @return success code
*/
int runCommand(char command, boolean verbose, boolean interactive_mode, fs::path root_dir) {
int runCommand(char command, bool verbose, bool interactive_mode, fs::path root_dir) {
switch (command) {
case '1':
return renameFiles(root_dir, verbose);
Expand Down Expand Up @@ -226,7 +226,7 @@ int runCommand(char command, boolean verbose, boolean interactive_mode, fs::path
*
* @return status code
*/
int runUI(fs::path root_dir, boolean verbose) {
int runUI(fs::path root_dir, bool verbose) {
std::cout << help_str;
while (1) {
char sel;
Expand All @@ -241,7 +241,7 @@ int runUI(fs::path root_dir, boolean verbose) {
}

int main(int argc, char **argv) { //Main loop - parse any command line options and run either command or interactive mode
boolean run_ui = false, verbose = false;
bool run_ui = false, verbose = false;
std::vector<char> commands;
fs::path root_dir = fs::path(DEFAULT_PATH);

Expand All @@ -257,7 +257,7 @@ int main(int argc, char **argv) { //Main loop - parse any command line options a
verbose = true;
} else if (argv[i][1] == 'c') { //Run command
if (strlen(argv[i]) > 3 && argv[i][2] == '=') {
for (int c=3; c<strlen(argv[i]); c++) {
for (unsigned int c = 3; c < strlen(argv[i]); c++) {
commands.push_back(argv[i][c]);
}
} else {
Expand All @@ -282,7 +282,7 @@ int main(int argc, char **argv) { //Main loop - parse any command line options a

//Run any commands
if (commands.size() > 0) {
for (int i = 0; i < commands.size(); i++) {
for (unsigned int i = 0; i < commands.size(); i++) {
char comm = commands.at(i);
int status = runCommand(comm, verbose, false, root_dir);
if (status > 0) return 1; //Exit upon error
Expand Down
10 changes: 8 additions & 2 deletions CoinPictureManager/CropImages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void getBounds(Mat img, Rect *bounding_rect) {
vector<vector<Point>> cnts;
findContours(canny, cnts, RETR_TREE, CHAIN_APPROX_SIMPLE);

for (int i = 0; i < cnts.size(); i++) {
for (unsigned int i = 0; i < cnts.size(); i++) {
float peri = arcLength(cnts[i], true);
vector<Point> cnts_poly(1);
approxPolyDP(cnts[i], cnts_poly, 0.0015 * peri, true);
Expand Down Expand Up @@ -112,7 +112,13 @@ void onCropTrackbar(int sp, void *val) {//Update the current shown image
plotBounds(&img_show, &bounding_rect);
cropImage(&img_show, &bounding_rect);

Size newsize(GetSystemMetrics(SM_CYSCREEN)-200, (GetSystemMetrics(SM_CYSCREEN)-200) * img_show.rows / img_show.cols); //Resize image to a reasonable size for display
#ifdef _WIN32
Size newsize(GetSystemMetrics(SM_CYSCREEN)-200, (GetSystemMetrics(SM_CYSCREEN)-200) * img_show.rows / img_show.cols); //Resize image to a reasonable size for display
#elif __linux__
Display* d = XOpenDisplay(NULL);
Screen* s = DefaultScreenOfDisplay(d);
Size newsize(s->width - 200, (s->height - 200) * img_show.rows / img_show.cols); //Resize image to a reasonable size for display
#endif
resize(img_show, img_show, newsize);
imshow(crop_window_name, img_show); //Show final image
}
Expand Down
38 changes: 23 additions & 15 deletions CoinPictureManager/Dependencies.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#ifndef DEPENDENCIES_H
#define DEPENDENCIES_H
#define DEPENDENCIES_H

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <iomanip>
#include <filesystem>
#include <string>
#include <stdlib.h>
#include <Windows.h>
#include <filesystem>
namespace fs = std::experimental::filesystem;
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <iomanip>
#include <filesystem>
#include <string>
#include <stdlib.h>


using namespace std;
using namespace cv;
#ifdef _WIN32
#include <Windows.h>
#include <filesystem>
#elif __linux__
#include <X11/Xlib.h>
#include <experimental/filesystem>
#endif

namespace fs = std::experimental::filesystem;

using namespace std;
using namespace cv;

#endif
6 changes: 3 additions & 3 deletions CoinPictureManager/ImageFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* Determine if file is an acceptable image format based on extension
*
* @param ext String of the file extension (case-insensitive)
* @return boolean if file is an image
* @return bool if file is an image
*/
boolean isImage(std::string ext) {
bool isImage(std::string ext) {
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
return ext==".jpg" || ext==".jpeg" || ext==".jpe" || ext==".jp2" || ext==".png";
}
Expand Down Expand Up @@ -95,7 +95,7 @@ int createThumbnail(fs::path image_dir, int thumbnail_height, int max_pics) {
* @param quality WebP image quality (0-100)
* @param verbose Verbose
*/
int createWebp(fs::path image_dir, int quality, boolean verbose) {
int createWebp(fs::path image_dir, int quality, bool verbose) {
vector<int> params;
params.push_back(IMWRITE_WEBP_QUALITY);
params.push_back(quality);
Expand Down
4 changes: 2 additions & 2 deletions CoinPictureManager/ImageFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "Dependencies.h"

boolean isImage(std::string ext); //Determine if the file is an image
bool isImage(std::string ext); //Determine if the file is an image

int createThumbnail(fs::path image_dir, int thumbnail_height, int max_pics); //Create a thumbnail image given the images in the directory at path with a maximum number of pictures max_pics

int createWebp(fs::path image_dir, int quality, boolean verbose); //Create WebP versions of each JPEG image file in image_dir
int createWebp(fs::path image_dir, int quality, bool verbose); //Create WebP versions of each JPEG image file in image_dir

#endif
27 changes: 27 additions & 0 deletions CoinPictureManager/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CC := g++
CFLAGS := -g -Wall -pedantic

SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(SRCS))

LIBS := -lopencv_core -lopencv_photo -lopencv_highgui -lopencv_calib3d -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lstdc++fs -lX11
INCLD := -I/usr/local/include/opencv4

PROG := coinpicturemanager

all: $(PROG)

coinpicturemanager: $(OBJS)
${CC} $(CFLAGS) -o $@ $^ $(LIBS) $(INCLD)

%.o: %.cpp
${CC} $(CFLAGS) -c $< $(INCLD)

clean:
rm -rf $(OBJS)

install:
mv $(PROG) /usr/local/bin/$(PROG)

remove:
rm -rf /usr/local/bin/$(PROG)
32 changes: 0 additions & 32 deletions CoinPictureManager/Thumbnail.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions CoinPictureManager/Thumbnail.h

This file was deleted.

14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
- For the latest stable version, please see the releases tab and download the EXE file.
- Next, either place this file in the directory where you want to manage pictures or place it in a safe directory and add the variable to the path as above so it can be run globally (run the command `SET PATH=%PATH%;c:\PATH\TO\YOUR\EXE`)
- *To download the latest code, clone this repository and open the project in Visual Studio*

### Linux
*To be added*
## Usage
- A Makefile is provided for easy code compilation. Install make with `sudo apt-get install make` if you do not have it already.
- Run `make` in the CoinPictureManager directory to compile the code. The finished program will be in a file called coinpicturemanager.
- Use `make clean` to remove the build files
- To install the command system-wide, run `make install` (`make remove` to remove it).

## Usage
`coinpicturemanager [DIRECTORY] [OPTIONS]`

*Without a specified directory, the app uses the current directory. Without any options or when run as a desktop app on Windows, it will run in verbose interactive mode.*

### Options
-h Print this help
-i Interactive mode (default unless other option specified)
-v Verbose mode
-h Print this help
-i Interactive mode (default unless other option specified)
-v Verbose mode
-c=COMMANDS Run command(s) (commands run in order listed; see available commands below)

### Commands
Expand Down

0 comments on commit 1c1979e

Please sign in to comment.