Converting Images to Halftone in Processing
in
Contributed Library Questions
•
5 months ago
Hi All,
I am quite new to processing, and have been reading up on this subject, understanding the syntax is sure not easy for me to pick up within a month.
Below: Is the code that my supervisor has help me write, I do not take ownership of the codes. But I would like to play around with it.
Here's a video of me toying with it on:
halftone app
- import sojamo.drop.*;
- import processing.pdf.*;
- SDrop drop;
- import controlP5.*;
- ControlP5 cp5;
- Textlabel myTextlabelA;
- Textlabel myTextlabelB;
- int myColorBackground = color(0, 0, 0);
- ControlWindow controlWindow;
- public int sliderValue = 40;
- PImage img;
- void setup() {
- size(1024, 720);
- frameRate(30);
- drop = new SDrop(this);
- ellipseMode(CENTER);
- img = loadImage(you can insert your image,);
- img.get () ;
- noStroke();
- smooth();
- cp5 = new ControlP5(this);
- myTextlabelA = cp5.addTextlabel("label")
- .setText("HALFTONE PROGRAM")
- .setPosition(250,25)
- .setColorValue(255)
- .setFont(createFont("Arial",22))
- ;
- cp5 = new ControlP5(this);{
- // PLEASE READ
- //
- // With controlP5 2.0 the ControlWindow has been removed,
- // please see the changelog.txt for details.
- // Instead, see the extra/ControlP5frame example for
- // a ControlWindow alternative.
- // controlWindow = cp5.addControlWindow("controlP5window", 100, 100, 400, 200)
- // .hideCoordinates()
- // .setBackground(color(40))
- // ;
- cp5.addSlider("sliderValue")
- .setRange(0, 255)
- .setPosition(40, 40)
- .setSize(200, 29)
- //.moveTo(controlWindow)
- ;
- }
- }
- void draw() {
- imageMode (CENTER) ; {
- image(img, 50, 50, 80, 80); // Draw image using CENTER mode
- }
- background( 255 );
- fill( 255, 0, 0 );
- if(img !=null) {
- if(mousePressed) {
- scl = map(mouseX, 0 ,width, 0.5, 50);
- }
- for (int y=0;y<img.height;y+=steps) {
- for (int x=0;x<img.width;x+=steps) {
- float val = ( red( img.get(x, y) ) + green( img.get(x, y) ) + blue( img.get(x, y) ) ) / 3;
- float s = map(val, 0, 255, 1, 3 * scl );
- s = (3 * scl) - s;
- ellipse(x*scl, y*scl, s * dotSize , s * dotSize );
- }
- }
- }
- }
- float dotSize = 1;
- float scl = 4;
- int steps = 3;
- void keyPressed() {
- switch(key) {
- case('='): dotSize+=0.03;break;
- case('-'): dotSize-=0.03;break;
- case('s'): export();break;
- }
- }
- void export() {
- println("generating halftone ... ");
- PGraphics c0 = createGraphics(int(img.width * scl) , int(img.height * scl), PDF, "output.pdf");
- c0.beginDraw();
- c0.image(img, 0, 0);
- c0.resize (0, 50);
- c0.background( 255 );
- c0.fill( 255, 0, 0 );
- c0.noStroke();
- for (int y=0;y<img.height;y+=steps) {
- for (int x=0;x<img.width;x+=steps) {
- float val = ( red( img.get(x, y) ) + green( img.get(x, y) ) + blue( img.get(x, y) ) ) / 4;
- float s = map(val, 0, 255, 1, 3 * scl );
- s = (3 * scl) - s;
- c0.ellipse(x*scl, y*scl, s * dotSize , s * dotSize );
- }
- }
- c0.endDraw(); // stop and save pdf
- c0.dispose();
- println("... done exporting.");
- }
- void dropEvent(DropEvent theDropEvent) {
- println("");
- println("isFile()\t"+theDropEvent.isFile());
- println("isImage()\t"+theDropEvent.isImage());
- println("isURL()\t"+theDropEvent.isURL());
- // if the dropped object is an image, then
- // load the image into our PImage.
- if(theDropEvent.isImage()) {
- println("### loading image ...");
- img = theDropEvent.loadImage();
- }
- }
I am using Sojamo Drop function (drop image into processing) and have change the export to PDF format versus, P2D (jpeg/pixelated)
The program works great, but the exported file is far too big.
For example;
For example;
drop image into processing: 55KB
exported image: approx 4.4mb
I understand what pdf export does; it exports out as vectorize image as well, making the filesize far too big, but again, when I open up the file, it takes far too long to open. OR When I want to edit the images in Adobe illustrator, it becomes, I notice they save the "drop" image together with vectored ellipse.
Is there anyway I could, 1) Render the images quickly. 2) Export it without using too space but maintain its vectored dots. 3) I would also like to center the 'drop' image within the size of (1024,720)
notes: I am just annoyed during the real time editing. (it gets really slow) but apart from that, converting it into jpeg, No problems with that.
Help?
Thanks.
Is there anyway I could, 1) Render the images quickly. 2) Export it without using too space but maintain its vectored dots. 3) I would also like to center the 'drop' image within the size of (1024,720)
notes: I am just annoyed during the real time editing. (it gets really slow) but apart from that, converting it into jpeg, No problems with that.
Help?
Thanks.
1