[Kotlin] I wanted to generate a png with a large capacity per area [Java]

What I wanted to do

A certain project required a large amount of heavy png image samples. On the other hand, when the image was generated in the form of simply filling the square, it was compressed and became about several KB at most, so the capacity could not be secured. Since the required image format was also decided, it was difficult to simply increase the area and gain capacity.

policy

Compression usually works strongly when the following two points are true.

--The same pattern continues --The same color continues

Therefore, I did it with the glue that "If you draw a lot of random lines of random colors, it will be difficult to compress."

The target is a size of 1940 x 500 and 2MB per sheet. As long as we have achieved that, we will do it without worrying about further increase in capacity and processing time.

Implementation

This is a sample to generate 2000 images in the $ {project root} / build / generated directory. If there are 100000 lines, it will stably exceed 2MB.

parallelStream () is about "It will be a little faster", and I have not confirmed whether it is actually faster.

import java.awt.Color
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import kotlin.random.Random

const val width = 1940
const val height = 500
const val lines = 100000 //Number of lines
const val count = 2000

fun main() {
    val generateTarget = System.getProperty("user.dir") + "/build/generated/"

    List(count) { it }.parallelStream().forEach { index ->
        val img = BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
        (img.graphics as Graphics2D).apply {
            repeat(lines) {
                //The transparency is also specified with the aim of increasing the capacity of the color as much as possible.
                this.color = Random.nextBytes(4).let {
                    Color(it[0] + 128, it[1] + 128, it[2] + 128, it[3] + 128)
                }
                this.drawLine(Random.nextInt(width), Random.nextInt(height), Random.nextInt(width), Random.nextInt(height))
            }
            dispose()
        }
        ImageIO.write(img, "png", File("$generateTarget${index}.png "))
    }
}

Execution result

It was 4.81GB with 2000 sheets and 2.4MB on average. The load is high and the execution time is reasonable, so I think it's best to turn it around in your spare time.

An image like this will be generated. 0.png

Recommended Posts

[Kotlin] I wanted to generate a png with a large capacity per area [Java]
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to break a block with java (1)
I want to transition screens with kotlin and java!
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I just wanted to make a Reactive Property in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to implement various functions with kotlin and java!
I tried to interact with Java
A story that I struggled to challenge a competition professional with Java
I want to return to the previous screen with kotlin and java!
I just wanted to logrotate with log4j 2
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I wanted to implement a slide show in a fashionable way with slick.
I made a rock-paper-scissors app with kotlin
I want to ForEach an array with a Lambda expression in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want to play a GIF image on the Andorid app (Java, Kotlin)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried to automatically generate a class to convert from a data class to a Bundle with APT
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I tried to make Basic authentication with Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
I want to use java8 forEach with index
Getting started with Kotlin to send to Java developers
What I did when I converted java to Kotlin
I wanted to gradle spring boot with multi-project
I tried to create a shopping site administrator function / screen with Java and Spring
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I want to implement it additionally while using kotlin on a site running Java
I tried what I wanted to try with Stream softly.
Submit a job to AWS Batch with Java (Eclipse)
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
Call a method with a Kotlin callback block from Java
How to manually generate a JWT with Rails Knock
I tried to create a Clova skill in Java
I want to monitor a specific file with WatchService
I tried to make a login function in Java
I tried OCR processing a PDF file with Java
I tried to implement Stalin sort with Java Collector
I wanted to develop PHP with vscode remote container
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
[Java] How to start a new line with StringBuilder
I want to write a loop that references an index with Java 8's Stream API
I want to return a type different from the input element with Java8 StreamAPI reduce ()
Java program to resize a photo into a square with margins
How to use trained model of tensorflow2.0 with Kotlin / Java
[Java] I want to convert a byte array to a hexadecimal number
I tried to summarize the basics of kotlin and java
[Rails] I tried to create a mini app with FullCalendar
[Beginner] Try to make a simple RPG game with Java ①
I made a plugin to execute jextract with Gradle task
[Beginner] I made a program to sell cakes in Java